Skip to content

Commit e228d1c

Browse files
authored
Merge pull request #1515 from w3bdesign/develop
Refactor cart fetching logic and improve error handling
2 parents fb51bdf + 9e847d4 commit e228d1c

File tree

2 files changed

+39
-44
lines changed

2 files changed

+39
-44
lines changed

plugins/cartUpdater.js

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

3-
export default defineNuxtPlugin((nuxtApp) => {
4-
nuxtApp.hook("app:created", () => {
5-
const cart = useCart();
6-
7-
cart.refetch();
8-
9-
// Refetch cart data on route change
10-
nuxtApp.$router.beforeEach((_to, _from, next) => {
11-
cart.refetch();
12-
next();
13-
});
14-
});
3+
export default defineNuxtPlugin(async (nuxtApp) => {
4+
const cart = useCart();
5+
await cart.fetchCart();
156
});

store/useCart.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineStore } from "pinia";
2-
import { useQuery, useMutation } from "@vue/apollo-composable";
3-
import { computed, ref, watch } from "vue";
2+
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";
@@ -14,21 +14,32 @@ export const useCart = defineStore(
1414
const error = ref(null);
1515
const cartTotals = ref({});
1616

17-
const { result: cartResult, refetch: refetchCart } = useQuery(
18-
GET_CART_QUERY,
19-
null,
20-
{
21-
fetchPolicy: "network-only",
22-
},
23-
);
17+
const { $apollo } = useNuxtApp();
2418

25-
watch(cartResult, (newCartResult) => {
26-
if (newCartResult && newCartResult.cart) {
27-
updateCartState(newCartResult.cart);
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+
});
28+
29+
if (data.value) {
30+
updateCartState(data.value);
31+
}
32+
} catch (e) {
33+
error.value = e;
2834
}
29-
});
35+
};
3036

3137
const updateCartState = (newCart) => {
38+
if (!newCart) {
39+
cart.value = [];
40+
cartTotals.value = {};
41+
return;
42+
}
3243
cart.value = newCart.contents.nodes.map((item) => ({
3344
key: item.key,
3445
product: item.product.node,
@@ -64,8 +75,9 @@ export const useCart = defineStore(
6475
quantity: quantity,
6576
},
6677
});
67-
await refetchCart();
78+
await fetchCart();
6879
} catch (err) {
80+
error.value = err;
6981
} finally {
7082
loading.value = false;
7183
}
@@ -81,41 +93,33 @@ export const useCart = defineStore(
8193
items: [{ key, quantity }],
8294
},
8395
});
84-
await refetchCart();
96+
await fetchCart();
8597
} catch (err) {
86-
await refetchCart();
98+
error.value = err;
99+
await fetchCart();
87100
} finally {
88101
loading.value = false;
89102
}
90103
};
91104

92105
const removeProductFromCart = async (key) => {
93-
loading.value = true;
94-
error.value = null;
95106
try {
96107
const isLastItem = cart.value.length === 1;
97108
await updateCartItemQuantity(key, 0);
98109
if (isLastItem) {
99110
await navigateTo("/");
100111
}
101112
} catch (err) {
102-
} finally {
103-
loading.value = false;
113+
error.value = err;
104114
}
105115
};
106116

107117
const clearCart = async () => {
108-
loading.value = true;
109-
error.value = null;
110-
try {
111-
for (const item of cart.value) {
112-
await removeProductFromCart(item.key);
113-
}
114-
} catch (err) {
115-
} finally {
116-
loading.value = false;
117-
await refetchCart();
118-
}
118+
const itemKeys = cart.value.map((item) => ({
119+
key: item.key,
120+
quantity: 0,
121+
}));
122+
await updateCartItemQuantity(itemKeys);
119123
};
120124

121125
const cartQuantity = computed(() => {
@@ -142,7 +146,7 @@ export const useCart = defineStore(
142146
cartQuantity,
143147
cartSubtotal,
144148
cartTotal,
145-
refetch: refetchCart,
149+
fetchCart,
146150
};
147151
},
148152
{

0 commit comments

Comments
 (0)