diff --git a/src/app/admin/events/create/page.tsx b/src/app/admin/events/create/page.tsx new file mode 100644 index 0000000..2bcf37a --- /dev/null +++ b/src/app/admin/events/create/page.tsx @@ -0,0 +1,5 @@ +import AdminEventForm from "@/views/AdminEventForm"; + +export default function AdminEventCreateRoute() { + return ; +} diff --git a/src/views/AdminEventEdit.tsx b/src/views/AdminEventEdit.tsx index a520673..d05f2f1 100644 --- a/src/views/AdminEventEdit.tsx +++ b/src/views/AdminEventEdit.tsx @@ -1,284 +1,7 @@ "use client"; -import * as React from 'react'; -import { useNavigate, useParams, Navigate } from '@/lib/router'; -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; -import { useAuth } from '@/contexts/AuthContext'; -import { api } from '@/lib/api'; -import type { EventAdminDetailSchema, EventUpdateSchema } from '@/lib/types'; -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; -import { Button } from '@/components/ui/button'; -import { Input } from '@/components/ui/input'; -import { Textarea } from '@/components/ui/textarea'; -import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; -import { useToast } from '@/hooks/use-toast'; -import { resolveErrorMessage } from '@/lib/utils'; - -const statusOptions = [ - { value: 'draft', label: 'پیش‌نویس' }, - { value: 'published', label: 'منتشر شده' }, - { value: 'cancelled', label: 'لغو شده' }, - { value: 'completed', label: 'برگزار شده' }, -]; - -const typeOptions = [ - { value: 'online', label: 'آنلاین' }, - { value: 'on_site', label: 'حضوری' }, - { value: 'hybrid', label: 'ترکیبی' }, -]; - -const toInputDateTime = (iso?: string | null) => { - if (!iso) return ''; - const d = new Date(iso); - return `${d.getFullYear().toString().padStart(4, '0')}-${(d.getMonth() + 1) - .toString() - .padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}T${d - .getHours() - .toString() - .padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`; -}; +import AdminEventForm from "@/views/AdminEventForm"; export default function AdminEventEdit() { - const { user, isAuthenticated, loading } = useAuth(); - const { id } = useParams<{ id: string }>(); - const navigate = useNavigate(); - const eventId = Number(id); - const { toast } = useToast(); - const queryClient = useQueryClient(); - - const detailQuery = useQuery({ - queryKey: ['admin', 'edit-event', eventId], - queryFn: () => api.getEventAdminDetail(eventId), - enabled: Boolean(eventId) && isAuthenticated, - }); - - const [formData, setFormData] = React.useState({ - title: '', - status: 'draft' as NonNullable, - event_type: 'online' as NonNullable, - price: '', - capacity: '', - start_time: '', - end_time: '', - registration_start_date: '', - registration_end_date: '', - location: '', - address: '', - online_link: '', - description: '', - }); - - React.useEffect(() => { - if (detailQuery.data) { - const d: EventAdminDetailSchema = detailQuery.data; - setFormData({ - title: d.title || '', - status: d.status || 'draft', - event_type: d.event_type || 'online', - price: d.price ? Math.floor(Number(d.price) / 10).toString() : '', - capacity: d.capacity != null ? String(d.capacity) : '', - start_time: toInputDateTime(d.start_time), - end_time: toInputDateTime(d.end_time), - registration_start_date: toInputDateTime(d.registration_start_date), - registration_end_date: toInputDateTime(d.registration_end_date), - location: d.location || '', - address: d.address || '', - online_link: d.online_link || '', - description: d.description || '', - }); - } - }, [detailQuery.data]); - - const updateMutation = useMutation({ - mutationFn: (payload: EventUpdateSchema) => api.updateEvent(eventId, payload), - onSuccess: () => { - toast({ title: 'رویداد به‌روزرسانی شد', variant: 'success' }); - queryClient.invalidateQueries({ queryKey: ['admin', 'edit-event', eventId] }); - queryClient.invalidateQueries({ queryKey: ['admin', 'events'] }); - navigate(`/admin/events/${eventId}`); - }, - onError: (error) => { - toast({ - variant: 'destructive', - title: 'خطا در ذخیره‌سازی رویداد', - description: resolveErrorMessage(error), - }); - }, - }); - - React.useEffect(() => { - if (detailQuery.error) { - toast({ - variant: 'destructive', - title: 'خطا در دریافت رویداد', - description: resolveErrorMessage(detailQuery.error), - }); - } - }, [detailQuery.error, toast]); - - if (loading) { - return ( -
-

در حال بررسی دسترسی...

-
- ); - } - - if (!isAuthenticated || !(user?.is_staff || user?.is_superuser)) { - return ; - } - - return ( -
-
- - - ویرایش رویداد - فرم کامل برای ویرایش جزئیات رویداد - - - {detailQuery.isLoading ? ( -

در حال بارگذاری جزئیات...

- ) : detailQuery.data ? ( -
{ - e.preventDefault(); - updateMutation.mutate({ - title: formData.title, - status: formData.status, - event_type: formData.event_type, - price: formData.price ? Number(formData.price) * 10 : 0, - capacity: formData.capacity ? Number(formData.capacity) : null, - start_time: formData.start_time || undefined, - end_time: formData.end_time || null, - registration_start_date: formData.registration_start_date || null, - registration_end_date: formData.registration_end_date || null, - location: formData.location || null, - address: formData.address || null, - online_link: formData.online_link || null, - description: formData.description || '', - }); - }} - > -
- setFormData((p) => ({ ...p, title: e.target.value }))} - required - /> - - - setFormData((p) => ({ ...p, price: e.target.value }))} - /> - setFormData((p) => ({ ...p, capacity: e.target.value }))} - /> - setFormData((p) => ({ ...p, start_time: e.target.value }))} - /> - setFormData((p) => ({ ...p, end_time: e.target.value }))} - /> - setFormData((p) => ({ ...p, registration_start_date: e.target.value }))} - /> - setFormData((p) => ({ ...p, registration_end_date: e.target.value }))} - /> - setFormData((p) => ({ ...p, location: e.target.value }))} - /> - setFormData((p) => ({ ...p, address: e.target.value }))} - /> - setFormData((p) => ({ ...p, online_link: e.target.value }))} - /> -
-