import { FC, useEffect, Fragment } from "react"; import { Controller, useForm } from "react-hook-form"; import { Dialog, Transition } from "@headlessui/react"; // ui import { Button, Input } from "@plane/ui"; // types import type { TIssueLinkEditableFields } from "@plane/types"; import { TLinkOperations } from "./root"; export type TLinkOperationsModal = Exclude; export type TIssueLinkCreateFormFieldOptions = TIssueLinkEditableFields & { id?: string; }; export type TIssueLinkCreateEditModal = { isModalOpen: boolean; handleModal: (modalToggle: boolean) => void; linkOperations: TLinkOperationsModal; preloadedData?: TIssueLinkCreateFormFieldOptions | null; }; const defaultValues: TIssueLinkCreateFormFieldOptions = { title: "", url: "", }; export const IssueLinkCreateUpdateModal: FC = (props) => { // props const { isModalOpen, handleModal, linkOperations, preloadedData } = props; // react hook form const { formState: { errors, isSubmitting }, handleSubmit, control, reset, } = useForm({ defaultValues, }); const onClose = () => { handleModal(false); const timeout = setTimeout(() => { reset(defaultValues); clearTimeout(timeout); }, 500); }; const handleFormSubmit = async (formData: TIssueLinkCreateFormFieldOptions) => { if (!formData || !formData.id) await linkOperations.create({ title: formData.title, url: formData.url }); else await linkOperations.update(formData.id as string, { title: formData.title, url: formData.url }); onClose(); }; useEffect(() => { reset({ ...defaultValues, ...preloadedData }); }, [preloadedData, reset]); return (
{preloadedData?.id ? "Update Link" : "Add Link"}
( )} />
( )} />
); };