@@ -11,7 +11,7 @@ export const useCart = defineStore("cartState", () => {
11
11
const error = ref ( null ) ;
12
12
const cartTotals = ref ( { } ) ;
13
13
14
- const { result : cartResult , loading : cartLoading , refetch } = useQuery ( GET_CART_QUERY , null , { fetchPolicy : 'network-only' } ) ;
14
+ const { result : cartResult , loading : cartLoading , refetch : refetchCart } = useQuery ( GET_CART_QUERY , null , { fetchPolicy : 'network-only' } ) ;
15
15
16
16
watch ( cartResult , ( newCartResult ) => {
17
17
if ( newCartResult && newCartResult . cart ) {
@@ -48,29 +48,16 @@ export const useCart = defineStore("cartState", () => {
48
48
loading . value = true ;
49
49
error . value = null ;
50
50
try {
51
- // Optimistically update the local state
52
- const newItem = {
53
- key : Date . now ( ) . toString ( ) , // Temporary key
54
- product : product ,
55
- quantity : quantity ,
56
- total : ( parseFloat ( product . price ) * quantity ) . toString ( ) ,
57
- subtotal : ( parseFloat ( product . price ) * quantity ) . toString ( ) ,
58
- } ;
59
- cart . value . push ( newItem ) ;
60
-
61
51
const { mutate } = useMutation ( ADD_TO_CART_MUTATION ) ;
62
52
await mutate ( {
63
53
input : {
64
54
productId : product . databaseId ,
65
55
quantity : quantity ,
66
56
} ,
67
57
} ) ;
68
- await refetch ( ) ;
58
+ await refetchCart ( ) ;
69
59
} catch ( err ) {
70
60
console . error ( "Error adding to cart:" , err ) ;
71
- error . value = err . message || "An error occurred while adding to cart." ;
72
- // Revert the optimistic update
73
- cart . value = cart . value . filter ( item => item . key !== Date . now ( ) . toString ( ) ) ;
74
61
} finally {
75
62
loading . value = false ;
76
63
}
@@ -80,24 +67,16 @@ export const useCart = defineStore("cartState", () => {
80
67
loading . value = true ;
81
68
error . value = null ;
82
69
try {
83
- // Optimistically update the local state
84
- const itemIndex = cart . value . findIndex ( item => item . key === key ) ;
85
- if ( itemIndex !== - 1 ) {
86
- cart . value [ itemIndex ] . quantity = quantity ;
87
- }
88
-
89
70
const { mutate } = useMutation ( UPDATE_CART_MUTATION ) ;
90
71
await mutate ( {
91
72
input : {
92
73
items : [ { key, quantity } ] ,
93
74
} ,
94
75
} ) ;
95
- await refetch ( ) ;
76
+ await refetchCart ( ) ;
96
77
} catch ( err ) {
97
78
console . error ( "Error updating cart:" , err ) ;
98
- error . value = err . message || "An error occurred while updating the cart." ;
99
- // Revert the optimistic update
100
- await refetch ( ) ;
79
+ await refetchCart ( ) ;
101
80
} finally {
102
81
loading . value = false ;
103
82
}
@@ -107,17 +86,12 @@ export const useCart = defineStore("cartState", () => {
107
86
loading . value = true ;
108
87
error . value = null ;
109
88
try {
110
- // Optimistically update the local state
111
- cart . value = cart . value . filter ( item => item . key !== key ) ;
112
-
113
89
await updateCartItemQuantity ( key , 0 ) ;
114
90
} catch ( err ) {
115
91
console . error ( "Error removing product from cart:" , err ) ;
116
- error . value = err . message || "An error occurred while removing the product from the cart." ;
117
- // Revert the optimistic update
118
- await refetch ( ) ;
119
92
} finally {
120
93
loading . value = false ;
94
+ await refetchCart ( ) ;
121
95
}
122
96
} ;
123
97
@@ -130,9 +104,9 @@ export const useCart = defineStore("cartState", () => {
130
104
}
131
105
} catch ( err ) {
132
106
console . error ( "Error clearing cart:" , err ) ;
133
- error . value = err . message || "An error occurred while clearing the cart." ;
134
107
} finally {
135
108
loading . value = false ;
109
+ await refetchCart ( ) ;
136
110
}
137
111
} ;
138
112
@@ -160,6 +134,6 @@ export const useCart = defineStore("cartState", () => {
160
134
cartQuantity,
161
135
cartSubtotal,
162
136
cartTotal,
163
- refetch,
137
+ refetch : refetchCart ,
164
138
} ;
165
139
} ) ;
0 commit comments