From 35ce9c5992a887174d2f2cc319a9aea6dfabc52b Mon Sep 17 00:00:00 2001 From: Michal Zachardala Date: Fri, 15 Nov 2024 14:45:28 +0100 Subject: [PATCH] issue#1127 --- .../catalog/models/ProductThumbnail.ts | 1 + storefront/pages/cart/index.tsx | 32 +++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/storefront/modules/catalog/models/ProductThumbnail.ts b/storefront/modules/catalog/models/ProductThumbnail.ts index 3981e164ac..0aea8b8234 100644 --- a/storefront/modules/catalog/models/ProductThumbnail.ts +++ b/storefront/modules/catalog/models/ProductThumbnail.ts @@ -4,4 +4,5 @@ export type ProductThumbnail = { slug: string; thumbnailUrl: string; price: number; + stock: number; }; diff --git a/storefront/pages/cart/index.tsx b/storefront/pages/cart/index.tsx index 013823e8ed..96f59a4c1c 100644 --- a/storefront/pages/cart/index.tsx +++ b/storefront/pages/cart/index.tsx @@ -30,8 +30,11 @@ const Cart = () => { slug: string; thumbnailUrl: string; price: number; + stock: number; }; + const LOW_STOCK_THRESHOLD = 5; + const [items, setItems] = useState([]); const [selectedProductIds, setSelectedProductIds] = useState>(new Set()); @@ -90,13 +93,16 @@ const Cart = () => { .then((results) => { const newItems: Item[] = []; results.forEach((result) => { + const quantity = cartDetails.find((detail) => detail.productId === result.id) + ?.quantity!; newItems.push({ productId: result.id, - quantity: cartDetails.find((detail) => detail.productId === result.id)?.quantity!, + quantity: quantity, productName: result.name, slug: result.slug, thumbnailUrl: result.thumbnailUrl, price: result.price, + stock: result.stock, }); }); setItems(newItems); @@ -292,16 +298,18 @@ const Cart = () => { {items.map((item) => { + const isOutOfStock = item.stock === 0; + const isLowStock = item.stock > 0 && item.stock <= LOW_STOCK_THRESHOLD; return ( @@ -329,6 +337,12 @@ const Cart = () => { >
{item.productName}
+ {isOutOfStock && ( +

Out of stock

+ )} + {isLowStock && ( +

Low stock: only {item.stock} left!

+ )} {formatPrice(item.price)} @@ -341,13 +355,13 @@ const Cart = () => { value="-" className="minus" onClick={() => handleMinus(item.productId, item.quantity)} + disabled={isOutOfStock} /> @@ -362,6 +376,7 @@ const Cart = () => { onKeyDown={(e) => handleQuantityKeyDown(item.productId, e.key)} title="Qty" className="input-text qty text" + disabled={isOutOfStock} /> { value="+" className="plus" onClick={() => handlePlus(item.productId, item.quantity)} + disabled={isOutOfStock} /> {calculateProductPrice(item)} - {' '} {' '} + );