Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue#1127 #1273

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions storefront/modules/catalog/models/ProductThumbnail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export type ProductThumbnail = {
slug: string;
thumbnailUrl: string;
price: number;
stock: number;
};
32 changes: 23 additions & 9 deletions storefront/pages/cart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ const Cart = () => {
slug: string;
thumbnailUrl: string;
price: number;
stock: number;
};

const LOW_STOCK_THRESHOLD = 5;

const [items, setItems] = useState<Item[]>([]);

const [selectedProductIds, setSelectedProductIds] = useState<Set<number>>(new Set());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -292,16 +298,18 @@ const Cart = () => {
</thead>
<tbody>
{items.map((item) => {
const isOutOfStock = item.stock === 0;
const isLowStock = item.stock > 0 && item.stock <= LOW_STOCK_THRESHOLD;
return (
<tr key={item.quantity.toString() + item.productId.toString()}>
<td>
<label className="item-checkbox-label" htmlFor="select-item-checkbox">
{''}
<input
className="form-check-input item-checkbox"
type="checkbox"
checked={selectedProductIds.has(item.productId)}
onChange={() => handleSelectItemChange(item.productId)}
disabled={isOutOfStock}
/>
</label>
</td>
Expand Down Expand Up @@ -329,6 +337,12 @@ const Cart = () => {
>
<h6 className="product-link">{item.productName}</h6>
</Link>
{isOutOfStock && (
<p className="text-danger">Out of stock</p>
)}
{isLowStock && (
<p className="text-warning">Low stock: only {item.stock} left!</p>
)}
</div>
</td>
<td className="cart__price">{formatPrice(item.price)}</td>
Expand All @@ -341,13 +355,13 @@ const Cart = () => {
value="-"
className="minus"
onClick={() => handleMinus(item.productId, item.quantity)}
disabled={isOutOfStock}
/>
<input
id="quanity"
id="quantity"
type="number"
step="1"
min="1"
max=""
name="quantity"
defaultValue={item.quantity}
onBlur={(e) =>
Expand All @@ -362,28 +376,28 @@ const Cart = () => {
onKeyDown={(e) => handleQuantityKeyDown(item.productId, e.key)}
title="Qty"
className="input-text qty text"
disabled={isOutOfStock}
/>
<input
id="plus-button"
type="button"
value="+"
className="plus"
onClick={() => handlePlus(item.productId, item.quantity)}
disabled={isOutOfStock}
/>
</div>
</div>
</td>
<td className="cart__total">{calculateProductPrice(item)}</td>
<td className="cart__close">
{' '}
<button
className="remove_product"
onClick={() => {
openRemoveConfirmDialog(item.productId);
}}
onClick={() => openRemoveConfirmDialog(item.productId)}
disabled={isOutOfStock}
>
<i className="bi bi-x-lg fs-5"></i>
</button>{' '}
</button>
</td>
</tr>
);
Expand Down