import { FC, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // headless ui import { Disclosure, Transition } from "@headlessui/react"; // services import issuesService from "services/issues.service"; // components import { CreateUpdateIssueModal, SubIssuesListModal } from "components/issues"; // ui import { CustomMenu } from "components/ui"; // icons import { ChevronRightIcon, PlusIcon } from "@heroicons/react/24/outline"; // types import { IIssue, IssueResponse, UserAuth } from "types"; // fetch-keys import { PROJECT_ISSUES_LIST, SUB_ISSUES } from "constants/fetch-keys"; type SubIssueListProps = { parentIssue: IIssue; userAuth: UserAuth; }; export const SubIssuesList: FC = ({ parentIssue, userAuth }) => { // states const [createIssueModal, setCreateIssueModal] = useState(false); const [subIssuesListModal, setSubIssuesListModal] = useState(false); const [preloadedData, setPreloadedData] = useState | null>(null); const router = useRouter(); const { workspaceSlug, projectId, issueId } = router.query; const { data: subIssues } = useSWR( workspaceSlug && projectId && issueId ? SUB_ISSUES(issueId as string) : null, workspaceSlug && projectId && issueId ? () => issuesService.subIssues(workspaceSlug as string, projectId as string, issueId as string) : null ); const handleSubIssueRemove = (issueId: string) => { if (!workspaceSlug || !projectId) return; mutate( SUB_ISSUES(parentIssue.id ?? ""), (prevData) => prevData?.filter((i) => i.id !== issueId), false ); issuesService .patchIssue(workspaceSlug as string, projectId as string, issueId, { parent: null }) .then((res) => { mutate(SUB_ISSUES(parentIssue.id ?? "")); mutate( PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string), (prevData) => ({ ...(prevData as IssueResponse), results: (prevData?.results ?? []).map((p) => { if (p.id === res.id) return { ...p, ...res, }; return p; }), }), false ); }) .catch((e) => { console.error(e); }); }; const handleCreateIssueModal = () => { setCreateIssueModal(true); setPreloadedData({ parent: parentIssue.id, }); }; const isNotAllowed = userAuth.isGuest || userAuth.isViewer; return ( <> setCreateIssueModal(false)} /> setSubIssuesListModal(false)} parent={parentIssue} /> {subIssues && subIssues.length > 0 ? ( {({ open }) => ( <>
Sub-issues {subIssues.length} {open && !isNotAllowed ? (
setSubIssuesListModal(true)}> Add an existing issue
) : null}
{subIssues.map((issue) => (
{issue.project_detail.identifier}-{issue.sequence_id} {issue.name} {!isNotAllowed && (
handleSubIssueRemove(issue.id)}> Remove as sub-issue
)}
))}
)}
) : ( !isNotAllowed && ( Add sub-issue } optionsPosition="left" noBorder > Create new setSubIssuesListModal(true)}> Add an existing issue ) )} ); };