import React, { useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import issueServices from "services/issues.service"; // hooks import useToast from "hooks/use-toast"; // icons import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; // ui import { Button } from "components/ui"; // types import type { CycleIssueResponse, IIssue, IssueResponse, ModuleIssueResponse } from "types"; // fetch-keys import { CYCLE_ISSUES, PROJECT_ISSUES_LIST, MODULE_ISSUES, USER_ISSUE } from "constants/fetch-keys"; type Props = { isOpen: boolean; handleClose: () => void; data: IIssue | null; }; export const DeleteIssueModal: React.FC = ({ isOpen, handleClose, data }) => { const cancelButtonRef = useRef(null); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const router = useRouter(); const { workspaceSlug, projectId: queryProjectId } = router.query; const { setToastAlert } = useToast(); useEffect(() => { setIsDeleteLoading(false); }, [isOpen]); const onClose = () => { setIsDeleteLoading(false); handleClose(); }; const handleDeletion = async () => { setIsDeleteLoading(true); if (!data || !workspaceSlug) return; const projectId = data.project; await issueServices .deleteIssue(workspaceSlug as string, projectId, data.id) .then(() => { const cycleId = data?.cycle; const moduleId = data?.module; if (cycleId) { mutate( CYCLE_ISSUES(cycleId), (prevData) => prevData?.filter((i) => i.issue !== data.id), false ); } if (moduleId) { mutate( MODULE_ISSUES(moduleId), (prevData) => prevData?.filter((i) => i.issue !== data.id), false ); } if (!queryProjectId) mutate( USER_ISSUE(workspaceSlug as string), (prevData) => prevData?.filter((i) => i.id !== data.id), false ); mutate( PROJECT_ISSUES_LIST(workspaceSlug as string, projectId), (prevData) => ({ ...(prevData as IssueResponse), results: prevData?.results.filter((i) => i.id !== data.id) ?? [], count: (prevData?.count as number) - 1, }), false ); handleClose(); setToastAlert({ title: "Success", type: "success", message: "Issue deleted successfully", }); }) .catch((error) => { console.log(error); setIsDeleteLoading(false); }); }; return (
Are you sure you want to delete {`"`} {data?.project_detail.identifier}-{data?.sequence_id} - {data?.name}?{`"`}

All of the data related to the issue will be permanently removed. This action cannot be undone.

); };