1
1
import { defineStore } from "pinia" ;
2
+ import { computed , ref , watch } from "vue" ;
2
3
import { useMutation } from "@vue/apollo-composable" ;
3
- import { computed , ref } from "vue" ;
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" ;
@@ -10,29 +10,26 @@ export const useCart = defineStore(
10
10
"cartState" ,
11
11
( ) => {
12
12
const cart = ref ( [ ] ) ;
13
- const loading = ref ( false ) ;
14
- const error = ref ( null ) ;
15
13
const cartTotals = ref ( { } ) ;
16
14
17
- const { $apollo } = useNuxtApp ( ) ;
18
-
19
- const fetchCart = async ( ) => {
20
- try {
21
- const { data } = await useAsyncData ( "cart" , async ( ) => {
22
- const { data } = await $apollo . client . query ( {
23
- query : GET_CART_QUERY ,
24
- fetchPolicy : "network-only" ,
25
- } ) ;
26
- return data . cart ;
27
- } ) ;
15
+ const {
16
+ data : cartData ,
17
+ pending : loading ,
18
+ error,
19
+ refresh : refetchCart ,
20
+ } = useAsyncQuery ( GET_CART_QUERY ) ;
28
21
29
- if ( data . value ) {
30
- updateCartState ( data . value ) ;
22
+ watch (
23
+ cartData ,
24
+ ( newCartData ) => {
25
+ if ( newCartData && newCartData . cart ) {
26
+ updateCartState ( newCartData . cart ) ;
27
+ } else if ( newCartData && newCartData . cart === null ) {
28
+ updateCartState ( null ) ;
31
29
}
32
- } catch ( e ) {
33
- error . value = e ;
34
- }
35
- } ;
30
+ } ,
31
+ { immediate : true } ,
32
+ ) ;
36
33
37
34
const updateCartState = ( newCart ) => {
38
35
if ( ! newCart ) {
@@ -64,62 +61,60 @@ export const useCart = defineStore(
64
61
} ;
65
62
} ;
66
63
64
+ const { mutate : addToCartMutation , loading : addToCartLoading } =
65
+ useMutation ( ADD_TO_CART_MUTATION ) ;
66
+ const { mutate : updateCartMutation , loading : updateCartLoading } =
67
+ useMutation ( UPDATE_CART_MUTATION ) ;
68
+
67
69
const addToCart = async ( product , quantity = 1 ) => {
68
- loading . value = true ;
69
- error . value = null ;
70
70
try {
71
- const { mutate } = useMutation ( ADD_TO_CART_MUTATION ) ;
72
- await mutate ( {
71
+ await addToCartMutation ( {
73
72
input : {
74
73
productId : product . databaseId ,
75
74
quantity : quantity ,
76
75
} ,
77
76
} ) ;
78
- await fetchCart ( ) ;
77
+ await refetchCart ( ) ;
79
78
} catch ( err ) {
80
- error . value = err ;
81
- } finally {
82
- loading . value = false ;
79
+ console . error ( "Error adding to cart:" , err ) ;
83
80
}
84
81
} ;
85
82
86
83
const updateCartItemQuantity = async ( key , quantity ) => {
87
- loading . value = true ;
88
- error . value = null ;
89
84
try {
90
- const { mutate } = useMutation ( UPDATE_CART_MUTATION ) ;
91
- await mutate ( {
85
+ await updateCartMutation ( {
92
86
input : {
93
- items : [ { key, quantity } ] ,
87
+ items : Array . isArray ( key ) ? key : [ { key, quantity } ] ,
94
88
} ,
95
89
} ) ;
96
- await fetchCart ( ) ;
90
+ await refetchCart ( ) ;
97
91
} catch ( err ) {
98
- error . value = err ;
99
- await fetchCart ( ) ;
100
- } finally {
101
- loading . value = false ;
92
+ console . error ( "Error updating cart item quantity:" , err ) ;
93
+ await refetchCart ( ) ;
102
94
}
103
95
} ;
104
96
105
97
const removeProductFromCart = async ( key ) => {
106
98
try {
107
- const isLastItem = cart . value . length === 1 ;
99
+ const isLastItem = cart . value . length === 1 && cart . value [ 0 ] . key === key ;
108
100
await updateCartItemQuantity ( key , 0 ) ;
109
101
if ( isLastItem ) {
102
+ updateCartState ( null ) ; // Clear cart locally to update UI instantly
110
103
await navigateTo ( "/" ) ;
111
104
}
112
105
} catch ( err ) {
113
- error . value = err ;
106
+ console . error ( "Error removing product from cart:" , err ) ;
114
107
}
115
108
} ;
116
109
117
110
const clearCart = async ( ) => {
111
+ if ( ! cart . value . length ) return ;
118
112
const itemKeys = cart . value . map ( ( item ) => ( {
119
113
key : item . key ,
120
114
quantity : 0 ,
121
115
} ) ) ;
122
116
await updateCartItemQuantity ( itemKeys ) ;
117
+ updateCartState ( null ) ; // Clear cart locally
123
118
} ;
124
119
125
120
const cartQuantity = computed ( ( ) => {
@@ -136,7 +131,10 @@ export const useCart = defineStore(
136
131
137
132
return {
138
133
cart,
139
- loading,
134
+ loading : computed (
135
+ ( ) =>
136
+ loading . value || addToCartLoading . value || updateCartLoading . value ,
137
+ ) ,
140
138
error,
141
139
cartTotals,
142
140
addToCart,
@@ -146,7 +144,7 @@ export const useCart = defineStore(
146
144
cartQuantity,
147
145
cartSubtotal,
148
146
cartTotal,
149
- fetchCart ,
147
+ refetch : refetchCart ,
150
148
} ;
151
149
} ,
152
150
{
0 commit comments