feat: implement admin API for user and representation management
- Updated UsersPage to fetch users from the new admin endpoint. - Enhanced user data structure to include 'name' and modified rendering logic. - Added RepresentationDetailPage for detailed representation management. - Created AdminApiController to handle user and representation CRUD operations. - Implemented pagination and search functionality for users and representations. - Updated user and representation data models to reflect new API structure.
This commit is contained in:
@@ -0,0 +1,269 @@
|
||||
import React, { useState } from 'react';
|
||||
import { useParams, useNavigate } from 'react-router-dom';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { PencilIcon, TrashIcon, CheckCircleIcon, XCircleIcon } from '@heroicons/react/24/outline';
|
||||
import { toast } from 'sonner';
|
||||
import { api } from '../lib/api';
|
||||
import type { ApiResponse } from '../lib/api';
|
||||
import type { Representation } from '../types';
|
||||
import { formatDate, formatNumber } from '../lib/utils';
|
||||
import PageHeader from '../components/ui/PageHeader';
|
||||
import { ActiveBadge } from '../components/ui/StatusBadge';
|
||||
import ConfirmDialog from '../components/ui/ConfirmDialog';
|
||||
import Modal from '../components/ui/Modal';
|
||||
|
||||
function DetailRow({ label, value }: { label: string; value: React.ReactNode }) {
|
||||
return (
|
||||
<div className="flex items-start gap-4 py-3 border-b border-gray-100 last:border-0">
|
||||
<span className="text-sm text-gray-500 w-40 shrink-0">{label}</span>
|
||||
<span className="text-sm text-gray-800 font-medium">{value ?? '—'}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function RepresentationDetailPage() {
|
||||
const { uuid } = useParams<{ uuid: string }>();
|
||||
const navigate = useNavigate();
|
||||
const qc = useQueryClient();
|
||||
const [deleteOpen, setDeleteOpen] = useState(false);
|
||||
const [editOpen, setEditOpen] = useState(false);
|
||||
const [formData, setFormData] = useState({ full_name: '', city: '', mobile_number: '', commission_percent: '' });
|
||||
|
||||
const { data, isLoading } = useQuery({
|
||||
queryKey: ['representation', uuid],
|
||||
queryFn: () => api.get<ApiResponse<{ data: Representation }>>(`/api/v1/representation/${uuid}`),
|
||||
enabled: !!uuid,
|
||||
});
|
||||
|
||||
const rep: Representation | undefined = (data?.data as any)?.data ?? data?.data;
|
||||
|
||||
const updateMutation = useMutation({
|
||||
mutationFn: (d: Partial<Representation>) =>
|
||||
api.patch<ApiResponse<Representation>>(`/api/v1/representation/${uuid}`, d),
|
||||
onSuccess: () => {
|
||||
toast.success('اطلاعات بروزرسانی شد');
|
||||
setEditOpen(false);
|
||||
qc.invalidateQueries({ queryKey: ['representation', uuid] });
|
||||
qc.invalidateQueries({ queryKey: ['representations'] });
|
||||
},
|
||||
onError: (err: Error) => toast.error(err.message),
|
||||
});
|
||||
|
||||
const toggleActiveMutation = useMutation({
|
||||
mutationFn: () =>
|
||||
api.patch<ApiResponse<Representation>>(`/api/v1/representation/${uuid}`, {
|
||||
active: !(rep?.active ?? rep?.is_active),
|
||||
}),
|
||||
onSuccess: () => {
|
||||
toast.success('وضعیت تغییر کرد');
|
||||
qc.invalidateQueries({ queryKey: ['representation', uuid] });
|
||||
},
|
||||
onError: (err: Error) => toast.error(err.message),
|
||||
});
|
||||
|
||||
const deleteMutation = useMutation({
|
||||
mutationFn: () => api.delete<ApiResponse<null>>(`/api/v1/representation/${uuid}`),
|
||||
onSuccess: () => {
|
||||
toast.success('نماینده حذف شد');
|
||||
navigate('/admin/representations');
|
||||
},
|
||||
onError: (err: Error) => toast.error(err.message),
|
||||
});
|
||||
|
||||
const openEdit = () => {
|
||||
if (!rep) return;
|
||||
setFormData({
|
||||
full_name: rep.full_name ?? rep.domain ?? '',
|
||||
city: rep.city ?? '',
|
||||
mobile_number: rep.mobile_number ?? '',
|
||||
commission_percent: String(rep.commission_percent),
|
||||
});
|
||||
setEditOpen(true);
|
||||
};
|
||||
|
||||
const isActive = rep?.active ?? rep?.is_active ?? false;
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title="جزئیات نماینده"
|
||||
breadcrumbs={[
|
||||
{ label: 'داشبورد', to: '/admin/dashboard' },
|
||||
{ label: 'نمایندگان', to: '/admin/representations' },
|
||||
{ label: 'جزئیات' },
|
||||
]}
|
||||
/>
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-100 p-6 animate-pulse">
|
||||
<div className="space-y-4">
|
||||
{[1, 2, 3, 4, 5].map((i) => (
|
||||
<div key={i} className="h-8 bg-gray-100 rounded" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!rep) {
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title="نماینده یافت نشد"
|
||||
breadcrumbs={[
|
||||
{ label: 'داشبورد', to: '/admin/dashboard' },
|
||||
{ label: 'نمایندگان', to: '/admin/representations' },
|
||||
]}
|
||||
/>
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-100 p-12 text-center text-gray-400">
|
||||
نمایندهای با این شناسه یافت نشد.
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const name = rep.full_name ?? rep.domain ?? '—';
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title={name}
|
||||
breadcrumbs={[
|
||||
{ label: 'داشبورد', to: '/admin/dashboard' },
|
||||
{ label: 'نمایندگان', to: '/admin/representations' },
|
||||
{ label: name },
|
||||
]}
|
||||
action={
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => toggleActiveMutation.mutate()}
|
||||
disabled={toggleActiveMutation.isPending}
|
||||
className={`flex items-center gap-1.5 px-3 py-2 text-sm rounded-[10px] border transition-colors disabled:opacity-50 ${
|
||||
isActive
|
||||
? 'border-red-300 text-red-600 hover:bg-red-50'
|
||||
: 'border-green-300 text-green-600 hover:bg-green-50'
|
||||
}`}
|
||||
>
|
||||
{isActive
|
||||
? <><XCircleIcon className="w-4 h-4" /> غیرفعال کردن</>
|
||||
: <><CheckCircleIcon className="w-4 h-4" /> فعال کردن</>
|
||||
}
|
||||
</button>
|
||||
<button
|
||||
onClick={openEdit}
|
||||
className="flex items-center gap-1.5 px-3 py-2 text-sm rounded-[10px] border border-gray-300 text-gray-700 hover:bg-gray-50 transition-colors"
|
||||
>
|
||||
<PencilIcon className="w-4 h-4" />
|
||||
ویرایش
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setDeleteOpen(true)}
|
||||
className="flex items-center gap-1.5 px-3 py-2 text-sm rounded-[10px] border border-red-300 text-red-600 hover:bg-red-50 transition-colors"
|
||||
>
|
||||
<TrashIcon className="w-4 h-4" />
|
||||
حذف
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
|
||||
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||
{/* Basic Info */}
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-100 p-6">
|
||||
<h2 className="text-sm font-semibold text-gray-700 mb-4">اطلاعات پایه</h2>
|
||||
<DetailRow label="نام کامل" value={rep.full_name} />
|
||||
<DetailRow label="موبایل" value={
|
||||
rep.mobile_number
|
||||
? <span dir="ltr">{rep.mobile_number}</span>
|
||||
: null
|
||||
} />
|
||||
<DetailRow label="شهر" value={rep.city} />
|
||||
<DetailRow label="درصد کمیسیون" value={`${formatNumber(rep.commission_percent)}٪`} />
|
||||
<DetailRow label="وضعیت" value={<ActiveBadge active={isActive} />} />
|
||||
<DetailRow label="تاریخ ثبت" value={formatDate(rep.created_at)} />
|
||||
</div>
|
||||
|
||||
{/* Bank Account */}
|
||||
<div className="bg-white rounded-2xl shadow-sm border border-gray-100 p-6">
|
||||
<h2 className="text-sm font-semibold text-gray-700 mb-4">اطلاعات بانکی</h2>
|
||||
{rep.bank_account ? (
|
||||
<>
|
||||
<DetailRow label="شماره کارت" value={
|
||||
rep.bank_account.card
|
||||
? <span dir="ltr" className="font-mono tracking-widest">{rep.bank_account.card}</span>
|
||||
: null
|
||||
} />
|
||||
<DetailRow label="نام بانک" value={rep.bank_account.bank_name} />
|
||||
<DetailRow label="شماره شبا" value={
|
||||
rep.bank_account.iban
|
||||
? <span dir="ltr" className="font-mono text-xs">{rep.bank_account.iban}</span>
|
||||
: null
|
||||
} />
|
||||
</>
|
||||
) : (
|
||||
<p className="text-sm text-gray-400 text-center py-6">اطلاعات بانکی ثبت نشده</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Edit Modal */}
|
||||
<Modal open={editOpen} title="ویرایش نماینده"
|
||||
onClose={() => setEditOpen(false)}
|
||||
footer={
|
||||
<>
|
||||
<button onClick={() => setEditOpen(false)}
|
||||
className="px-4 py-2 border border-gray-300 text-sm text-gray-700 rounded-[10px] hover:bg-gray-50 transition-colors">
|
||||
لغو
|
||||
</button>
|
||||
<button
|
||||
onClick={() => updateMutation.mutate({
|
||||
full_name: formData.full_name,
|
||||
city: formData.city,
|
||||
mobile_number: formData.mobile_number || null,
|
||||
commission_percent: parseFloat(formData.commission_percent) || rep.commission_percent,
|
||||
})}
|
||||
disabled={updateMutation.isPending}
|
||||
className="px-4 py-2 bg-primary-600 text-white text-sm rounded-[10px] hover:bg-primary-700 disabled:opacity-50 transition-colors">
|
||||
{updateMutation.isPending ? 'در حال ذخیره...' : 'ذخیره'}
|
||||
</button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">نام کامل</label>
|
||||
<input value={formData.full_name} onChange={(e) => setFormData((p) => ({ ...p, full_name: e.target.value }))}
|
||||
className="w-full h-11 border border-gray-300 rounded-[10px] px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">شهر</label>
|
||||
<input value={formData.city} onChange={(e) => setFormData((p) => ({ ...p, city: e.target.value }))}
|
||||
className="w-full h-11 border border-gray-300 rounded-[10px] px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">موبایل</label>
|
||||
<input value={formData.mobile_number} onChange={(e) => setFormData((p) => ({ ...p, mobile_number: e.target.value }))}
|
||||
dir="ltr" className="w-full h-11 border border-gray-300 rounded-[10px] px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">درصد کمیسیون</label>
|
||||
<input value={formData.commission_percent} onChange={(e) => setFormData((p) => ({ ...p, commission_percent: e.target.value }))}
|
||||
type="number" min="0" max="100" dir="ltr"
|
||||
className="w-full h-11 border border-gray-300 rounded-[10px] px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500" />
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
<ConfirmDialog
|
||||
open={deleteOpen}
|
||||
title="حذف نماینده"
|
||||
message={`آیا از حذف نماینده "${name}" اطمینان دارید؟`}
|
||||
confirmLabel="حذف"
|
||||
danger
|
||||
loading={deleteMutation.isPending}
|
||||
onConfirm={() => deleteMutation.mutate()}
|
||||
onCancel={() => setDeleteOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user