@@ -11,7 +11,7 @@ export const useCart = defineStore("cartState", () => {
1111 const error = ref ( null ) ;
1212 const cartTotals = ref ( { } ) ;
1313
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' } ) ;
1515
1616 watch ( cartResult , ( newCartResult ) => {
1717 if ( newCartResult && newCartResult . cart ) {
@@ -48,29 +48,16 @@ export const useCart = defineStore("cartState", () => {
4848 loading . value = true ;
4949 error . value = null ;
5050 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-
6151 const { mutate } = useMutation ( ADD_TO_CART_MUTATION ) ;
6252 await mutate ( {
6353 input : {
6454 productId : product . databaseId ,
6555 quantity : quantity ,
6656 } ,
6757 } ) ;
68- await refetch ( ) ;
58+ await refetchCart ( ) ;
6959 } catch ( err ) {
7060 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 ( ) ) ;
7461 } finally {
7562 loading . value = false ;
7663 }
@@ -80,24 +67,16 @@ export const useCart = defineStore("cartState", () => {
8067 loading . value = true ;
8168 error . value = null ;
8269 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-
8970 const { mutate } = useMutation ( UPDATE_CART_MUTATION ) ;
9071 await mutate ( {
9172 input : {
9273 items : [ { key, quantity } ] ,
9374 } ,
9475 } ) ;
95- await refetch ( ) ;
76+ await refetchCart ( ) ;
9677 } catch ( err ) {
9778 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 ( ) ;
10180 } finally {
10281 loading . value = false ;
10382 }
@@ -107,17 +86,12 @@ export const useCart = defineStore("cartState", () => {
10786 loading . value = true ;
10887 error . value = null ;
10988 try {
110- // Optimistically update the local state
111- cart . value = cart . value . filter ( item => item . key !== key ) ;
112-
11389 await updateCartItemQuantity ( key , 0 ) ;
11490 } catch ( err ) {
11591 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 ( ) ;
11992 } finally {
12093 loading . value = false ;
94+ await refetchCart ( ) ;
12195 }
12296 } ;
12397
@@ -130,9 +104,9 @@ export const useCart = defineStore("cartState", () => {
130104 }
131105 } catch ( err ) {
132106 console . error ( "Error clearing cart:" , err ) ;
133- error . value = err . message || "An error occurred while clearing the cart." ;
134107 } finally {
135108 loading . value = false ;
109+ await refetchCart ( ) ;
136110 }
137111 } ;
138112
@@ -160,6 +134,6 @@ export const useCart = defineStore("cartState", () => {
160134 cartQuantity,
161135 cartSubtotal,
162136 cartTotal,
163- refetch,
137+ refetch : refetchCart ,
164138 } ;
165139} ) ;
0 commit comments