Skip to content

Commit 1c01f9b

Browse files
committed
¨¨
1 parent 85bcadd commit 1c01f9b

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

src/components/Product/AddToCart.component.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,8 @@ const AddToCart = ({
125125

126126
const handleAddToCart = async () => {
127127
try {
128-
// First attempt the server update
128+
// Let the mutation handle the cart update through refetchQueries
129129
await addToCart();
130-
131-
// Only update local state if server update succeeds
132-
if (cart) {
133-
setCart({
134-
...cart,
135-
totalProductsCount: cart.totalProductsCount + 1,
136-
products: [...cart.products]
137-
});
138-
} else {
139-
setCart({
140-
products: [],
141-
totalProductsCount: 1,
142-
totalProductsPrice: 0
143-
});
144-
}
145130
} catch (error) {
146131
console.error('Failed to add to cart:', error);
147132
setRequestError(true);

src/utils/functions/functions.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,15 @@ export const getFormattedCart = (data: IFormattedCartProps) => {
154154
return;
155155
}
156156

157+
// Add console logs to debug the data
158+
console.log('Raw cart data:', data);
159+
console.log('Given products:', givenProducts);
160+
157161
// Map products to the correct format
158162
formattedCart.products = givenProducts.map((item) => {
163+
console.log('Processing item:', item);
159164
const givenProduct = item.product.node;
165+
console.log('Product node:', givenProduct);
160166

161167
// Convert price to a float value
162168
const convertedCurrency = item.total.replace(/[^0-9.-]+/g, '');
@@ -165,14 +171,14 @@ export const getFormattedCart = (data: IFormattedCartProps) => {
165171
totalProductsCount += item.quantity;
166172

167173
// Create a new product object for each item
168-
return {
169-
productId: givenProduct.productId,
174+
const formattedProduct = {
175+
productId: givenProduct.databaseId, // Changed from productId to databaseId
170176
cartKey: item.key,
171177
name: givenProduct.name,
172178
qty: item.quantity,
173179
price: Number(convertedCurrency) / item.quantity,
174180
totalPrice: item.total,
175-
image: givenProduct.image.sourceUrl
181+
image: givenProduct.image?.sourceUrl
176182
? {
177183
sourceUrl: givenProduct.image.sourceUrl,
178184
srcSet: givenProduct.image.srcSet,
@@ -182,9 +188,13 @@ export const getFormattedCart = (data: IFormattedCartProps) => {
182188
sourceUrl: process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL,
183189
srcSet: process.env.NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL,
184190
title: givenProduct.name,
185-
},
191+
}
186192
};
193+
console.log('Formatted product:', formattedProduct);
194+
return formattedProduct;
187195
});
196+
197+
console.log('Final formatted cart:', formattedCart);
188198
formattedCart.totalProductsCount = totalProductsCount;
189199
formattedCart.totalProductsPrice = data.cart.total;
190200

0 commit comments

Comments
 (0)