Skip to content

Commit c42ac31

Browse files
committed
Refactor cart mutations to use refetchQueries option
Updated addToCart and updateCart mutations to use the refetchQueries option in useMutation, ensuring the cart is refreshed after mutations. Removed manual calls to refetchCart as they are now handled automatically.
1 parent ba3cdaa commit c42ac31

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

store/useCart.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,15 @@ export const useCart = defineStore(
6262
};
6363

6464
const { mutate: addToCartMutation, loading: addToCartLoading } =
65-
useMutation(ADD_TO_CART_MUTATION);
65+
useMutation(ADD_TO_CART_MUTATION, {
66+
refetchQueries: [{ query: GET_CART_QUERY }],
67+
awaitRefetchQueries: true,
68+
});
6669
const { mutate: updateCartMutation, loading: updateCartLoading } =
67-
useMutation(UPDATE_CART_MUTATION);
70+
useMutation(UPDATE_CART_MUTATION, {
71+
refetchQueries: [{ query: GET_CART_QUERY }],
72+
awaitRefetchQueries: true,
73+
});
6874

6975
const addToCart = async (product, quantity = 1) => {
7076
try {
@@ -74,7 +80,6 @@ export const useCart = defineStore(
7480
quantity: quantity,
7581
},
7682
});
77-
await refetchCart();
7883
} catch (err) {
7984
console.error("Error adding to cart:", err);
8085
}
@@ -87,10 +92,8 @@ export const useCart = defineStore(
8792
items: Array.isArray(key) ? key : [{ key, quantity }],
8893
},
8994
});
90-
await refetchCart();
9195
} catch (err) {
9296
console.error("Error updating cart item quantity:", err);
93-
await refetchCart();
9497
}
9598
};
9699

0 commit comments

Comments
 (0)