Skip to content

Commit e7dc217

Browse files
committed
Update CheckoutForm.vue
1 parent 0dd438d commit e7dc217

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

components/Checkout/CheckoutForm.vue

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
<script setup>
3737
import { Form, Field, ErrorMessage } from "vee-validate";
3838
import { uid } from "uid";
39+
import { ref, onMounted } from "vue";
40+
import { useCart } from "@/store/useCart";
3941
4042
import { BILLING_FIELDS, BILLING_SCHEMA } from "./constants/BILLING_FIELDS";
4143
@@ -51,6 +53,68 @@ const upperCaseFirstChar = (input) =>
5153
input.charAt(0).toUpperCase() + input.slice(1);
5254
5355
const paymentMethod = "cod";
56+
const cart = useCart();
57+
const orderData = ref(null);
58+
const isLoading = ref(false);
59+
60+
// Clear WooCommerce session
61+
const clearWooCommerceSession = () => {
62+
if (process.client) {
63+
localStorage.removeItem("woo-session");
64+
localStorage.removeItem("woocommerce-cart");
65+
}
66+
};
67+
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+
101+
// Ensure cart is loaded on mount
102+
onMounted(async () => {
103+
await cart.refetch();
104+
});
105+
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 });
54118
55119
/**
56120
* Handles the submission of a checkout form with the provided user information.
@@ -92,28 +156,6 @@ const handleSubmit = ({
92156
transactionId: "hjkhjkhsdsdiui",
93157
};
94158
95-
const { mutate, onDone, onError } = useMutation(CHECKOUT_MUTATION);
96-
97-
mutate({ input: checkoutData });
98-
99-
onDone(async (result) => {
100-
const { data } = result;
101-
if (data?.checkout?.result === "success") {
102-
await navigateTo("/success");
103-
} else {
104-
alert("Error, order not placed. Please try again.");
105-
}
106-
});
107-
108-
onError((error) => {
109-
console.error("Checkout error:", error);
110-
if (error.message.includes("session")) {
111-
alert(
112-
"Your session has expired. Please refresh the page and try again.",
113-
);
114-
} else {
115-
alert("An unexpected error occurred. Please try again.");
116-
}
117-
});
159+
orderData.value = checkoutData;
118160
};
119161
</script>

0 commit comments

Comments
 (0)