"use client" import { useState } from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import * as z from "zod" import { PushSubscriptionSettings } from "@/components/push-manager" import { Settings } from "lucide-react" import { toast } from "sonner" import { Button } from "@/components/ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Checkbox } from "@/components/ui/checkbox" import { updatePlan } from "@/app/actions/plan" interface PlanSettingsProps { planId: string initialTitle: string initialInstructions: string | null initialWebhookUrl: string | null initialNotifyAll: boolean initialFeedingPerDay: number initialFeedingInterval: number initialLitterInterval: number dict: any lang: string } export function PlanSettings({ planId, initialTitle, initialInstructions, initialWebhookUrl, initialNotifyAll, initialFeedingPerDay, initialFeedingInterval, initialLitterInterval, dict, lang }: PlanSettingsProps) { const [open, setOpen] = useState(false) const [isPending, setIsPending] = useState(false) const formSchema = z.object({ title: z.string().min(2), instructions: z.string().optional(), webhookUrl: z.string().url().optional().or(z.literal("")), notifyAll: z.boolean(), feedingPerDay: z.number().min(1).max(10), feedingInterval: z.number().min(1).max(30), litterInterval: z.number().min(1).max(30), }) type FormValues = z.infer const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { title: initialTitle, instructions: initialInstructions || "", webhookUrl: initialWebhookUrl || "", notifyAll: initialNotifyAll, feedingPerDay: initialFeedingPerDay, feedingInterval: initialFeedingInterval, litterInterval: initialLitterInterval, }, }) async function onSubmit(data: FormValues) { setIsPending(true) try { await updatePlan(planId, data, lang) toast.success(dict.success) setOpen(false) } catch (error) { toast.error(dict.error) } finally { setIsPending(false) } } return ( {dict.title} {dict.description}
( {dict.titleLabel} )} /> ( {dict.webhookLabel} {dict.webhookDesc} )} />
( {dict.feedingPerDayLabel} field.onChange(parseInt(e.target.value))} /> )} /> ( {dict.feedingIntervalLabel} field.onChange(parseInt(e.target.value))} /> )} />
( {dict.litterIntervalLabel} field.onChange(parseInt(e.target.value))} /> )} /> (
{dict.notifyAllLabel} {dict.notifyAllDesc}
)} /> ( {dict.instructionsLabel}