36
36
<script setup>
37
37
import { Form , Field , ErrorMessage } from " vee-validate" ;
38
38
import { uid } from " uid" ;
39
+ import { ref , onMounted } from " vue" ;
40
+ import { useCart } from " @/store/useCart" ;
39
41
40
42
import { BILLING_FIELDS , BILLING_SCHEMA } from " ./constants/BILLING_FIELDS" ;
41
43
@@ -51,6 +53,68 @@ const upperCaseFirstChar = (input) =>
51
53
input .charAt (0 ).toUpperCase () + input .slice (1 );
52
54
53
55
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 });
54
118
55
119
/**
56
120
* Handles the submission of a checkout form with the provided user information.
@@ -92,28 +156,6 @@ const handleSubmit = ({
92
156
transactionId: " hjkhjkhsdsdiui" ,
93
157
};
94
158
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;
118
160
};
119
161
< / script>
0 commit comments