diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx new file mode 100644 index 0000000..35d7322 --- /dev/null +++ b/src/app/admin/page.tsx @@ -0,0 +1,343 @@ +"use client"; + +import { ThemeProvider } from "@/providers/themeProvider/ThemeProvider"; +import NavbarStyleCentered from "@/components/navbar/NavbarStyleCentered/NavbarStyleCentered"; +import FooterBase from "@/components/sections/footer/FooterBase"; +import Input from "@/components/form/Input"; +import ButtonHoverMagnetic from "@/components/button/ButtonHoverMagnetic/ButtonHoverMagnetic"; +import { useState, useEffect } from "react"; +import { Edit2, Trash2, Plus, LogOut } from "lucide-react"; +import { useRouter } from "next/navigation"; + +interface Product { + id: string; + name: string; + price: string; + category: string; +} + +export default function AdminPage() { + const [products, setProducts] = useState([ + { id: "1", name: "Samsung Galaxy M54", price: "₹25,999", category: "Smartphones" }, + { id: "2", name: "Redmi Note 13", price: "₹18,999", category: "Smartphones" }, + { id: "3", name: "OPPO A78", price: "₹16,999", category: "Smartphones" }, + { id: "4", name: "Vivo T2", price: "₹19,999", category: "Smartphones" }, + { id: "5", name: "iPhone 14", price: "₹78,999", category: "Smartphones" }, + { id: "6", name: "Universal Phone Case", price: "₹499", category: "Accessories" }, + ]); + const [editingId, setEditingId] = useState(null); + const [editValues, setEditValues] = useState>({}); + const [newProduct, setNewProduct] = useState>({ + name: "", price: "", category: "Smartphones"}); + const [isAuthorized, setIsAuthorized] = useState(false); + const [loading, setLoading] = useState(true); + const router = useRouter(); + + useEffect(() => { + // Check if user is authenticated + const token = localStorage.getItem("adminToken"); + if (!token) { + router.push("/login"); + } else { + setIsAuthorized(true); + setLoading(false); + } + }, [router]); + + const handleLogout = () => { + localStorage.removeItem("adminToken"); + router.push("/login"); + }; + + const handleEdit = (product: Product) => { + setEditingId(product.id); + setEditValues({ ...product }); + }; + + const handleSave = (id: string) => { + setProducts( + products.map((p) => (p.id === id ? { ...p, ...editValues } : p)) + ); + setEditingId(null); + setEditValues({}); + }; + + const handleDelete = (id: string) => { + if (confirm("Are you sure you want to delete this product?")) { + setProducts(products.filter((p) => p.id !== id)); + } + }; + + const handleAddProduct = () => { + if (newProduct.name && newProduct.price && newProduct.category) { + const product: Product = { + id: Date.now().toString(), + name: newProduct.name as string, + price: newProduct.price as string, + category: newProduct.category as string, + }; + setProducts([...products, product]); + setNewProduct({ name: "", price: "", category: "Smartphones" }); + } + }; + + if (loading) { + return ( +
+

Loading...

+
+ ); + } + + if (!isAuthorized) { + return ( +
+

Redirecting to login...

+
+ ); + } + + return ( + + + +
+
+
+
+

Admin Dashboard

+

Manage prices and product inventory

+
+ +
+ + {/* Add New Product Section */} +
+

+ + Add New Product +

+
+ + setNewProduct({ ...newProduct, name: value }) + } + type="text" + placeholder="Product name" + /> + + setNewProduct({ ...newProduct, price: value }) + } + type="text" + placeholder="Price (e.g., ₹25,999)" + /> + + +
+
+ + {/* Products Table */} +
+ + + + + + + + + + + {products.map((product) => ( + + + + + + + ))} + +
Product NamePriceCategoryActions
+ {editingId === product.id ? ( + + setEditValues({ ...editValues, name: value }) + } + type="text" + placeholder="Product name" + /> + ) : ( + {product.name} + )} + + {editingId === product.id ? ( + + setEditValues({ ...editValues, price: value }) + } + type="text" + placeholder="Price" + /> + ) : ( + + {product.price} + + )} + + {editingId === product.id ? ( + + ) : ( + {product.category} + )} + +
+ {editingId === product.id ? ( + <> + + + + ) : ( + <> + + + + )} +
+
+
+ + {products.length === 0 && ( +
+

No products found. Add your first product above.

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