From 2b2b897cefbc5761fff2c7b30793be376f7f45dc Mon Sep 17 00:00:00 2001 From: Lee Robinson Date: Sun, 28 Jul 2024 14:49:48 -0500 Subject: [PATCH] Handle null ids --- components/cart/actions.ts | 6 +- components/cart/cart-context.tsx | 4 +- components/cart/modal.tsx | 144 ++++++++++++++++--------------- lib/shopify/types.ts | 4 +- 4 files changed, 81 insertions(+), 77 deletions(-) diff --git a/components/cart/actions.ts b/components/cart/actions.ts index 03b8a459a..a2c592557 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -37,7 +37,7 @@ export async function removeItem(prevState: any, merchandiseId: string) { const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId); - if (lineItem) { + if (lineItem && lineItem.id) { await removeFromCart(cartId, [lineItem.id]); revalidateTag(TAGS.cart); } else { @@ -72,7 +72,7 @@ export async function updateItemQuantity( const lineItem = cart.lines.find((line) => line.merchandise.id === merchandiseId); - if (lineItem) { + if (lineItem && lineItem.id) { if (quantity === 0) { await removeFromCart(cartId, [lineItem.id]); } else { @@ -114,5 +114,5 @@ export async function redirectToCheckout() { export async function createCartAndSetCookie() { let cart = await createCart(); - cookies().set('cartId', cart.id); + cookies().set('cartId', cart.id!); } diff --git a/components/cart/cart-context.tsx b/components/cart/cart-context.tsx index d994e793a..2d8ddd42f 100644 --- a/components/cart/cart-context.tsx +++ b/components/cart/cart-context.tsx @@ -52,7 +52,7 @@ function createOrUpdateCartItem( const totalAmount = calculateItemCost(quantity, variant.price.amount); return { - id: existingItem?.id || `${variant.id}_${Date.now()}`, + id: existingItem?.id, quantity, cost: { totalAmount: { @@ -91,7 +91,7 @@ function updateCartTotals(lines: CartItem[]): Pick
diff --git a/lib/shopify/types.ts b/lib/shopify/types.ts index 789c6d53e..9b443559e 100644 --- a/lib/shopify/types.ts +++ b/lib/shopify/types.ts @@ -20,7 +20,7 @@ export type CartProduct = { }; export type CartItem = { - id: string; + id: string | undefined; quantity: number; cost: { totalAmount: Money; @@ -96,7 +96,7 @@ export type SEO = { }; export type ShopifyCart = { - id: string; + id: string | undefined; checkoutUrl: string; cost: { subtotalAmount: Money;