import React, { useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; import { Dialog, Transition } from "@headlessui/react"; // services import stateServices from "lib/services/state.service"; import issuesServices from "lib/services/issues.service"; // fetch api import { STATE_LIST, PROJECT_ISSUES_LIST } from "constants/fetch-keys"; // common import { groupBy } from "constants/common"; // icons import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; // ui import { Button } from "ui"; // types import type { IState } from "types"; type Props = { isOpen: boolean; onClose: () => void; data: IState | null; }; const ConfirmStateDeletion: React.FC = ({ isOpen, onClose, data }) => { const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [issuesWithThisStateExist, setIssuesWithThisStateExist] = useState(true); const router = useRouter(); const { workspaceSlug, projectId } = router.query; 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), (prevData) => prevData?.filter((state) => state.id !== data?.id), false ); handleClose(); }) .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.

)}
); }; export default ConfirmStateDeletion;