167 lines
6.6 KiB
TypeScript
167 lines
6.6 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { toast } from 'react-hot-toast';
|
|
import api from '../services/api';
|
|
import ConfirmModal from '../components/ui/ConfirmModal';
|
|
import Button from '../components/ui/Button';
|
|
|
|
|
|
export interface WholesaleRequest {
|
|
id: string;
|
|
fullName?: string;
|
|
email?: string;
|
|
phoneNumber?: string;
|
|
mobile?: string;
|
|
role: string;
|
|
createdAt: string;
|
|
}
|
|
|
|
export default function WholesaleApplications() {
|
|
const [requests, setRequests] = useState<WholesaleRequest[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [approveTargetId, setApproveTargetId] = useState<string | null>(null);
|
|
const [rejectTargetId, setRejectTargetId] = useState<string | null>(null);
|
|
|
|
const fetchRequestsData = async () => {
|
|
try {
|
|
const res = await api.get('/wholesale/requests');
|
|
setRequests(res.data || []);
|
|
} catch (err) {
|
|
console.error('Failed to fetch wholesale requests:', err);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
let isMounted = true;
|
|
api.get('/wholesale/requests').then(res => {
|
|
if (isMounted) setRequests(res.data || []);
|
|
}).catch(err => {
|
|
console.error('Failed to fetch wholesale requests:', err);
|
|
}).finally(() => {
|
|
if (isMounted) setLoading(false);
|
|
});
|
|
return () => {
|
|
isMounted = false;
|
|
};
|
|
}, []);
|
|
|
|
const confirmApprove = async () => {
|
|
if (!approveTargetId) return;
|
|
try {
|
|
await api.put(`/wholesale/approve/${approveTargetId}`);
|
|
toast.success('کاربر با موفقیت به نقش خریدار عمده (User_Wholesale) ارتقا یافت.');
|
|
fetchRequestsData();
|
|
} catch {
|
|
toast.error('خطا در ارتقای نقش کاربر');
|
|
} finally {
|
|
setApproveTargetId(null);
|
|
}
|
|
};
|
|
|
|
const confirmReject = async () => {
|
|
if (!rejectTargetId) return;
|
|
try {
|
|
await api.put(`/wholesale/reject/${rejectTargetId}`);
|
|
toast.success('درخواست همکاری رد شد.');
|
|
fetchRequestsData();
|
|
} catch {
|
|
toast.error('خطا در رد درخواست همکاری');
|
|
} finally {
|
|
setRejectTargetId(null);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="p-6 font-vazir" dir="rtl">
|
|
<div className="flex justify-between items-center mb-6">
|
|
<div>
|
|
<h1 className="text-2xl font-bold text-gray-900">درخواستهای ارتقا به همکار B2B</h1>
|
|
<p className="text-gray-500 text-sm mt-1">تایید یا رد درخواستهای کاربران عادی برای خرید عمده و دسترسی به تخفیفهای تجاری</p>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="text-center py-12 text-gray-500 font-bold">در حال بارگذاری درخواستها...</div>
|
|
) : (
|
|
<div className="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
|
|
<table className="w-full text-right border-collapse">
|
|
<thead>
|
|
<tr className="bg-gray-50/50 border-b border-gray-100 text-xs text-gray-500">
|
|
<th className="px-6 py-4 font-bold">نام و اطلاعات متقاضی</th>
|
|
<th className="px-6 py-4 font-bold">وضعیت فعلی</th>
|
|
<th className="px-6 py-4 font-bold">تاریخ درخواست</th>
|
|
<th className="px-6 py-4 font-bold text-center">عملیات</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-gray-100 text-sm">
|
|
{requests.length === 0 ? (
|
|
<tr>
|
|
<td colSpan={4} className="text-center py-8 text-gray-400 font-medium">هیچ درخواستی در انتظار بررسی یافت نشد.</td>
|
|
</tr>
|
|
) : (
|
|
requests.map((u: WholesaleRequest) => (
|
|
<tr key={u.id} className="hover:bg-gray-50/60 transition-colors">
|
|
<td className="px-6 py-4">
|
|
<div className="font-bold text-gray-900">{u.fullName || u.email || 'کاربر بدون نام'}</div>
|
|
<div className="text-xs text-gray-400 dir-ltr text-right">{u.email || u.phoneNumber}</div>
|
|
</td>
|
|
<td className="px-6 py-4">
|
|
<span className={`inline-flex px-3 py-1 rounded-full text-xs font-bold ${u.role === 'User_Wholesale' ? 'bg-green-100 text-green-700' : 'bg-amber-100 text-amber-700'
|
|
}`}>
|
|
{u.role === 'User_Wholesale' ? 'خریدار عمده تاییدشده' : 'در انتظار بررسی B2B'}
|
|
</span>
|
|
</td>
|
|
<td className="px-6 py-4 text-xs text-gray-500 font-mono">
|
|
{new Date(u.createdAt).toLocaleDateString('fa-IR')}
|
|
</td>
|
|
<td className="px-6 py-4 text-center">
|
|
<div className="flex items-center justify-center gap-2">
|
|
{u.role !== 'User_Wholesale' && (
|
|
<Button
|
|
variant="primary"
|
|
size="xs"
|
|
className="!bg-emerald-600 hover:!bg-emerald-700"
|
|
onClick={() => setApproveTargetId(u.id)}
|
|
>
|
|
تایید و اعطای لایسنس B2B
|
|
</Button>
|
|
)}
|
|
<Button
|
|
variant="secondary"
|
|
size="xs"
|
|
onClick={() => setRejectTargetId(u.id)}
|
|
>
|
|
رد درخواست
|
|
</Button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
))
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
)}
|
|
|
|
<ConfirmModal
|
|
isOpen={!!approveTargetId}
|
|
title="تایید حساب همکار B2B"
|
|
message="آیا از تایید حساب همکار و اعطای قیمتهای عمده-۳۰٪ مطمئن هستید؟"
|
|
confirmText="تایید و اعطا"
|
|
isDestructive={false}
|
|
onConfirm={confirmApprove}
|
|
onCancel={() => setApproveTargetId(null)}
|
|
/>
|
|
|
|
<ConfirmModal
|
|
isOpen={!!rejectTargetId}
|
|
title="رد درخواست همکاری"
|
|
message="آیا از رد درخواست همکاری مطمئن هستید؟"
|
|
confirmText="رد درخواست"
|
|
isDestructive={true}
|
|
onConfirm={confirmReject}
|
|
onCancel={() => setRejectTargetId(null)}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|