Skip to content

Commit 57b9649

Browse files
committed
Refactor cart store to use useAsyncQuery and improve mutations
Replaces manual cart fetching with useAsyncQuery for better reactivity and error handling. Refactors add, update, and clear cart actions to use dedicated mutation instances, consolidates loading state, and improves cart clearing logic for immediate UI updates. Removes unused variables and streamlines error handling.
1 parent 9e847d4 commit 57b9649

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

store/useCart.js

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineStore } from "pinia";
2+
import { computed, ref, watch } from "vue";
23
import { useMutation } from "@vue/apollo-composable";
3-
import { computed, ref } from "vue";
44

55
import ADD_TO_CART_MUTATION from "@/apollo/mutations/ADD_TO_CART_MUTATION.gql";
66
import UPDATE_CART_MUTATION from "@/apollo/mutations/UPDATE_CART_MUTATION.gql";
@@ -10,29 +10,26 @@ export const useCart = defineStore(
1010
"cartState",
1111
() => {
1212
const cart = ref([]);
13-
const loading = ref(false);
14-
const error = ref(null);
1513
const cartTotals = ref({});
1614

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);
2821

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);
3129
}
32-
} catch (e) {
33-
error.value = e;
34-
}
35-
};
30+
},
31+
{ immediate: true },
32+
);
3633

3734
const updateCartState = (newCart) => {
3835
if (!newCart) {
@@ -64,62 +61,61 @@ export const useCart = defineStore(
6461
};
6562
};
6663

64+
const { mutate: addToCartMutation, loading: addToCartLoading } =
65+
useMutation(ADD_TO_CART_MUTATION);
66+
const { mutate: updateCartMutation, loading: updateCartLoading } =
67+
useMutation(UPDATE_CART_MUTATION);
68+
6769
const addToCart = async (product, quantity = 1) => {
68-
loading.value = true;
69-
error.value = null;
7070
try {
71-
const { mutate } = useMutation(ADD_TO_CART_MUTATION);
72-
await mutate({
71+
await addToCartMutation({
7372
input: {
7473
productId: product.databaseId,
7574
quantity: quantity,
7675
},
7776
});
78-
await fetchCart();
77+
await refetchCart();
7978
} catch (err) {
80-
error.value = err;
81-
} finally {
82-
loading.value = false;
79+
console.error("Error adding to cart:", err);
8380
}
8481
};
8582

8683
const updateCartItemQuantity = async (key, quantity) => {
87-
loading.value = true;
88-
error.value = null;
8984
try {
90-
const { mutate } = useMutation(UPDATE_CART_MUTATION);
91-
await mutate({
85+
await updateCartMutation({
9286
input: {
93-
items: [{ key, quantity }],
87+
items: Array.isArray(key) ? key : [{ key, quantity }],
9488
},
9589
});
96-
await fetchCart();
90+
await refetchCart();
9791
} 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();
10294
}
10395
};
10496

10597
const removeProductFromCart = async (key) => {
10698
try {
107-
const isLastItem = cart.value.length === 1;
99+
const isLastItem =
100+
cart.value.length === 1 && cart.value[0].key === key;
108101
await updateCartItemQuantity(key, 0);
109102
if (isLastItem) {
103+
updateCartState(null); // Clear cart locally to update UI instantly
110104
await navigateTo("/");
111105
}
112106
} catch (err) {
113-
error.value = err;
107+
console.error("Error removing product from cart:", err);
114108
}
115109
};
116110

117111
const clearCart = async () => {
112+
if (!cart.value.length) return;
118113
const itemKeys = cart.value.map((item) => ({
119114
key: item.key,
120115
quantity: 0,
121116
}));
122117
await updateCartItemQuantity(itemKeys);
118+
updateCartState(null); // Clear cart locally
123119
};
124120

125121
const cartQuantity = computed(() => {
@@ -136,7 +132,9 @@ export const useCart = defineStore(
136132

137133
return {
138134
cart,
139-
loading,
135+
loading: computed(
136+
() => loading.value || addToCartLoading.value || updateCartLoading.value,
137+
),
140138
error,
141139
cartTotals,
142140
addToCart,
@@ -146,7 +144,7 @@ export const useCart = defineStore(
146144
cartQuantity,
147145
cartSubtotal,
148146
cartTotal,
149-
fetchCart,
147+
refetch: refetchCart,
150148
};
151149
},
152150
{

0 commit comments

Comments
 (0)