Skip to content

Commit f810b1d

Browse files
committed
.
1 parent f075d58 commit f810b1d

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed

components/Cart/CartContents.vue

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ const cartItems = computed(() => cart.cart);
3434
/**
3535
* Handles the removal of a product.
3636
*
37-
* @param {Object} product - The product to be removed.
37+
* @param {string} key - The key of the product to be removed.
3838
*/
39-
const handleRemoveProduct = async (product) => {
39+
const handleRemoveProduct = async (key) => {
4040
try {
41-
await cart.removeProductFromCart(product.key);
41+
await cart.removeProductFromCart(key);
4242
} catch (error) {
4343
console.error('Error removing product from cart:', error);
44-
// You might want to show an error message to the user here
44+
// Optionally, you can add a user-friendly notification here
45+
// without exposing the error details
4546
}
4647
};
4748
</script>

store/useCart.js

+7-33
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const useCart = defineStore("cartState", () => {
1111
const error = ref(null);
1212
const cartTotals = ref({});
1313

14-
const { result: cartResult, loading: cartLoading, refetch } = useQuery(GET_CART_QUERY, null, { fetchPolicy: 'network-only' });
14+
const { result: cartResult, loading: cartLoading, refetch: refetchCart } = useQuery(GET_CART_QUERY, null, { fetchPolicy: 'network-only' });
1515

1616
watch(cartResult, (newCartResult) => {
1717
if (newCartResult && newCartResult.cart) {
@@ -48,29 +48,16 @@ export const useCart = defineStore("cartState", () => {
4848
loading.value = true;
4949
error.value = null;
5050
try {
51-
// Optimistically update the local state
52-
const newItem = {
53-
key: Date.now().toString(), // Temporary key
54-
product: product,
55-
quantity: quantity,
56-
total: (parseFloat(product.price) * quantity).toString(),
57-
subtotal: (parseFloat(product.price) * quantity).toString(),
58-
};
59-
cart.value.push(newItem);
60-
6151
const { mutate } = useMutation(ADD_TO_CART_MUTATION);
6252
await mutate({
6353
input: {
6454
productId: product.databaseId,
6555
quantity: quantity,
6656
},
6757
});
68-
await refetch();
58+
await refetchCart();
6959
} catch (err) {
7060
console.error("Error adding to cart:", err);
71-
error.value = err.message || "An error occurred while adding to cart.";
72-
// Revert the optimistic update
73-
cart.value = cart.value.filter(item => item.key !== Date.now().toString());
7461
} finally {
7562
loading.value = false;
7663
}
@@ -80,24 +67,16 @@ export const useCart = defineStore("cartState", () => {
8067
loading.value = true;
8168
error.value = null;
8269
try {
83-
// Optimistically update the local state
84-
const itemIndex = cart.value.findIndex(item => item.key === key);
85-
if (itemIndex !== -1) {
86-
cart.value[itemIndex].quantity = quantity;
87-
}
88-
8970
const { mutate } = useMutation(UPDATE_CART_MUTATION);
9071
await mutate({
9172
input: {
9273
items: [{ key, quantity }],
9374
},
9475
});
95-
await refetch();
76+
await refetchCart();
9677
} catch (err) {
9778
console.error("Error updating cart:", err);
98-
error.value = err.message || "An error occurred while updating the cart.";
99-
// Revert the optimistic update
100-
await refetch();
79+
await refetchCart();
10180
} finally {
10281
loading.value = false;
10382
}
@@ -107,17 +86,12 @@ export const useCart = defineStore("cartState", () => {
10786
loading.value = true;
10887
error.value = null;
10988
try {
110-
// Optimistically update the local state
111-
cart.value = cart.value.filter(item => item.key !== key);
112-
11389
await updateCartItemQuantity(key, 0);
11490
} catch (err) {
11591
console.error("Error removing product from cart:", err);
116-
error.value = err.message || "An error occurred while removing the product from the cart.";
117-
// Revert the optimistic update
118-
await refetch();
11992
} finally {
12093
loading.value = false;
94+
await refetchCart();
12195
}
12296
};
12397

@@ -130,9 +104,9 @@ export const useCart = defineStore("cartState", () => {
130104
}
131105
} catch (err) {
132106
console.error("Error clearing cart:", err);
133-
error.value = err.message || "An error occurred while clearing the cart.";
134107
} finally {
135108
loading.value = false;
109+
await refetchCart();
136110
}
137111
};
138112

@@ -160,6 +134,6 @@ export const useCart = defineStore("cartState", () => {
160134
cartQuantity,
161135
cartSubtotal,
162136
cartTotal,
163-
refetch,
137+
refetch: refetchCart,
164138
};
165139
});

0 commit comments

Comments
 (0)