Skip to content

Commit 6c9ca8d

Browse files
committed
Update CheckoutForm.vue
1 parent e7dc217 commit 6c9ca8d

File tree

1 file changed

+29
-51
lines changed

1 file changed

+29
-51
lines changed

components/Checkout/CheckoutForm.vue

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script setup>
3737
import { Form, Field, ErrorMessage } from "vee-validate";
3838
import { uid } from "uid";
39-
import { ref, onMounted } from "vue";
39+
import { onMounted } from "vue";
4040
import { useCart } from "@/store/useCart";
4141
4242
import { BILLING_FIELDS, BILLING_SCHEMA } from "./constants/BILLING_FIELDS";
@@ -54,8 +54,6 @@ const upperCaseFirstChar = (input) =>
5454
5555
const paymentMethod = "cod";
5656
const cart = useCart();
57-
const orderData = ref(null);
58-
const isLoading = ref(false);
5957
6058
// Clear WooCommerce session
6159
const clearWooCommerceSession = () => {
@@ -65,61 +63,15 @@ const clearWooCommerceSession = () => {
6563
}
6664
};
6765
68-
// Set up mutation with variables
69-
const { mutate, onDone, onError } = useMutation(CHECKOUT_MUTATION, {
70-
variables: computed(() => ({ input: orderData.value })),
71-
});
72-
73-
onDone(async (result) => {
74-
const { data } = result;
75-
clearWooCommerceSession();
76-
isLoading.value = false;
77-
78-
if (data?.checkout?.result === "success") {
79-
await cart.refetch();
80-
await navigateTo("/success");
81-
} else {
82-
alert("Error, order not placed. Please try again.");
83-
await cart.refetch();
84-
}
85-
});
86-
87-
onError(async (error) => {
88-
console.error("Checkout error:", error);
89-
isLoading.value = false;
90-
91-
if (error.message.includes("session")) {
92-
clearWooCommerceSession();
93-
alert("Your session has expired. Please refresh the page and try again.");
94-
} else {
95-
alert("An unexpected error occurred. Please try again.");
96-
}
97-
98-
await cart.refetch();
99-
});
100-
10166
// Ensure cart is loaded on mount
10267
onMounted(async () => {
10368
await cart.refetch();
10469
});
10570
106-
// Watch for orderData changes and trigger mutation
107-
watch(orderData, (newOrderData) => {
108-
if (newOrderData) {
109-
isLoading.value = true;
110-
mutate();
111-
112-
// Refetch after a delay
113-
setTimeout(() => {
114-
cart.refetch();
115-
}, 2000);
116-
}
117-
}, { deep: true });
118-
11971
/**
12072
* Handles the submission of a checkout form with the provided user information.
12173
*/
122-
const handleSubmit = ({
74+
const handleSubmit = async ({
12375
firstName,
12476
lastName,
12577
address1,
@@ -156,6 +108,32 @@ const handleSubmit = ({
156108
transactionId: "hjkhjkhsdsdiui",
157109
};
158110
159-
orderData.value = checkoutData;
111+
try {
112+
const { mutate } = useMutation(CHECKOUT_MUTATION);
113+
114+
const result = await mutate({
115+
input: checkoutData,
116+
});
117+
118+
if (result?.data?.checkout?.result === "success") {
119+
clearWooCommerceSession();
120+
await cart.refetch();
121+
await navigateTo("/success");
122+
} else {
123+
alert("Error, order not placed. Please try again.");
124+
await cart.refetch();
125+
}
126+
} catch (error) {
127+
console.error("Checkout error:", error);
128+
129+
if (error.message.includes("session")) {
130+
clearWooCommerceSession();
131+
alert("Your session has expired. Please refresh the page and try again.");
132+
} else {
133+
alert("An unexpected error occurred. Please try again.");
134+
}
135+
136+
await cart.refetch();
137+
}
160138
};
161139
</script>

0 commit comments

Comments
 (0)