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" ;
39
+ import { onMounted } from " vue" ;
40
40
import { useCart } from " @/store/useCart" ;
41
41
42
42
import { BILLING_FIELDS , BILLING_SCHEMA } from " ./constants/BILLING_FIELDS" ;
@@ -54,8 +54,6 @@ const upperCaseFirstChar = (input) =>
54
54
55
55
const paymentMethod = " cod" ;
56
56
const cart = useCart ();
57
- const orderData = ref (null );
58
- const isLoading = ref (false );
59
57
60
58
// Clear WooCommerce session
61
59
const clearWooCommerceSession = () => {
@@ -65,61 +63,15 @@ const clearWooCommerceSession = () => {
65
63
}
66
64
};
67
65
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
66
// Ensure cart is loaded on mount
102
67
onMounted (async () => {
103
68
await cart .refetch ();
104
69
});
105
70
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
-
119
71
/**
120
72
* Handles the submission of a checkout form with the provided user information.
121
73
*/
122
- const handleSubmit = ({
74
+ const handleSubmit = async ({
123
75
firstName,
124
76
lastName,
125
77
address1,
@@ -156,6 +108,32 @@ const handleSubmit = ({
156
108
transactionId: " hjkhjkhsdsdiui" ,
157
109
};
158
110
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
+ }
160
138
};
161
139
< / script>
0 commit comments