import React, { useEffect } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; import { Controller, useForm } from "react-hook-form"; // services import { IssueService } from "services/issue"; // fetch keys import { ISSUE_DETAILS } from "constants/fetch-keys"; // hooks import useToast from "hooks/use-toast"; // ui import { Button, Input } from "@plane/ui"; // types import type { linkDetails, IIssueLink } from "types"; type Props = { isOpen: boolean; data?: linkDetails; links?: linkDetails[]; onSuccess: () => void; }; const issueService = new IssueService(); export const CreateUpdateLinkForm: React.FC = (props) => { const { isOpen, data, links, onSuccess } = props; const router = useRouter(); const { workspaceSlug, projectId, issueId } = router.query; const { setToastAlert } = useToast(); const { handleSubmit, control, reset, formState: { errors, isSubmitting }, } = useForm({ defaultValues: { title: "", url: "", }, }); useEffect(() => { if (!data) return; reset({ title: data.title, url: data.url, }); }, [data, reset]); useEffect(() => { if (!isOpen) reset({ title: "", url: "", }); }, [isOpen, reset]); const onSubmit = async (formData: IIssueLink) => { if (!workspaceSlug || !projectId || !issueId) return; const payload = { metadata: {}, ...formData }; if (!data) await issueService .createIssueLink(workspaceSlug.toString(), projectId.toString(), issueId.toString(), payload) .then(() => { onSuccess(); mutate(ISSUE_DETAILS(issueId.toString())); }) .catch((err) => { if (err?.status === 400) setToastAlert({ type: "error", title: "Error!", message: "This URL already exists for this issue.", }); else setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again.", }); }); else { const updatedLinks = links?.map((l) => l.id === data.id ? { ...l, title: formData.title, url: formData.url, } : l ); mutate(ISSUE_DETAILS(issueId.toString()), (prevData: any) => ({ ...prevData, issue_link: updatedLinks }), false); await issueService .updateIssueLink(workspaceSlug.toString(), projectId.toString(), issueId.toString(), data!.id, payload) .then(() => { onSuccess(); mutate(ISSUE_DETAILS(issueId.toString())); }); } }; return (
( <> )} />
( <> )} />
); };