Task: Fix Cart Quantity Bug

Task Description: Fix the bug in the cart where product quantity can reach 0 or become negative by adding a minimum quantity check to the updateQuantity function.

video thumbnailYouTube Icon

Detailed Description

OpenAI iconAsk ChatGPT

Fix Cart Quantity Bug

Objective

Fix the bug in the cart where product quantity can reach 0 or become negative.

Why It Matters

A quantity of 0 or less makes no logical sense and can cause calculation errors. This teaches defensive programming and input validation.

Technical Requirements

  • Modify updateQuantity in src/features/cart/useCartStore.js
  • Add a minimum quantity check: if newQuantity < 1, either:
    • Clamp to 1 (prevent going below), OR
    • Remove the item from the cart
  • Ensure the UI reflects this constraint properly

Expected Behavior

  • Clicking "−" when quantity is 1 either keeps it at 1 or removes the item
  • Quantity never displays as 0 or negative
  • The total price calculation remains correct

Acceptance Criteria

  • Quantity cannot go below 1
  • Decided behavior for quantity at 1 (clamp or remove)
  • Total price is always correct
  • UI buttons disabled or provide feedback at minimum quantity

Edge Cases

  • Rapid clicking of the "−" button
  • Direct quantity input (if implemented later)
  • Cart with multiple items at quantity 1

Hints

  • Add if (newQuantity < 1) return; before the state update, or
  • Add if (newQuantity < 1) { removeFromCart(productId); return; }
  • Consider disabling the "−" button in the UI when quantity is 1