import React, { useEffect } from "react"; // react-hook-form import { useForm } from "react-hook-form"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // ui import { Input, PrimaryButton, SecondaryButton } from "components/ui"; // types import type { IIssueLink, linkDetails, ModuleLink } from "types"; type Props = { isOpen: boolean; handleClose: () => void; data?: linkDetails | null; status: boolean; createIssueLink: (formData: IIssueLink | ModuleLink) => Promise; updateIssueLink: (formData: IIssueLink | ModuleLink, linkId: string) => Promise; }; const defaultValues: IIssueLink | ModuleLink = { title: "", url: "", }; export const LinkModal: React.FC = ({ isOpen, handleClose, createIssueLink, updateIssueLink, status, data, }) => { const { register, formState: { errors, isSubmitting }, handleSubmit, reset, } = useForm({ defaultValues, }); const onClose = () => { handleClose(); const timeout = setTimeout(() => { reset(defaultValues); clearTimeout(timeout); }, 500); }; const handleFormSubmit = async (formData: IIssueLink | ModuleLink) => { if (!data) await createIssueLink({ title: formData.title, url: formData.url }); else await updateIssueLink({ title: formData.title, url: formData.url }, data.id); onClose(); }; const handleCreateUpdatePage = async (formData: IIssueLink | ModuleLink) => { await handleFormSubmit(formData); reset({ ...defaultValues, }); }; useEffect(() => { reset({ ...defaultValues, ...data, }); }, [data, reset]); return (
{status ? "Update Link" : "Add Link"}
Cancel {status ? isSubmitting ? "Updating Link..." : "Update Link" : isSubmitting ? "Adding Link..." : "Add Link"}
); };