"use client" import { z } from "zod" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { Plus, Save } from "lucide-react" import { Button } from "@/shared/components/ui/button" import { FieldGroup } from "@/shared/components/ui/field" import { Rhform, RhfTextField } from "@/shared/components/form" import { toast } from "sonner" import { useAuthApi } from "@/shared/useApi" import { useEffect } from "react" // ── Schema ── const paymentTermSchema = z.object({ name: z.string().min(1, "Name is required"), }) type PaymentTermFormValues = z.infer // ── Props ── type PaymentTermFormProps = { resourceId?: string | null initialData?: any onSuccess?: () => void } // ── Component ── export function PaymentTermForm({ resourceId, initialData, onSuccess }: PaymentTermFormProps) { const api = useAuthApi() const isEditing = !!resourceId const form = useForm({ resolver: zodResolver(paymentTermSchema), defaultValues: { name: "" }, }) useEffect(() => { if (initialData) { const d = initialData?.data ?? initialData form.reset({ name: d.title ?? "" }) } }, [initialData, form]) const handleSubmit = async (values: PaymentTermFormValues) => { try { const promise = isEditing ? api.paymentTerms.update(resourceId!, { title: values.name } as any) : api.paymentTerms.create({ title: values.name } as any) toast.promise(promise, { loading: isEditing ? "Updating..." : "Creating...", success: isEditing ? "Updated successfully" : "Created successfully", error: isEditing ? "Failed to update" : "Failed to create", }) await promise form.reset() onSuccess?.() } catch { // toast already shown } } return ( ) }