1
1
import { defineStore } from "pinia" ;
2
2
import { computed , ref , watch } from "vue" ;
3
- import { useMutation } from "@vue/apollo-composable" ;
3
+ import { useQuery , useMutation } from "@vue/apollo-composable" ;
4
4
5
5
import ADD_TO_CART_MUTATION from "@/apollo/mutations/ADD_TO_CART_MUTATION.gql" ;
6
6
import UPDATE_CART_MUTATION from "@/apollo/mutations/UPDATE_CART_MUTATION.gql" ;
@@ -13,11 +13,18 @@ export const useCart = defineStore(
13
13
const cartTotals = ref ( { } ) ;
14
14
15
15
const {
16
- data : cartData ,
17
- pending : loading ,
16
+ result : cartData ,
17
+ loading,
18
18
error,
19
- refresh : refetchCart ,
20
- } = useAsyncQuery ( GET_CART_QUERY ) ;
19
+ refetch : refetchCart ,
20
+ } = useQuery (
21
+ GET_CART_QUERY ,
22
+ { } ,
23
+ {
24
+ fetchPolicy : "cache-and-network" ,
25
+ notifyOnNetworkStatusChange : true ,
26
+ } ,
27
+ ) ;
21
28
22
29
watch (
23
30
cartData ,
@@ -62,15 +69,9 @@ export const useCart = defineStore(
62
69
} ;
63
70
64
71
const { mutate : addToCartMutation , loading : addToCartLoading } =
65
- useMutation ( ADD_TO_CART_MUTATION , {
66
- refetchQueries : [ { query : GET_CART_QUERY } ] ,
67
- awaitRefetchQueries : true ,
68
- } ) ;
72
+ useMutation ( ADD_TO_CART_MUTATION ) ;
69
73
const { mutate : updateCartMutation , loading : updateCartLoading } =
70
- useMutation ( UPDATE_CART_MUTATION , {
71
- refetchQueries : [ { query : GET_CART_QUERY } ] ,
72
- awaitRefetchQueries : true ,
73
- } ) ;
74
+ useMutation ( UPDATE_CART_MUTATION ) ;
74
75
75
76
const addToCart = async ( product , quantity = 1 ) => {
76
77
try {
@@ -80,8 +81,12 @@ export const useCart = defineStore(
80
81
quantity : quantity ,
81
82
} ,
82
83
} ) ;
84
+ // Force refetch to ensure cache is updated
85
+ await refetchCart ( ) ;
83
86
} catch ( err ) {
84
87
console . error ( "Error adding to cart:" , err ) ;
88
+ // Still refetch on error to ensure consistency
89
+ await refetchCart ( ) ;
85
90
}
86
91
} ;
87
92
@@ -92,8 +97,12 @@ export const useCart = defineStore(
92
97
items : Array . isArray ( key ) ? key : [ { key, quantity } ] ,
93
98
} ,
94
99
} ) ;
100
+ // Force refetch to ensure cache is updated
101
+ await refetchCart ( ) ;
95
102
} catch ( err ) {
96
103
console . error ( "Error updating cart item quantity:" , err ) ;
104
+ // Still refetch on error to ensure consistency
105
+ await refetchCart ( ) ;
97
106
}
98
107
} ;
99
108
0 commit comments