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:
@@ -1,6 +1,6 @@
|
||||
import React, { useState } from 'react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { CheckIcon, XMarkIcon, TrashIcon, PlusIcon, PencilIcon } from '@heroicons/react/24/outline';
|
||||
import { CheckIcon, XMarkIcon, TrashIcon, PlusIcon } from '@heroicons/react/24/outline';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { z } from 'zod';
|
||||
@@ -18,8 +18,7 @@ import Modal from '../components/ui/Modal';
|
||||
|
||||
const templateSchema = z.object({
|
||||
name: z.string().min(2, 'نام قالب الزامی است'),
|
||||
category: z.string().min(1, 'دستهبندی الزامی است'),
|
||||
content: z.string().min(5, 'متن قالب الزامی است'),
|
||||
body: z.string().min(5, 'متن قالب الزامی است'),
|
||||
});
|
||||
type TemplateFormData = z.infer<typeof templateSchema>;
|
||||
|
||||
@@ -38,7 +37,9 @@ export default function SmsPage() {
|
||||
const sampleTemplatesQuery = useQuery({
|
||||
queryKey: ['sms-samples', page],
|
||||
queryFn: () =>
|
||||
api.get<PaginatedResponse<SmsTemplate>>(`/api/v1/sms/sample-templates?page=${page}&limit=${limit}`),
|
||||
api.get<PaginatedResponse<SmsTemplate>>(
|
||||
`/api/v1/admin/sms/sample-templates?status=approved&page=${page}&limit=${limit}`,
|
||||
),
|
||||
enabled: activeTab === 'samples',
|
||||
});
|
||||
|
||||
@@ -46,7 +47,7 @@ export default function SmsPage() {
|
||||
queryKey: ['sms-pending', page],
|
||||
queryFn: () =>
|
||||
api.get<PaginatedResponse<SmsTemplate>>(
|
||||
`/api/v1/sms/templates?status=pending_approval&page=${page}&limit=${limit}`,
|
||||
`/api/v1/admin/sms/sample-templates?status=pending&page=${page}&limit=${limit}`,
|
||||
),
|
||||
enabled: activeTab === 'pending',
|
||||
});
|
||||
@@ -54,7 +55,7 @@ export default function SmsPage() {
|
||||
const logsQuery = useQuery({
|
||||
queryKey: ['sms-logs', page],
|
||||
queryFn: () =>
|
||||
api.get<PaginatedResponse<SmsLog>>(`/api/v1/sms/logs?page=${page}&limit=${limit}`),
|
||||
api.get<PaginatedResponse<SmsLog>>(`/api/v1/admin/sms/logs?page=${page}&limit=${limit}`),
|
||||
enabled: activeTab === 'logs',
|
||||
});
|
||||
|
||||
@@ -64,7 +65,7 @@ export default function SmsPage() {
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: (d: TemplateFormData) =>
|
||||
api.post<ApiResponse<SmsTemplate>>('/api/v1/sms/sample-template', d),
|
||||
api.post<ApiResponse<SmsTemplate>>('/api/v1/sms/template', d),
|
||||
onSuccess: () => {
|
||||
toast.success('قالب اضافه شد');
|
||||
setAddOpen(false);
|
||||
@@ -76,7 +77,7 @@ export default function SmsPage() {
|
||||
|
||||
const approveMutation = useMutation({
|
||||
mutationFn: (t: SmsTemplate) =>
|
||||
api.patch<ApiResponse<null>>(`/api/v1/sms/templates/${t.uuid}/approve`, {}),
|
||||
api.post<ApiResponse<null>>(`/api/v1/admin/sms/template/${t.uuid}/approve`, {}),
|
||||
onSuccess: () => {
|
||||
toast.success('قالب تأیید شد');
|
||||
qc.invalidateQueries({ queryKey: ['sms-pending'] });
|
||||
@@ -86,7 +87,7 @@ export default function SmsPage() {
|
||||
|
||||
const rejectMutation = useMutation({
|
||||
mutationFn: ({ t, reason }: { t: SmsTemplate; reason: string }) =>
|
||||
api.patch<ApiResponse<null>>(`/api/v1/sms/templates/${t.uuid}/reject`, { reason }),
|
||||
api.post<ApiResponse<null>>(`/api/v1/admin/sms/template/${t.uuid}/reject`, { reason }),
|
||||
onSuccess: () => {
|
||||
toast.success('قالب رد شد');
|
||||
setRejectTarget(null);
|
||||
@@ -110,29 +111,23 @@ export default function SmsPage() {
|
||||
const templateColumns: Column<SmsTemplate>[] = [
|
||||
{ key: 'name', header: 'نام', render: (t) => <span className="font-medium">{t.name}</span> },
|
||||
{
|
||||
key: 'category',
|
||||
header: 'دستهبندی',
|
||||
render: (t) => (
|
||||
<span className="text-xs bg-gray-100 text-gray-600 px-2 py-0.5 rounded-full">{t.category}</span>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: 'content',
|
||||
key: 'body',
|
||||
header: 'محتوا',
|
||||
render: (t) => <span className="text-xs text-gray-500 line-clamp-1 max-w-xs">{t.content}</span>,
|
||||
render: (t) => <span className="text-xs text-gray-500 line-clamp-1 max-w-xs">{t.body}</span>,
|
||||
},
|
||||
{ key: 'status', header: 'وضعیت', render: (t) => <StatusBadge type="sms" value={t.status} /> },
|
||||
{ key: 'created_at', header: 'تاریخ', render: (t) => formatDate(t.created_at) },
|
||||
];
|
||||
|
||||
const pendingColumns: Column<SmsTemplate>[] = [
|
||||
...templateColumns.filter((c) => c.key !== 'status'),
|
||||
{ key: 'owner_name', header: 'ارسالکننده' },
|
||||
];
|
||||
const pendingColumns: Column<SmsTemplate>[] = templateColumns.filter((c) => c.key !== 'status');
|
||||
|
||||
const logColumns: Column<SmsLog>[] = [
|
||||
{ key: 'recipient', header: 'گیرنده', render: (l) => <span dir="ltr">{l.recipient}</span> },
|
||||
{ key: 'message', header: 'پیام', render: (l) => <span className="text-xs text-gray-500 line-clamp-1 max-w-xs">{l.message}</span> },
|
||||
{
|
||||
key: 'message',
|
||||
header: 'پیام',
|
||||
render: (l) => <span className="text-xs text-gray-500 line-clamp-1 max-w-xs">{l.message}</span>,
|
||||
},
|
||||
{
|
||||
key: 'status',
|
||||
header: 'وضعیت',
|
||||
@@ -196,21 +191,19 @@ export default function SmsPage() {
|
||||
<>
|
||||
<DataTable<SmsTemplate>
|
||||
columns={templateColumns}
|
||||
data={sampleTemplatesQuery.data?.data?.items ?? []}
|
||||
data={sampleTemplatesQuery.data?.data ?? []}
|
||||
loading={sampleTemplatesQuery.isLoading}
|
||||
emptyMessage="هیچ قالبی یافت نشد"
|
||||
actions={(t) => (
|
||||
<>
|
||||
<button onClick={() => setDeleteTarget(t)}
|
||||
className="p-1.5 text-gray-400 hover:text-red-600 hover:bg-red-50 rounded-lg transition-colors" title="حذف">
|
||||
<TrashIcon className="w-4 h-4" />
|
||||
</button>
|
||||
</>
|
||||
<button onClick={() => setDeleteTarget(t)}
|
||||
className="p-1.5 text-gray-400 hover:text-red-600 hover:bg-red-50 rounded-lg transition-colors" title="حذف">
|
||||
<TrashIcon className="w-4 h-4" />
|
||||
</button>
|
||||
)}
|
||||
/>
|
||||
<Pagination
|
||||
page={page}
|
||||
total={sampleTemplatesQuery.data?.data?.total ?? 0}
|
||||
total={sampleTemplatesQuery.data?.meta?.totalRecords ?? 0}
|
||||
limit={limit}
|
||||
onPageChange={setPage}
|
||||
/>
|
||||
@@ -221,7 +214,7 @@ export default function SmsPage() {
|
||||
<>
|
||||
<DataTable<SmsTemplate>
|
||||
columns={pendingColumns}
|
||||
data={pendingTemplatesQuery.data?.data?.items ?? []}
|
||||
data={pendingTemplatesQuery.data?.data ?? []}
|
||||
loading={pendingTemplatesQuery.isLoading}
|
||||
emptyMessage="هیچ قالبی در انتظار تأیید نیست"
|
||||
actions={(t) => (
|
||||
@@ -239,7 +232,7 @@ export default function SmsPage() {
|
||||
/>
|
||||
<Pagination
|
||||
page={page}
|
||||
total={pendingTemplatesQuery.data?.data?.total ?? 0}
|
||||
total={pendingTemplatesQuery.data?.meta?.totalRecords ?? 0}
|
||||
limit={limit}
|
||||
onPageChange={setPage}
|
||||
/>
|
||||
@@ -250,13 +243,13 @@ export default function SmsPage() {
|
||||
<>
|
||||
<DataTable<SmsLog>
|
||||
columns={logColumns}
|
||||
data={logsQuery.data?.data?.items ?? []}
|
||||
data={logsQuery.data?.data ?? []}
|
||||
loading={logsQuery.isLoading}
|
||||
emptyMessage="لاگی یافت نشد"
|
||||
/>
|
||||
<Pagination
|
||||
page={page}
|
||||
total={logsQuery.data?.data?.total ?? 0}
|
||||
total={logsQuery.data?.meta?.totalRecords ?? 0}
|
||||
limit={limit}
|
||||
onPageChange={setPage}
|
||||
/>
|
||||
@@ -286,17 +279,11 @@ export default function SmsPage() {
|
||||
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" />
|
||||
{errors.name && <p className="text-red-500 text-xs mt-1">{errors.name.message}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">دستهبندی</label>
|
||||
<input {...register('category')} placeholder="مثال: appointment"
|
||||
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" />
|
||||
{errors.category && <p className="text-red-500 text-xs mt-1">{errors.category.message}</p>}
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">متن قالب</label>
|
||||
<textarea {...register('content')} rows={4} placeholder="متن پیامک..."
|
||||
<textarea {...register('body')} rows={4} placeholder="متن پیامک..."
|
||||
className="w-full border border-gray-300 rounded-[10px] px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500 resize-none" />
|
||||
{errors.content && <p className="text-red-500 text-xs mt-1">{errors.content.message}</p>}
|
||||
{errors.body && <p className="text-red-500 text-xs mt-1">{errors.body.message}</p>}
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
Reference in New Issue
Block a user