diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 29b9f53..ca7f856 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -5,8 +5,10 @@ import About from './components/About'; import Footer from './components/Footer'; import Products from './components/entity/product/Products'; import Login from './components/Login'; +import Cart from './components/Cart'; import { AuthProvider } from './context/AuthContext'; import { ThemeProvider } from './context/ThemeContext'; +import { CartProvider } from './context/CartContext'; import AdminProducts from './components/admin/AdminProducts'; import { useTheme } from './context/ThemeContext'; @@ -25,6 +27,7 @@ function ThemedApp() { } /> } /> } /> + } /> } /> } /> @@ -39,7 +42,9 @@ function App() { return ( - + + + ); diff --git a/frontend/src/components/Cart.tsx b/frontend/src/components/Cart.tsx new file mode 100644 index 0000000..c2af8cd --- /dev/null +++ b/frontend/src/components/Cart.tsx @@ -0,0 +1,164 @@ +import { Link } from 'react-router-dom'; +import { useCart, CartItem } from '../context/CartContext'; +import { useTheme } from '../context/ThemeContext'; + +export default function Cart() { + const { items, removeItem, updateQuantity, clearCart, totalPrice } = useCart(); + const { darkMode } = useTheme(); + + return ( +
+
+
+

+ Shopping Cart +

+ {items.length > 0 && ( + + )} +
+ + {items.length === 0 ? ( +
+ + + +

+ Your cart is empty +

+ + Browse Products + +
+ ) : ( +
+ {items.map((item: CartItem) => { + const effectivePrice = item.price * (1 - (item.discount ?? 0)); + return ( +
+ {item.name} +
+

+ {item.name} +

+

+ ${effectivePrice.toFixed(2)}{' '} + + / {item.unit} + +

+
+
+ + + {item.quantity} + + +
+

+ ${(effectivePrice * item.quantity).toFixed(2)} +

+ +
+ ); + })} + +
+
+

+ Total: ${totalPrice.toFixed(2)} +

+ +
+
+
+ )} +
+
+ ); +} diff --git a/frontend/src/components/Navigation.tsx b/frontend/src/components/Navigation.tsx index 5f35e12..82a36c2 100644 --- a/frontend/src/components/Navigation.tsx +++ b/frontend/src/components/Navigation.tsx @@ -1,11 +1,13 @@ import { Link } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; import { useTheme } from '../context/ThemeContext'; +import { useCart } from '../context/CartContext'; import { useState } from 'react'; export default function Navigation() { const { isLoggedIn, isAdmin, logout } = useAuth(); const { darkMode, toggleTheme } = useTheme(); + const { totalItems } = useCart(); const [adminMenuOpen, setAdminMenuOpen] = useState(false); return ( @@ -85,6 +87,31 @@ export default function Navigation() {
+ + + + + {totalItems > 0 && ( + + {totalItems > 99 ? '99+' : totalItems} + + )} +