import { useState } from "react" import { useTranslation } from "react-i18next" import { VisuallyHidden } from "radix-ui" import { Drawer, DrawerContent, DrawerTitle } from "@/components/ui/drawer" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { useCreateGoal, useUpdateGoal } from "@/hooks/use-goals" import { toast } from "sonner" type GoalSheetProps = { open: boolean onOpenChange: (open: boolean) => void editYear?: number editTarget?: number } export function GoalSheet({ open, onOpenChange, editYear, editTarget, }: GoalSheetProps) { const { t } = useTranslation() const isEditing = editYear !== undefined const currentYear = new Date().getFullYear() const [year, setYear] = useState(editYear ?? currentYear) const [target, setTarget] = useState(editTarget ?? 52) const createMutation = useCreateGoal() const updateMutation = useUpdateGoal() function handleClose() { onOpenChange(false) if (!isEditing) { setYear(currentYear) setTarget(52) } } function handleSubmit() { if (target < 1) return if (isEditing) { updateMutation.mutate( { year, data: { target_count: target } }, { onSuccess: () => { toast.success(t("goals.updated")) handleClose() }, }, ) } else { createMutation.mutate( { year, target_count: target }, { onSuccess: () => { toast.success(t("goals.created")) handleClose() }, }, ) } } const isPending = createMutation.isPending || updateMutation.isPending return ( {isEditing ? t("goals.editGoal") : t("goals.setGoal")}

{isEditing ? t("goals.editGoal") : t("goals.setGoal")}

setYear(Number(e.target.value))} disabled={isEditing} />
setTarget(Number(e.target.value))} />
) }