diff --git a/frontend/package.json b/frontend/package.json index bc9fa52..8d357b1 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -7,6 +7,7 @@ "dev": "vite", "build": "tsc -b && vite build", "lint": "eslint .", + "test": "vitest run", "preview": "vite preview" }, "dependencies": { diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index d0b02da..26e9af1 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -4,8 +4,10 @@ import Welcome from './components/Welcome'; import About from './components/About'; import Footer from './components/Footer'; import Products from './components/entity/product/Products'; +import CartPage from './components/cart/CartPage'; import Login from './components/Login'; import { AuthProvider } from './context/AuthContext'; +import { CartProvider } from './context/CartContext'; import { ThemeProvider } from './context/ThemeContext'; import AdminProducts from './components/admin/AdminProducts'; import { useTheme } from './context/ThemeContext'; @@ -23,6 +25,7 @@ function ThemedApp() { } /> } /> } /> + } /> } /> } /> @@ -36,9 +39,11 @@ function ThemedApp() { function App() { return ( - - - + + + + + ); } diff --git a/frontend/src/components/Footer.tsx b/frontend/src/components/Footer.tsx index 7ce970f..4d4abc9 100644 --- a/frontend/src/components/Footer.tsx +++ b/frontend/src/components/Footer.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import { Link } from 'react-router-dom'; import { useTheme } from '../context/ThemeContext'; const Footer: React.FC = () => { @@ -20,8 +21,8 @@ const Footer: React.FC = () => {

Account

    -
  • My Cart
  • -
  • Checkout
  • +
  • My Cart
  • +
  • Checkout
  • Shopping Details
  • Order
  • Help Center
  • diff --git a/frontend/src/components/Navigation.tsx b/frontend/src/components/Navigation.tsx index d7b393b..fb66426 100644 --- a/frontend/src/components/Navigation.tsx +++ b/frontend/src/components/Navigation.tsx @@ -1,10 +1,12 @@ import { Link } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; +import { useCart } from '../context/CartContext'; import { useTheme } from '../context/ThemeContext'; import { useState } from 'react'; export default function Navigation() { const { isLoggedIn, isAdmin, logout } = useAuth(); + const { itemCount } = useCart(); const { darkMode, toggleTheme } = useTheme(); const [adminMenuOpen, setAdminMenuOpen] = useState(false); @@ -29,6 +31,7 @@ export default function Navigation() {
    Home Products + Cart About us {isAdmin && (
    @@ -68,6 +71,18 @@ export default function Navigation() {
    + + Cart + {itemCount > 0 && ( + + {itemCount} + + )} + + {coupon && ( + + )} +
    + +
+ + ) : ( +
+

Your cart is empty. Add products to continue.

+
+ )} + + + + + + {statusMessage && ( +

+ {statusMessage} +

+ )} + {lastUpdatedAt && ( +

+ Last updated at {lastUpdatedAt} +

+ )} + + + ); +} diff --git a/frontend/src/components/entity/product/Products.tsx b/frontend/src/components/entity/product/Products.tsx index af2319e..89de214 100644 --- a/frontend/src/components/entity/product/Products.tsx +++ b/frontend/src/components/entity/product/Products.tsx @@ -2,19 +2,9 @@ import { useState } from 'react'; import axios from 'axios'; import { useQuery } from 'react-query'; import { api } from '../../../api/config'; +import { useCart } from '../../../context/CartContext'; import { useTheme } from '../../../context/ThemeContext'; - -interface Product { - productId: number; - name: string; - description: string; - price: number; - imgName: string; - sku: string; - unit: string; - supplierId: number; - discount?: number; -} +import type { Product } from '../../../types/product'; const fetchProducts = async (): Promise => { const { data } = await axios.get(`${api.baseURL}${api.endpoints.products}`); @@ -27,6 +17,7 @@ export default function Products() { const [selectedProduct, setSelectedProduct] = useState(null); const [showModal, setShowModal] = useState(false); const { data: products, isLoading, error } = useQuery('products', fetchProducts); + const { addItem } = useCart(); const { darkMode } = useTheme(); const filteredProducts = products?.filter(product => @@ -41,11 +32,11 @@ export default function Products() { })); }; - const handleAddToCart = (productId: number) => { + const handleAddToCart = (product: Product) => { + const productId = product.productId; const quantity = quantities[productId] || 0; if (quantity > 0) { - // TODO: Implement cart functionality - alert(`Added ${quantity} items to cart`); + addItem(product, quantity); setQuantities(prev => ({ ...prev, [productId]: 0 @@ -169,7 +160,7 @@ export default function Products() {