diff --git a/frontend/src/AdminRoute.tsx b/frontend/src/AdminRoute.tsx new file mode 100644 index 0000000..cac4a61 --- /dev/null +++ b/frontend/src/AdminRoute.tsx @@ -0,0 +1,11 @@ +import React from 'react'; +import { Navigate, Outlet } from 'react-router-dom'; +import { useAuth } from './AuthContext'; + +const AdminRoute: React.FC = () => { + const { isAuthenticated } = useAuth(); + + return isAuthenticated ? : ; +}; + +export default AdminRoute; diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index e27721f..941eb68 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -5,6 +5,8 @@ import ProductList from './ProductList'; import ProductDetail from './ProductDetail'; import CartPage from './CartPage'; import AdminLayout from './AdminLayout'; +import AdminRoute from './AdminRoute'; +import ProductManagement from './ProductManagement'; import { CartProvider } from './CartContext'; import { AuthProvider } from './AuthContext'; @@ -27,10 +29,12 @@ function App() { } /> - }> - Product Management (To be implemented)} /> - Category Management (To be implemented)} /> - Seller Management (To be implemented)} /> + }> + }> + } /> + Category Management (To be implemented)} /> + Seller Management (To be implemented)} /> + diff --git a/frontend/src/ProductManagement.tsx b/frontend/src/ProductManagement.tsx new file mode 100644 index 0000000..54a2983 --- /dev/null +++ b/frontend/src/ProductManagement.tsx @@ -0,0 +1,76 @@ +import React, { useEffect, useState } from 'react'; +import axios from 'axios'; + +interface Product { + id: number; + name: string; + price: string; + description: string; + category: string; +} + +const ProductManagement: React.FC = () => { + const [products, setProducts] = useState([]); + const [loading, setLoading] = useState(true); + + useEffect(() => { + fetchProducts(); + }, []); + + const fetchProducts = () => { + axios.get('http://127.0.0.1:8000/products/') + .then(response => { + setProducts(response.data); + setLoading(false); + }) + .catch(error => { + console.error('Error fetching products:', error); + setLoading(false); + }); + }; + + const deleteProduct = (id: number) => { + if (window.confirm('Are you sure you want to delete this product?')) { + axios.delete(`http://127.0.0.1:8000/products/${id}/`) + .then(() => fetchProducts()) + .catch(error => console.error('Error deleting product:', error)); + } + }; + + if (loading) return
Loading...
; + + return ( +
+

Product Management

+ + + + + + + + + + + {products.map(product => ( + + + + + + + ))} + +
IDNamePriceActions
{product.id}{product.name}{product.price} + +
+
+ ); +}; + +export default ProductManagement;