"use client"; import { zodResolver } from "@hookform/resolvers/zod"; import { Dispatch, SetStateAction } from "react"; import { useForm } from "react-hook-form"; import * as z from "zod"; import { updateScan } from "@/actions/scans"; import { SaveIcon } from "@/components/icons"; import { useToast } from "@/components/ui"; import { CustomButton, CustomInput } from "@/components/ui/custom"; import { Form } from "@/components/ui/form"; import { editScanFormSchema } from "@/types"; export const EditScanForm = ({ scanId, scanName, setIsOpen, }: { scanId: string; scanName: string; setIsOpen: Dispatch>; }) => { const formSchema = editScanFormSchema(scanName); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { scanId: scanId, scanName: scanName, }, }); const { toast } = useToast(); const isLoading = form.formState.isSubmitting; const onSubmitClient = async (values: z.infer) => { const formData = new FormData(); Object.entries(values).forEach( ([key, value]) => value !== undefined && formData.append(key, value), ); const data = await updateScan(formData); if (data?.errors && data.errors.length > 0) { const error = data.errors[0]; const errorMessage = `${error.detail}`; // show error toast({ variant: "destructive", title: "Oops! Something went wrong", description: errorMessage, }); } else { toast({ title: "Success!", description: "The scan was updated successfully.", }); setIsOpen(false); // Close the modal on success } }; return (
Current name: {scanName}
setIsOpen(false)} isDisabled={isLoading} > Cancel } > {isLoading ? <>Loading : Save}
); };