Skip to content

Commit c737efe

Browse files
authored
Merge pull request #1516 from w3bdesign/develop
Refactor cart store to use useAsyncQuery and improve mutations
2 parents e228d1c + ba3cdaa commit c737efe

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

plugins/cartUpdater.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useCart } from "@/store/useCart";
22

3-
export default defineNuxtPlugin(async (nuxtApp) => {
4-
const cart = useCart();
5-
await cart.fetchCart();
3+
export default defineNuxtPlugin(() => {
4+
useCart();
65
});

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,60 @@ 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 = cart.value.length === 1 && cart.value[0].key === key;
108100
await updateCartItemQuantity(key, 0);
109101
if (isLastItem) {
102+
updateCartState(null); // Clear cart locally to update UI instantly
110103
await navigateTo("/");
111104
}
112105
} catch (err) {
113-
error.value = err;
106+
console.error("Error removing product from cart:", err);
114107
}
115108
};
116109

117110
const clearCart = async () => {
111+
if (!cart.value.length) return;
118112
const itemKeys = cart.value.map((item) => ({
119113
key: item.key,
120114
quantity: 0,
121115
}));
122116
await updateCartItemQuantity(itemKeys);
117+
updateCartState(null); // Clear cart locally
123118
};
124119

125120
const cartQuantity = computed(() => {
@@ -136,7 +131,10 @@ export const useCart = defineStore(
136131

137132
return {
138133
cart,
139-
loading,
134+
loading: computed(
135+
() =>
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)