feat: complete admin panel major functionalities including responsive layout, recharts, custom modals, inline editing, and loading states
This commit is contained in:
1 parent
ee041d4e52
commit
a7ebb8d792
33 files changed
+1311
-173
No files matched your search
@@ -1,11 +1,52 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { DollarSign, ShoppingCart, Users, Activity } from 'lucide-react';
|
||||
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, PieChart, Pie, Cell } from 'recharts';
|
||||
import api from '../services/api';
|
||||
|
||||
export default function Dashboard() {
|
||||
const [data, setData] = useState({
|
||||
revenue: 0,
|
||||
newOrders: 0,
|
||||
users: 0,
|
||||
todayVisits: 0
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const fetchStats = async () => {
|
||||
try {
|
||||
const response = await api.get('/admin/dashboard/stats');
|
||||
if (response.data?.success) {
|
||||
setData(response.data.data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch stats', err);
|
||||
}
|
||||
};
|
||||
fetchStats();
|
||||
}, []);
|
||||
|
||||
const chartData = [
|
||||
{ name: 'شنبه', بازدید: 400, فروش: 2400 },
|
||||
{ name: 'یکشنبه', بازدید: 300, فروش: 1398 },
|
||||
{ name: 'دوشنبه', بازدید: 200, فروش: 9800 },
|
||||
{ name: 'سهشنبه', بازدید: 278, فروش: 3908 },
|
||||
{ name: 'چهارشنبه', بازدید: 189, فروش: 4800 },
|
||||
{ name: 'پنجشنبه', بازدید: 239, فروش: 3800 },
|
||||
{ name: 'جمعه', بازدید: 349, فروش: 4300 },
|
||||
];
|
||||
|
||||
const pieData = [
|
||||
{ name: 'سگ', value: 400 },
|
||||
{ name: 'گربه', value: 300 },
|
||||
{ name: 'پرنده', value: 300 },
|
||||
];
|
||||
const COLORS = ['#0088FE', '#00C49F', '#FFBB28'];
|
||||
|
||||
const stats = [
|
||||
{ title: 'درآمد کل', value: '۴۵,۰۰۰,۰۰۰ تومان', icon: DollarSign, color: 'text-green-600', bg: 'bg-green-100' },
|
||||
{ title: 'سفارشات جدید', value: '۱۲', icon: ShoppingCart, color: 'text-blue-600', bg: 'bg-blue-100' },
|
||||
{ title: 'کاربران فعال', value: '۱,۰۲۴', icon: Users, color: 'text-purple-600', bg: 'bg-purple-100' },
|
||||
{ title: 'بازدید امروز', value: '۳۴۲', icon: Activity, color: 'text-orange-600', bg: 'bg-orange-100' },
|
||||
{ title: 'درآمد کل', value: `${data.revenue.toLocaleString()} تومان`, icon: DollarSign, color: 'text-green-600', bg: 'bg-green-100' },
|
||||
{ title: 'سفارشات جدید', value: data.newOrders.toString(), icon: ShoppingCart, color: 'text-blue-600', bg: 'bg-blue-100' },
|
||||
{ title: 'کاربران فعال', value: data.users.toString(), icon: Users, color: 'text-purple-600', bg: 'bg-purple-100' },
|
||||
{ title: 'بازدید امروز', value: data.todayVisits.toString(), icon: Activity, color: 'text-orange-600', bg: 'bg-orange-100' },
|
||||
];
|
||||
|
||||
return (
|
||||
@@ -32,8 +73,55 @@ export default function Dashboard() {
|
||||
})}
|
||||
</div>
|
||||
|
||||
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-100 min-h-[400px] flex items-center justify-center">
|
||||
<p className="text-gray-400 font-bold">بخش نمودارها (در فاز بعدی اضافه میشود)</p>
|
||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
||||
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-100 col-span-1 lg:col-span-2">
|
||||
<h3 className="text-lg font-bold text-gray-900 mb-6">روند بازدید و فروش (۷ روز گذشته)</h3>
|
||||
<div className="h-80 w-full" dir="ltr">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<LineChart data={chartData} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" vertical={false} />
|
||||
<XAxis dataKey="name" axisLine={false} tickLine={false} />
|
||||
<YAxis axisLine={false} tickLine={false} />
|
||||
<Tooltip />
|
||||
<Line type="monotone" dataKey="فروش" stroke="#8b5cf6" strokeWidth={3} dot={{ r: 4 }} activeDot={{ r: 8 }} />
|
||||
<Line type="monotone" dataKey="بازدید" stroke="#10b981" strokeWidth={3} dot={{ r: 4 }} />
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-100 col-span-1">
|
||||
<h3 className="text-lg font-bold text-gray-900 mb-6">دستهبندی محصولات</h3>
|
||||
<div className="h-80 w-full flex items-center justify-center">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<PieChart>
|
||||
<Pie
|
||||
data={pieData}
|
||||
cx="50%"
|
||||
cy="50%"
|
||||
innerRadius={60}
|
||||
outerRadius={80}
|
||||
fill="#8884d8"
|
||||
paddingAngle={5}
|
||||
dataKey="value"
|
||||
>
|
||||
{pieData.map((entry, index) => (
|
||||
<Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
|
||||
))}
|
||||
</Pie>
|
||||
<Tooltip />
|
||||
</PieChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
<div className="flex justify-center gap-4 mt-4">
|
||||
{pieData.map((entry, index) => (
|
||||
<div key={entry.name} className="flex items-center gap-2">
|
||||
<div className="w-3 h-3 rounded-full" style={{ backgroundColor: COLORS[index % COLORS.length] }}></div>
|
||||
<span className="text-sm font-medium text-gray-600">{entry.name}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Lock, Mail, ChevronLeft } from 'lucide-react';
|
||||
import api from '../services/api';
|
||||
|
||||
export default function Login() {
|
||||
const [email, setEmail] = useState('');
|
||||
@@ -8,15 +9,21 @@ export default function Login() {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const handleLogin = (e: React.FormEvent) => {
|
||||
const handleLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setIsLoading(true);
|
||||
|
||||
// Simulate API call
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const response = await api.post('/auth/admin-login', { email, password });
|
||||
if (response.data?.success) {
|
||||
localStorage.setItem('adminToken', response.data.data.accessToken);
|
||||
navigate('/');
|
||||
}
|
||||
} catch (error) {
|
||||
alert('ایمیل یا رمز عبور اشتباه است');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
navigate('/');
|
||||
}, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { ShoppingCart, Eye, Truck, Check, X, Search, Filter } from 'lucide-react';
|
||||
|
||||
const orders = [
|
||||
{ id: 'ORD-10045', customer: 'علی رضایی', amount: '۴,۵۰۰,۰۰۰ تومان', date: '۱۴۰۲/۰۸/۱۵', status: 'pending', items: 3 },
|
||||
{ id: 'ORD-10044', customer: 'کلینیک دامپزشکی مهر', amount: '۱۲,۸۰۰,۰۰۰ تومان', date: '۱۴۰۲/۰۸/۱۴', status: 'shipped', items: 12 },
|
||||
{ id: 'ORD-10043', customer: 'سارا احمدی', amount: '۸۵۰,۰۰۰ تومان', date: '۱۴۰۲/۰۸/۱۲', status: 'delivered', items: 1 },
|
||||
{ id: 'ORD-10042', customer: 'محمد کریمی', amount: '۳,۲۰۰,۰۰۰ تومان', date: '۱۴۰۲/۰۸/۱۱', status: 'cancelled', items: 2 },
|
||||
];
|
||||
import api from '../services/api';
|
||||
import Skeleton from '../components/ui/Skeleton';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
|
||||
const statusStyles: Record<string, { label: string, color: string }> = {
|
||||
pending: { label: 'در حال بررسی', color: 'bg-orange-100 text-orange-700' },
|
||||
@@ -15,6 +12,40 @@ const statusStyles: Record<string, { label: string, color: string }> = {
|
||||
};
|
||||
|
||||
export default function Orders() {
|
||||
const [orders, setOrders] = useState<any[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [processingId, setProcessingId] = useState<string | null>(null);
|
||||
|
||||
const fetchOrders = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
const response = await api.get('/admin/orders');
|
||||
if (response.data?.success) {
|
||||
setOrders(response.data.data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch orders', err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchOrders();
|
||||
}, []);
|
||||
|
||||
const handleUpdateStatus = async (id: string, status: string) => {
|
||||
try {
|
||||
setProcessingId(id);
|
||||
await api.put(`/admin/orders/${id}/status`, { status });
|
||||
fetchOrders();
|
||||
} catch (err) {
|
||||
console.error('Failed to update status', err);
|
||||
} finally {
|
||||
setProcessingId(null);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
||||
@@ -58,42 +89,59 @@ export default function Orders() {
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{orders.map((order) => (
|
||||
<tr key={order.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="py-4 px-6 font-bold text-purple-600" dir="ltr">{order.id}</td>
|
||||
<td className="py-4 px-6 font-bold text-gray-900">{order.customer}</td>
|
||||
<td className="py-4 px-6 font-bold text-gray-900">{order.amount}</td>
|
||||
<td className="py-4 px-6 text-gray-500 text-sm font-medium">{order.date}</td>
|
||||
<td className="py-4 px-6 text-gray-500 font-bold">{order.items} عدد</td>
|
||||
<td className="py-4 px-6">
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-bold ${statusStyles[order.status].color}`}>
|
||||
{statusStyles[order.status].label}
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
<button className="text-gray-600 hover:bg-gray-100 p-2 rounded-lg transition-colors" title="مشاهده فاکتور">
|
||||
<Eye className="w-4 h-4" />
|
||||
</button>
|
||||
{order.status === 'pending' && (
|
||||
<>
|
||||
<button className="text-blue-600 hover:bg-blue-50 p-2 rounded-lg transition-colors" title="ثبت به عنوان ارسال شده">
|
||||
<Truck className="w-4 h-4" />
|
||||
{isLoading ? (
|
||||
Array.from({ length: 4 }).map((_, i) => (
|
||||
<tr key={i}>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-32" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-16" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-8 w-24 mx-auto" /></td>
|
||||
</tr>
|
||||
))
|
||||
) : (
|
||||
orders.map((order) => {
|
||||
const isProcessing = processingId === order.id;
|
||||
return (
|
||||
<tr key={order.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="py-4 px-6 font-bold text-purple-600" dir="ltr">{order.id.slice(0,8)}</td>
|
||||
<td className="py-4 px-6 font-bold text-gray-900">{order.user?.firstName} {order.user?.lastName}</td>
|
||||
<td className="py-4 px-6 font-bold text-gray-900">{Number(order.totalAmount).toLocaleString()} تومان</td>
|
||||
<td className="py-4 px-6 text-gray-500 text-sm font-medium">{new Date(order.createdAt).toLocaleDateString('fa-IR')}</td>
|
||||
<td className="py-4 px-6 text-gray-500 font-bold">{order.orderItems?.length || 0} قلم</td>
|
||||
<td className="py-4 px-6">
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-bold ${statusStyles[order.status]?.color || 'bg-gray-100 text-gray-700'}`}>
|
||||
{statusStyles[order.status]?.label || order.status}
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
<button className="text-gray-600 hover:bg-gray-100 p-2 rounded-lg transition-colors" title="مشاهده فاکتور" disabled={isProcessing}>
|
||||
<Eye className="w-4 h-4" />
|
||||
</button>
|
||||
<button className="text-red-600 hover:bg-red-50 p-2 rounded-lg transition-colors" title="لغو سفارش">
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{order.status === 'shipped' && (
|
||||
<button className="text-green-600 hover:bg-green-50 p-2 rounded-lg transition-colors" title="ثبت به عنوان تحویل داده شده">
|
||||
<Check className="w-4 h-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{order.status === 'processing' && (
|
||||
<>
|
||||
<button onClick={() => handleUpdateStatus(order.id, 'shipped')} className="text-blue-600 hover:bg-blue-50 p-2 rounded-lg transition-colors" title="ثبت به عنوان ارسال شده" disabled={isProcessing}>
|
||||
{isProcessing ? <Spinner size="sm" /> : <Truck className="w-4 h-4" />}
|
||||
</button>
|
||||
<button onClick={() => handleUpdateStatus(order.id, 'cancelled')} className="text-red-600 hover:bg-red-50 p-2 rounded-lg transition-colors" title="لغو سفارش" disabled={isProcessing}>
|
||||
{isProcessing ? <Spinner size="sm" /> : <X className="w-4 h-4" />}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
{order.status === 'shipped' && (
|
||||
<button onClick={() => handleUpdateStatus(order.id, 'delivered')} className="text-green-600 hover:bg-green-50 p-2 rounded-lg transition-colors" title="ثبت به عنوان تحویل داده شده" disabled={isProcessing}>
|
||||
{isProcessing ? <Spinner size="sm" /> : <Check className="w-4 h-4" />}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
@@ -1,13 +1,72 @@
|
||||
import { Package, Plus, Edit3, Trash2, Search } from 'lucide-react';
|
||||
|
||||
const products = [
|
||||
{ id: 1, nameFa: 'کانینا بیوتین فورت', category: 'پوست و مو', price: '۲,۵۰۰,۰۰۰', stock: 45, status: 'موجود' },
|
||||
{ id: 2, nameFa: 'کانینا پِتویتال', category: 'مفاصل', price: '۳,۲۰۰,۰۰۰', stock: 12, status: 'موجود' },
|
||||
{ id: 3, nameFa: 'شیر خشک مخصوص سگ', category: 'رشد توله', price: '۱,۸۰۰,۰۰۰', stock: 0, status: 'ناموجود' },
|
||||
{ id: 4, nameFa: 'قطره تمیزکننده چشم', category: 'بهداشتی', price: '۹۵۰,۰۰۰', stock: 120, status: 'موجود' },
|
||||
];
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Package, Plus, Edit3, Trash2, Search, Check, X } from 'lucide-react';
|
||||
import api from '../services/api';
|
||||
import ConfirmModal from '../components/ui/ConfirmModal';
|
||||
import Skeleton from '../components/ui/Skeleton';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
|
||||
export default function Products() {
|
||||
const [products, setProducts] = useState<any[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
// Confirm Modal state
|
||||
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
||||
const [productToDelete, setProductToDelete] = useState<string | null>(null);
|
||||
|
||||
// Edit Mode state
|
||||
const [editingId, setEditingId] = useState<string | null>(null);
|
||||
const [editForm, setEditForm] = useState({ packageSize: 0, priceValue: 0 });
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
|
||||
const fetchProducts = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
const response = await api.get('/admin/products');
|
||||
if (response.data?.success) {
|
||||
setProducts(response.data.data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch products', err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchProducts();
|
||||
}, []);
|
||||
|
||||
const openDeleteModal = (id: string) => {
|
||||
setProductToDelete(id);
|
||||
setIsDeleteModalOpen(true);
|
||||
};
|
||||
|
||||
const confirmDelete = async () => {
|
||||
if (productToDelete) {
|
||||
await api.get(`/admin/products/${productToDelete}/delete`);
|
||||
setIsDeleteModalOpen(false);
|
||||
setProductToDelete(null);
|
||||
fetchProducts();
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditClick = (product: any) => {
|
||||
setEditingId(product.id);
|
||||
setEditForm({ packageSize: Number(product.packageSize), priceValue: Number(product.priceValue) });
|
||||
};
|
||||
|
||||
const handleSaveEdit = async (id: string) => {
|
||||
try {
|
||||
setIsSaving(true);
|
||||
await api.put(`/admin/products/${id}`, editForm);
|
||||
setEditingId(null);
|
||||
fetchProducts();
|
||||
} catch (err) {
|
||||
console.error('Failed to update product', err);
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
||||
@@ -49,46 +108,103 @@ export default function Products() {
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{products.map((product) => (
|
||||
<tr key={product.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="py-4 px-6 font-bold text-gray-900">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-10 h-10 bg-gray-100 rounded-lg flex items-center justify-center">
|
||||
<Package className="w-5 h-5 text-gray-400" />
|
||||
</div>
|
||||
{product.nameFa}
|
||||
</div>
|
||||
</td>
|
||||
<td className="py-4 px-6 text-gray-500">{product.category}</td>
|
||||
<td className="py-4 px-6 font-bold text-gray-900">{product.price}</td>
|
||||
<td className="py-4 px-6">
|
||||
<span className={`font-bold ${product.stock < 10 ? 'text-red-500' : 'text-gray-900'}`}>
|
||||
{product.stock} عدد
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-bold ${
|
||||
product.stock > 0 ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'
|
||||
}`}>
|
||||
{product.status}
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
<button className="text-blue-600 hover:bg-blue-50 p-2 rounded-lg transition-colors" title="ویرایش">
|
||||
<Edit3 className="w-4 h-4" />
|
||||
</button>
|
||||
<button className="text-red-600 hover:bg-red-50 p-2 rounded-lg transition-colors" title="حذف">
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{isLoading ? (
|
||||
Array.from({ length: 4 }).map((_, i) => (
|
||||
<tr key={i}>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-32" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-16" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-20" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-8 w-20 mx-auto" /></td>
|
||||
</tr>
|
||||
))
|
||||
) : (
|
||||
products.map((product) => {
|
||||
const isEditing = editingId === product.id;
|
||||
return (
|
||||
<tr key={product.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="py-4 px-6 font-bold text-gray-900">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-10 h-10 bg-gray-100 rounded-lg flex items-center justify-center">
|
||||
<Package className="w-5 h-5 text-gray-400" />
|
||||
</div>
|
||||
{product.name}
|
||||
</div>
|
||||
</td>
|
||||
<td className="py-4 px-6 text-gray-500">{product.category}</td>
|
||||
<td className="py-4 px-6 font-bold text-gray-900">
|
||||
{isEditing ? (
|
||||
<input
|
||||
type="number"
|
||||
className="border border-purple-300 rounded p-1 w-24"
|
||||
value={editForm.priceValue}
|
||||
onChange={(e) => setEditForm({...editForm, priceValue: Number(e.target.value)})}
|
||||
/>
|
||||
) : (
|
||||
product.priceValue?.toLocaleString()
|
||||
)}
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
{isEditing ? (
|
||||
<input
|
||||
type="number"
|
||||
className="border border-purple-300 rounded p-1 w-16"
|
||||
value={editForm.packageSize}
|
||||
onChange={(e) => setEditForm({...editForm, packageSize: Number(e.target.value)})}
|
||||
/>
|
||||
) : (
|
||||
<span className={`font-bold ${product.packageSize < 10 ? 'text-red-500' : 'text-gray-900'}`}>
|
||||
{product.packageSize} عدد
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-bold ${
|
||||
product.packageSize > 0 ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'
|
||||
}`}>
|
||||
{product.packageSize > 0 ? 'موجود' : 'ناموجود'}
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4 px-6">
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
{isEditing ? (
|
||||
<>
|
||||
<button onClick={() => handleSaveEdit(product.id)} className="text-green-600 hover:bg-green-50 p-2 rounded-lg transition-colors" title="ذخیره" disabled={isSaving}>
|
||||
{isSaving ? <Spinner size="sm" /> : <Check className="w-4 h-4" />}
|
||||
</button>
|
||||
<button onClick={() => setEditingId(null)} className="text-gray-500 hover:bg-gray-50 p-2 rounded-lg transition-colors" title="لغو" disabled={isSaving}>
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<button onClick={() => handleEditClick(product)} className="text-blue-600 hover:bg-blue-50 p-2 rounded-lg transition-colors" title="ویرایش">
|
||||
<Edit3 className="w-4 h-4" />
|
||||
</button>
|
||||
<button onClick={() => openDeleteModal(product.id)} className="text-red-600 hover:bg-red-50 p-2 rounded-lg transition-colors" title="حذف">
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ConfirmModal
|
||||
isOpen={isDeleteModalOpen}
|
||||
title="حذف محصول"
|
||||
message="آیا از حذف این محصول اطمینان دارید؟ این عمل غیرقابل بازگشت است."
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setIsDeleteModalOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,13 +1,53 @@
|
||||
import { Users as UsersIcon, CheckCircle, XCircle, Search, Filter } from 'lucide-react';
|
||||
|
||||
const users = [
|
||||
{ id: 1, name: 'علی رضایی', email: 'ali@example.com', role: 'B2C', status: 'active', date: '۱۴۰۲/۰۸/۱۵' },
|
||||
{ id: 2, name: 'کلینیک دامپزشکی مهر', email: 'mehr@clinic.com', role: 'B2B', status: 'pending', date: '۱۴۰۲/۰۸/۱۴' },
|
||||
{ id: 3, name: 'سارا احمدی', email: 'sara@example.com', role: 'B2C', status: 'active', date: '۱۴۰۲/۰۸/۱۲' },
|
||||
{ id: 4, name: 'داروخانه پتشاپ', email: 'info@petshop.ir', role: 'B2B', status: 'active', date: '۱۴۰۲/۰۸/۱۰' },
|
||||
];
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Users as UsersIcon, CheckCircle, XCircle, Search, Filter, Edit3, Check, X } from 'lucide-react';
|
||||
import api from '../services/api';
|
||||
import Skeleton from '../components/ui/Skeleton';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
|
||||
export default function Users() {
|
||||
const [users, setUsers] = useState<any[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
// Edit State
|
||||
const [editingId, setEditingId] = useState<string | null>(null);
|
||||
const [editRole, setEditRole] = useState<string>('');
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
|
||||
const fetchUsers = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
const response = await api.get('/admin/users');
|
||||
if (response.data?.success) {
|
||||
setUsers(response.data.data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch users', err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchUsers();
|
||||
}, []);
|
||||
|
||||
const handleEditClick = (user: any) => {
|
||||
setEditingId(user.id);
|
||||
setEditRole(user.role || 'User_PetOwner');
|
||||
};
|
||||
|
||||
const handleSaveEdit = async (id: string) => {
|
||||
try {
|
||||
setIsSaving(true);
|
||||
await api.put(`/admin/users/${id}/role`, { role: editRole });
|
||||
setEditingId(null);
|
||||
fetchUsers();
|
||||
} catch (err) {
|
||||
console.error('Failed to update user role', err);
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
||||
@@ -49,47 +89,77 @@ export default function Users() {
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{users.map((user) => (
|
||||
<tr key={user.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="py-4 px-6 font-bold text-gray-900">{user.name}</td>
|
||||
<td className="py-4 px-6 text-gray-500" dir="ltr">{user.email}</td>
|
||||
<td className="py-4 px-6">
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-bold ${user.role === 'B2B' ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-700'}`}>
|
||||
{user.role === 'B2B' ? 'همکار (B2B)' : 'مشتری عادی'}
|
||||
</span>
|
||||
</td>
|
||||
<td className="py-4 px-6 text-gray-500 text-sm font-medium">{user.date}</td>
|
||||
<td className="py-4 px-6">
|
||||
{user.status === 'active' ? (
|
||||
<span className="flex items-center gap-1.5 text-green-600 font-bold text-sm">
|
||||
<CheckCircle className="w-4 h-4" />
|
||||
تایید شده
|
||||
</span>
|
||||
) : (
|
||||
<span className="flex items-center gap-1.5 text-orange-500 font-bold text-sm">
|
||||
<div className="w-2 h-2 rounded-full bg-orange-500"></div>
|
||||
در انتظار تایید
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="py-4 px-6 text-center">
|
||||
{user.role === 'B2B' && user.status === 'pending' ? (
|
||||
<div className="flex justify-center gap-2">
|
||||
<button className="text-green-600 hover:bg-green-50 p-1.5 rounded-lg transition-colors" title="تایید مدارک">
|
||||
<CheckCircle className="w-5 h-5" />
|
||||
</button>
|
||||
<button className="text-red-600 hover:bg-red-50 p-1.5 rounded-lg transition-colors" title="رد درخواست">
|
||||
<XCircle className="w-5 h-5" />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<button className="text-purple-600 hover:underline text-sm font-bold">
|
||||
مشاهده پروفایل
|
||||
</button>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{isLoading ? (
|
||||
Array.from({ length: 4 }).map((_, i) => (
|
||||
<tr key={i}>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-32" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-40" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-20" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-6 w-24" /></td>
|
||||
<td className="py-4 px-6"><Skeleton className="h-8 w-20 mx-auto" /></td>
|
||||
</tr>
|
||||
))
|
||||
) : (
|
||||
users.map((user) => {
|
||||
const isEditing = editingId === user.id;
|
||||
return (
|
||||
<tr key={user.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="py-4 px-6 font-bold text-gray-900">{user.firstName} {user.lastName}</td>
|
||||
<td className="py-4 px-6 text-gray-500" dir="ltr">{user.email}</td>
|
||||
<td className="py-4 px-6">
|
||||
{isEditing ? (
|
||||
<select
|
||||
className="border border-purple-300 rounded p-1"
|
||||
value={editRole}
|
||||
onChange={(e) => setEditRole(e.target.value)}
|
||||
>
|
||||
<option value="User_PetOwner">مشتری عادی</option>
|
||||
<option value="User_B2B">همکار (B2B)</option>
|
||||
<option value="ADMIN">ادمین</option>
|
||||
</select>
|
||||
) : (
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-bold ${user.role?.includes('B2B') ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-700'}`}>
|
||||
{user.role?.includes('B2B') ? 'همکار (B2B)' : (user.role === 'ADMIN' ? 'مدیر سیستم' : 'مشتری عادی')}
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="py-4 px-6 text-gray-500 text-sm font-medium">{new Date(user.createdAt).toLocaleDateString('fa-IR')}</td>
|
||||
<td className="py-4 px-6">
|
||||
{user.status === 'active' ? (
|
||||
<span className="flex items-center gap-1.5 text-green-600 font-bold text-sm">
|
||||
<CheckCircle className="w-4 h-4" />
|
||||
تایید شده
|
||||
</span>
|
||||
) : (
|
||||
<span className="flex items-center gap-1.5 text-orange-500 font-bold text-sm">
|
||||
<div className="w-2 h-2 rounded-full bg-orange-500"></div>
|
||||
در انتظار تایید
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="py-4 px-6 text-center">
|
||||
{isEditing ? (
|
||||
<div className="flex justify-center gap-2">
|
||||
<button onClick={() => handleSaveEdit(user.id)} className="text-green-600 hover:bg-green-50 p-1.5 rounded-lg transition-colors" disabled={isSaving}>
|
||||
{isSaving ? <Spinner size="sm" /> : <Check className="w-5 h-5" />}
|
||||
</button>
|
||||
<button onClick={() => setEditingId(null)} className="text-gray-500 hover:bg-gray-50 p-1.5 rounded-lg transition-colors" disabled={isSaving}>
|
||||
<X className="w-5 h-5" />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="flex justify-center gap-2">
|
||||
<button onClick={() => handleEditClick(user)} className="text-blue-600 hover:bg-blue-50 p-1.5 rounded-lg transition-colors" title="ویرایش نقش">
|
||||
<Edit3 className="w-5 h-5" />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user