'use client'; import { useEffect } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { Loader2 } from 'lucide-react'; import { useTranslations } from 'next-intl'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { type PagesProject, PagesService } from '@/lib/services/openflare'; import { projectQueryKey, projectsQueryKey } from './pages-utils'; import { buildProjectPayload, ProjectFormFields, toFormValues, usePagesProjectForm, } from './project-form'; interface ProjectEditorDialogProps { open: boolean; onOpenChange: (open: boolean) => void; project?: PagesProject | null; } export function ProjectEditorDialog({ open, onOpenChange, project, }: ProjectEditorDialogProps) { const t = useTranslations('pages'); const tCommon = useTranslations('common'); const queryClient = useQueryClient(); const form = usePagesProjectForm(project); useEffect(() => { if (open) form.reset(toFormValues(project)); }, [form, project, open]); const mutation = useMutation({ mutationFn: async (values: Parameters[0]) => { const payload = buildProjectPayload(values, project); return project ? PagesService.updateProject(project.id, payload) : PagesService.createProject(payload); }, onSuccess: async () => { toast.success(project ? t('updated') : t('created')); await queryClient.invalidateQueries({ queryKey: projectsQueryKey }); if (project) { await queryClient.invalidateQueries({ queryKey: projectQueryKey(project.id), }); } onOpenChange(false); }, onError: (error) => { toast.error(error instanceof Error ? error.message : t('saveFailed')); }, }); return ( {project ? t('editTitle') : t('createTitle')} {t('editorDesc')}
mutation.mutate(values))} >
); }