Refine catalog cart controls and show cart badge in header
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user