Skip to content

feat: Implement Shopping Cart with Item Management and Coupon Support#7

Open
yortch wants to merge 2 commits into
mainfrom
cart-demo
Open

feat: Implement Shopping Cart with Item Management and Coupon Support#7
yortch wants to merge 2 commits into
mainfrom
cart-demo

Conversation

@yortch

@yortch yortch commented Feb 12, 2026

Copy link
Copy Markdown
Owner

Overview

This PR implements a complete shopping cart experience for the OctoCAT Supply demo application, including cart state management, a cart page with full item management, and navigation integration.

Features Implemented

🛒 Cart State Management (CartContext.tsx)

  • Global cart state provider with React Context
  • Methods: addToCart, removeFromCart, updateQuantity, clearCart
  • Computed values: totalItems, subtotal (with product discount support)
  • Persistent cart state across navigation

📄 Cart Page (Cart.tsx)

  • Item Table: Displays S.No, product image, name, unit price, quantity, total, and remove button
  • Quantity Management: Direct inline editing with number input
  • Discount Display: Shows original price with strikethrough when discounts apply
  • Coupon System:
    • OCTOCAT10 - 10% off
    • MEOW20 - 20% off
    • COPILOT5 - 5% off
  • Order Summary: Subtotal, discount amount, $10 flat shipping, grand total
  • Empty State: Friendly message with link back to products
  • Dark/Light Mode: Full theme support throughout

🧭 Navigation Integration (Navigation.tsx)

  • Shopping cart icon in header
  • Badge showing total item count (hidden when 0)
  • Links to /cart page

🛍️ Products Integration (Products.tsx)

  • Replaced alert() stub with real cart functionality
  • Adds items to cart with proper product details
  • Resets quantity counter after adding to cart

📚 Documentation (.github/copilot-instructions.md)

  • Comprehensive codebase guide for AI agents
  • Architecture overview, critical patterns, conventions
  • Integration points and common development tasks

Technical Details

  • TypeScript with strict typing throughout
  • React Context for state management (no external dependencies)
  • Tailwind CSS for responsive, theme-aware styling
  • react-router-dom for cart page routing
  • Follows existing patterns from ThemeContext and AuthContext

Testing

  • ✅ Frontend builds successfully with no TypeScript errors
  • ✅ Dark/light mode works throughout cart experience
  • ✅ Cart badge updates correctly
  • ✅ Discount calculations apply properly
  • ✅ Coupon codes validate and calculate correctly

Screenshots

The implementation matches the design provided in the mockup with:

  • Clean table layout for cart items
  • Order summary sidebar
  • Coupon input and validation
  • Responsive design for mobile/tablet/desktop

Demo scenario ready: This implementation supports the Copilot demo scenario of building a cart page from an image mockup with natural language prompts.

- Add CartContext for global cart state management
- Create Cart page with item table, coupon codes, and order summary
- Add cart icon with item count badge to Navigation
- Wire up Products page to add items to cart
- Support discount calculations and coupon codes (OCTOCAT10, MEOW20, COPILOT5)
- Add dark mode support throughout cart experience
- Update copilot-instructions.md with comprehensive codebase guidance

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a shopping cart feature to the frontend, including global cart state via React Context, a dedicated cart page UI, and navigation/routing updates so users can add products to a persisted cart and manage quantities.

Changes:

  • Added CartContext with localStorage persistence and cart mutation helpers.
  • Implemented cart and checkout placeholder routes/pages, plus cart pricing helpers.
  • Integrated “add to cart” into the Products page and exposed cart access from Navigation.
Show a summary per file
File Description
frontend/src/utils/cartPricing.ts Introduces cart pricing helpers (subtotal/discount/shipping/total).
frontend/src/types/product.ts Adds a shared Product type for frontend usage (incl. optional discount).
frontend/src/types/cart.ts Defines cart item/state types used by context and UI.
frontend/src/context/CartContext.tsx Implements cart state management + persistence + public cart API.
frontend/src/components/Navigation.tsx Adds cart link/icon and item count badge in the header.
frontend/src/components/entity/product/Products.tsx Hooks product list “Add to Cart” into the new cart context and adds a status message.
frontend/src/components/entity/cart/CheckoutPlaceholder.tsx Adds a placeholder checkout route/page.
frontend/src/components/entity/cart/Cart.tsx Adds full cart page UI (table/mobile layout), quantity editing, coupon input, summary, and actions.
frontend/src/App.tsx Registers cart/checkout routes and wraps app with CartProvider.
.github/copilot-instructions.md Adds repository guidance for agents (architecture, commands, patterns, demo hints).

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 10/10 changed files
  • Comments generated: 8

Comment on lines +16 to +20
export const calculateCartSummary = (items: CartItem[]) => {
const subtotal = calculateSubtotal(items);
const discount = subtotal * CART_DISCOUNT_RATE;
const shipping = CART_SHIPPING_FEE;
const total = subtotal - discount + shipping;
Comment on lines +37 to +40
const handleApplyCoupon = () => {
applyCoupon(couponInput);
setUpdateMessage(couponInput.trim() ? 'Coupon applied.' : 'Coupon cleared.');
};
Comment on lines +84 to +86
<span className="absolute -right-2 -top-1 min-w-5 rounded-full bg-primary px-1 text-center text-xs font-bold text-white">
{itemCount}
</span>
Comment on lines +34 to +38
const parsedState = JSON.parse(storedState) as Partial<CartState>;
return {
items: Array.isArray(parsedState.items) ? parsedState.items : [],
couponCode: typeof parsedState.couponCode === 'string' ? parsedState.couponCode : '',
};
Comment on lines +106 to +109
<input
type="number"
min={0}
value={draftQuantities[item.product.productId] ?? item.quantity}
Comment on lines +170 to +173
<input
type="number"
min={0}
value={draftQuantities[item.product.productId] ?? item.quantity}
Comment on lines +21 to +22
npm run test # Run tests in both workspaces (vitest)
npm run test:coverage # API coverage with vitest
Comment on lines +110 to +111
- **No cart persistence**: Cart functionality stub in Products.tsx; implement in new Cart component
- **Discounts**: Implemented in Product model but not applied in UI (add discount calculation when building Cart)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants