Refine catalog cart controls and show cart badge in header

This commit is contained in:
Ruslan Bakiev
2026-04-03 08:50:24 +07:00
parent 4f14c24e46
commit 0f7787de36
3 changed files with 39 additions and 9 deletions

View File

@@ -12,8 +12,8 @@ export type ClientCartItem = {
};
function normalizeQuantity(value: number) {
if (!Number.isFinite(value) || value < 1) {
return 1;
if (!Number.isFinite(value) || value < 0) {
return 0;
}
return Math.floor(value);
@@ -64,7 +64,13 @@ export function useClientCart() {
return;
}
existing.quantity = normalizeQuantity(quantity);
const normalizedQuantity = normalizeQuantity(quantity);
if (normalizedQuantity === 0) {
removeProduct(productId);
return;
}
existing.quantity = normalizedQuantity;
}
function incrementQuantity(productId: string) {
@@ -82,7 +88,13 @@ export function useClientCart() {
return;
}
existing.quantity = normalizeQuantity(existing.quantity - 1);
const normalizedQuantity = normalizeQuantity(existing.quantity - 1);
if (normalizedQuantity === 0) {
removeProduct(productId);
return;
}
existing.quantity = normalizedQuantity;
}
function removeProduct(productId: string) {