Skip to content

Refactor cart fetching logic and improve error handling #1515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions plugins/cartUpdater.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { useCart } from "@/store/useCart";

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook("app:created", () => {
const cart = useCart();

cart.refetch();

// Refetch cart data on route change
nuxtApp.$router.beforeEach((_to, _from, next) => {
cart.refetch();
next();
});
});
export default defineNuxtPlugin(async (nuxtApp) => {
const cart = useCart();
await cart.fetchCart();
});
68 changes: 36 additions & 32 deletions store/useCart.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineStore } from "pinia";
import { useQuery, useMutation } from "@vue/apollo-composable";
import { computed, ref, watch } from "vue";
import { useMutation } from "@vue/apollo-composable";
import { computed, ref } from "vue";

import ADD_TO_CART_MUTATION from "@/apollo/mutations/ADD_TO_CART_MUTATION.gql";
import UPDATE_CART_MUTATION from "@/apollo/mutations/UPDATE_CART_MUTATION.gql";
Expand All @@ -14,21 +14,32 @@ export const useCart = defineStore(
const error = ref(null);
const cartTotals = ref({});

const { result: cartResult, refetch: refetchCart } = useQuery(
GET_CART_QUERY,
null,
{
fetchPolicy: "network-only",
},
);
const { $apollo } = useNuxtApp();

watch(cartResult, (newCartResult) => {
if (newCartResult && newCartResult.cart) {
updateCartState(newCartResult.cart);
const fetchCart = async () => {
try {
const { data } = await useAsyncData("cart", async () => {
const { data } = await $apollo.client.query({
query: GET_CART_QUERY,
fetchPolicy: "network-only",
});
return data.cart;
});

if (data.value) {
updateCartState(data.value);
}
} catch (e) {
error.value = e;
}
});
};

const updateCartState = (newCart) => {
if (!newCart) {
cart.value = [];
cartTotals.value = {};
return;
}
cart.value = newCart.contents.nodes.map((item) => ({
key: item.key,
product: item.product.node,
Expand Down Expand Up @@ -64,8 +75,9 @@ export const useCart = defineStore(
quantity: quantity,
},
});
await refetchCart();
await fetchCart();
} catch (err) {
error.value = err;
} finally {
loading.value = false;
}
Expand All @@ -81,41 +93,33 @@ export const useCart = defineStore(
items: [{ key, quantity }],
},
});
await refetchCart();
await fetchCart();
} catch (err) {
await refetchCart();
error.value = err;
await fetchCart();
} finally {
loading.value = false;
}
};

const removeProductFromCart = async (key) => {
loading.value = true;
error.value = null;
try {
const isLastItem = cart.value.length === 1;
await updateCartItemQuantity(key, 0);
if (isLastItem) {
await navigateTo("/");
}
} catch (err) {
} finally {
loading.value = false;
error.value = err;
}
};

const clearCart = async () => {
loading.value = true;
error.value = null;
try {
for (const item of cart.value) {
await removeProductFromCart(item.key);
}
} catch (err) {
} finally {
loading.value = false;
await refetchCart();
}
const itemKeys = cart.value.map((item) => ({
key: item.key,
quantity: 0,
}));
await updateCartItemQuantity(itemKeys);
};

const cartQuantity = computed(() => {
Expand All @@ -142,7 +146,7 @@ export const useCart = defineStore(
cartQuantity,
cartSubtotal,
cartTotal,
refetch: refetchCart,
fetchCart,
};
},
{
Expand Down