import React, { useState } from "react"; import { useRouter } from "next/router"; import { 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"; // hooks import useToast from "hooks/use-toast"; // ui import { DangerButton, SecondaryButton } from "components/ui"; // types import type { IState, IStateResponse } from "types"; // fetch-keys import { STATES_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 router = useRouter(); const { workspaceSlug } = router.query; const { setToastAlert } = useToast(); const handleClose = () => { onClose(); setIsDeleteLoading(false); }; const handleDeletion = async () => { if (!workspaceSlug || !data) return; setIsDeleteLoading(true); await stateServices .deleteState(workspaceSlug as string, data.project, data.id) .then(() => { mutate( STATES_LIST(data.project), (prevData) => { if (!prevData) return prevData; const stateGroup = [...prevData[data.group]].filter((s) => s.id !== data.id); return { ...prevData, [data.group]: stateGroup, }; }, false ); handleClose(); }) .catch((err) => { setIsDeleteLoading(false); if (err.status === 400) setToastAlert({ type: "error", title: "Error!", message: "This state contains some issues within it, please move them to some other state to delete this state.", }); else setToastAlert({ type: "error", title: "Error!", message: "State could not be deleted. Please try again.", }); }); }; 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.

Cancel {isDeleteLoading ? "Deleting..." : "Delete"}
); };