forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.jsx
More file actions
134 lines (124 loc) · 4.31 KB
/
Copy pathpage.jsx
File metadata and controls
134 lines (124 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"use client";
import React, { useState, useEffect } from "react";
import { listProducts, deleteProduct } from "../../lib/products";
import AddProductForm from "./AddProductForm";
import EditProductForm from "./EditProductForm";
import AdminGuard from "../../components/AdminGuard";
import Link from "next/link";
import { SiStellar } from "react-icons/si";
import { MdInventory } from "react-icons/md";
const ProductsAdminContent = () => {
const [products, setProducts] = useState([]);
const [selectedProductId, setSelectedProductId] = useState(null);
useEffect(() => {
fetchProducts();
}, []);
const fetchProducts = async () => {
try {
const data = await listProducts();
setProducts(data);
} catch (error) {
console.error("Error fetching products: ", error);
}
};
const handleProductAdded = () => {
setSelectedProductId(null);
fetchProducts();
};
const handleProductUpdated = () => {
setSelectedProductId(null);
fetchProducts();
};
const handleDelete = async (id) => {
try {
await deleteProduct(id);
setProducts(products.filter((product) => product.id !== id));
} catch (error) {
console.error("Error deleting product: ", error);
}
};
return (
<div className="container mx-auto px-4 py-8">
<div className="flex justify-center gap-4 mb-8">
<Link
href="/admin"
className="flex items-center gap-2 px-6 py-3 bg-purple-600 text-white rounded-lg shadow hover:bg-purple-700 transition-colors"
>
<MdInventory className="text-xl" />
Products
</Link>
<Link
href="/admin/orders"
className="flex items-center gap-2 px-6 py-3 bg-purple-600 text-white rounded-lg shadow hover:bg-purple-700 transition-colors"
>
<SiStellar className="text-xl" />
Stellar Orders
</Link>
</div>
<h1 className="text-4xl font-extrabold mb-8 text-center text-purple-600">
Products Admin
</h1>
<AddProductForm onProductAdded={handleProductAdded} />
{selectedProductId && (
<EditProductForm
productId={selectedProductId}
onProductUpdated={handleProductUpdated}
/>
)}
<div className="mt-12">
<h2 className="text-3xl font-bold mb-6">Product List</h2>
<div className="overflow-x-auto">
<table className="min-w-full bg-white border border-gray-300 rounded-lg shadow-md">
<thead className="bg-purple-600 text-white">
<tr>
<th className="py-3 px-6 border-b text-left">Name</th>
<th className="py-3 px-6 border-b text-left">Price</th>
<th className="py-3 px-6 border-b text-left">Image</th>
<th className="py-3 px-6 border-b text-left">Actions</th>
</tr>
</thead>
<tbody>
{products.map((product) => (
<tr key={product.id} className="border-b hover:bg-gray-50">
<td className="py-4 px-6 text-gray-800">{product.name}</td>
<td className="py-4 px-6 text-gray-800">
${Number(product.price).toFixed(2)}
</td>
<td className="py-4 px-6">
<img
src={product.img}
alt={product.name}
className="h-20 w-20 object-cover rounded-lg border border-gray-300"
/>
</td>
<td className="py-4 px-6">
<button
className="text-blue-600 hover:text-blue-800 font-semibold mr-4"
onClick={() => setSelectedProductId(product.id)}
>
Edit
</button>
<button
className="text-purple-600 hover:text-purple-800 font-semibold"
onClick={() => handleDelete(product.id)}
>
Delete
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
</div>
);
};
const ProductsAdmin = () => {
return (
<AdminGuard>
<ProductsAdminContent />
</AdminGuard>
);
};
export default ProductsAdmin;