import { useState, FormEvent } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { toast } from 'sonner'; import api, { getErrorMessage } from '@/lib/api'; import type { Project } from '@/types'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogClose, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Select } from '@/components/ui/select'; import { Label } from '@/components/ui/label'; import { Button } from '@/components/ui/button'; interface ProjectFormProps { project: Project | null; onClose: () => void; } export default function ProjectForm({ project, onClose }: ProjectFormProps) { const queryClient = useQueryClient(); const [formData, setFormData] = useState({ name: project?.name || '', description: project?.description || '', status: project?.status || 'not_started', color: project?.color || '', due_date: project?.due_date || '', }); const mutation = useMutation({ mutationFn: async (data: typeof formData) => { if (project) { const response = await api.put(`/projects/${project.id}`, data); return response.data; } else { const response = await api.post('/projects', data); return response.data; } }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['projects'] }); if (project) { queryClient.invalidateQueries({ queryKey: ['projects', project.id.toString()] }); } toast.success(project ? 'Project updated' : 'Project created'); onClose(); }, onError: (error) => { toast.error(getErrorMessage(error, project ? 'Failed to update project' : 'Failed to create project')); }, }); const handleSubmit = (e: FormEvent) => { e.preventDefault(); mutation.mutate(formData); }; return ( {project ? 'Edit Project' : 'New Project'}
setFormData({ ...formData, name: e.target.value })} required />