import React, { useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // icons import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; // services import stateServices from "services/state.service"; import issuesServices from "services/issues.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Button } from "components/ui"; // helpers import { groupBy } from "helpers/array.helper"; // types import type { IState } from "types"; // fetch-keys import { STATE_LIST, PROJECT_ISSUES_LIST } from "constants/fetch-keys"; type Props = { isOpen: boolean; onClose: () => void; data: IState | null; }; export const DeleteStateModal: React.FC = ({ isOpen, onClose, data }) => { const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [issuesWithThisStateExist, setIssuesWithThisStateExist] = useState(true); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { data: issues } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string) : null, workspaceSlug && projectId ? () => issuesServices.getIssues(workspaceSlug as string, projectId as string) : null ); const cancelButtonRef = useRef(null); const handleClose = () => { onClose(); setIsDeleteLoading(false); }; const handleDeletion = async () => { setIsDeleteLoading(true); if (!data || !workspaceSlug || issuesWithThisStateExist) return; await stateServices .deleteState(workspaceSlug as string, data.project, data.id) .then(() => { mutate(STATE_LIST(data.project)); handleClose(); setToastAlert({ title: "Success", type: "success", message: "State deleted successfully", }); }) .catch((error) => { console.log(error); setIsDeleteLoading(false); }); }; const groupedIssues = groupBy(issues?.results ?? [], "state"); useEffect(() => { if (data) setIssuesWithThisStateExist(!!groupedIssues[data.id]); }, [groupedIssues, data]); return (
Delete State

Are you sure you want to delete state - {`"`} {data?.name} {`"`} ? All of the data related to the state will be permanently removed. This action cannot be undone.

{issuesWithThisStateExist && (

There are issues with this state. Please move them to another state before deleting this state.

)}
); };