diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 13e5c3f..2335416 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -2,19 +2,24 @@ import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import Header from './Header'; import LoginPage from './LoginPage'; import ProductList from './ProductList'; +import ProductDetail from './ProductDetail'; +import { CartProvider } from './CartContext'; function App() { return ( -
-
- - } /> - } /> - -
+ +
+
+ + } /> + } /> + } /> + +
+ ) } -export default App \ No newline at end of file +export default App; \ No newline at end of file diff --git a/frontend/src/CartContext.tsx b/frontend/src/CartContext.tsx new file mode 100644 index 0000000..95e1419 --- /dev/null +++ b/frontend/src/CartContext.tsx @@ -0,0 +1,34 @@ +import React, { createContext, useContext, useState } from 'react'; + +interface CartItem { + id: number; + name: string; + price: number; +} + +interface CartContextType { + cart: CartItem[]; + addToCart: (product: CartItem) => void; +} + +const CartContext = createContext(undefined); + +export const CartProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { + const [cart, setCart] = useState([]); + + const addToCart = (product: CartItem) => { + setCart((prev) => [...prev, product]); + }; + + return ( + + {children} + + ); +}; + +export const useCart = () => { + const context = useContext(CartContext); + if (!context) throw new Error('useCart must be used within CartProvider'); + return context; +}; diff --git a/frontend/src/ProductDetail.tsx b/frontend/src/ProductDetail.tsx new file mode 100644 index 0000000..cc66be4 --- /dev/null +++ b/frontend/src/ProductDetail.tsx @@ -0,0 +1,42 @@ +import React, { useEffect, useState } from 'react'; +import { useParams, Link } from 'react-router-dom'; +import axios from 'axios'; +import { useCart } from './CartContext'; + +interface Product { + id: number; + name: string; + description: string; + price: number; +} + +const ProductDetail: React.FC = () => { + const { id } = useParams<{ id: string }>(); + const [product, setProduct] = useState(null); + const { addToCart } = useCart(); + + useEffect(() => { + axios.get(`http://127.0.0.1:8000/api/products/${id}/`) + .then(res => setProduct(res.data)) + .catch(err => console.error(err)); + }, [id]); + + if (!product) return
Loading...
; + + return ( +
+ ← Back to Products +

{product.name}

+

{product.description}

+

${product.price}

+ +
+ ); +}; + +export default ProductDetail; \ No newline at end of file