import { useEffect, useState } from 'react' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/ui/components/ui/dialog' import { Button } from '@/ui/components/ui/button' import { Input } from '@/ui/components/ui/input' import { Textarea } from '@/ui/components/ui/textarea' import { Label } from '@/ui/components/ui/label' import { client } from '@/ui/api/client' import { useMutation, useQueryClient } from '@tanstack/react-query' import { toast } from 'sonner' import { m } from '@/ui/paraglide/messages.js' import type { KanbanGoalInfo } from '@shumai/dtos' import { Loader2 } from 'lucide-react' interface KanbanCreateGoalDialogProps { teamId: string isOpen: boolean onClose: () => void goalToEdit?: KanbanGoalInfo | null } export function KanbanCreateGoalDialog({ teamId, isOpen, onClose, goalToEdit, }: KanbanCreateGoalDialogProps) { const queryClient = useQueryClient() const [title, setTitle] = useState('') const [description, setDescription] = useState('') useEffect(() => { if (goalToEdit) { setTitle(goalToEdit.title) setDescription(goalToEdit.description || '') } else { setTitle('') setDescription('') } }, [goalToEdit, isOpen]) const { mutate: createGoal, isPending: isCreating } = useMutation({ mutationFn: async () => { const res = await client.api.teams[':teamId'].kanban.goals.$post({ param: { teamId }, json: { title: title.trim(), description: description.trim() || undefined, }, }) if (!res.ok) { const err = await res.json().catch(() => ({ message: m.error() })) throw new Error((err as { message?: string }).message || m.error()) } return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'goals'] }) toast.success(m.goal_created()) onClose() }, onError: (err) => { toast.error(err.message) }, }) const { mutate: updateGoal, isPending: isUpdating } = useMutation({ mutationFn: async () => { if (!goalToEdit) return const res = await client.api.teams[':teamId'].kanban.goals[':goalId'].$patch({ param: { teamId, goalId: goalToEdit.id }, json: { title: title.trim(), description: description.trim() || null, }, }) if (!res.ok) { const err = await res.json().catch(() => ({ message: m.error() })) throw new Error((err as { message?: string }).message || m.error()) } return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'goals'] }) toast.success(m.goal_updated()) onClose() }, onError: (err) => { toast.error(err.message) }, }) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (!title.trim()) return if (goalToEdit) { updateGoal() } else { createGoal() } } const isLoading = isCreating || isUpdating return ( !open && onClose()}>
{goalToEdit ? m.edit_goal() : m.create_goal()} {goalToEdit ? m.edit_goal() : m.create_first_goal()}
setTitle(e.target.value)} placeholder={m.goal_title_placeholder()} required autoFocus />