import React, { useEffect, useRef, useState } from "react"; // swr import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import stateServices from "lib/services/state.service"; // fetch api import { STATE_LIST } from "constants/fetch-keys"; // hooks import useUser from "lib/hooks/useUser"; // 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 { activeWorkspace, issues } = useUser(); const cancelButtonRef = useRef(null); const handleClose = () => { onClose(); setIsDeleteLoading(false); }; const handleDeletion = async () => { setIsDeleteLoading(true); if (!data || !activeWorkspace || issuesWithThisStateExist) return; await stateServices .deleteState(activeWorkspace.slug, 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;