import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import workspaceService from "services/workspace.service"; // hooks import useToast from "hooks/use-toast"; // icons import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; // ui import { DangerButton, Input, SecondaryButton } from "components/ui"; // types import type { IWorkspace } from "types"; // fetch-keys import { USER_WORKSPACES } from "constants/fetch-keys"; type Props = { isOpen: boolean; data: IWorkspace | null; onClose: () => void; }; export const DeleteWorkspaceModal: React.FC = ({ isOpen, data, onClose }) => { const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [confirmWorkspaceName, setConfirmWorkspaceName] = useState(""); const [confirmDeleteMyWorkspace, setConfirmDeleteMyWorkspace] = useState(false); const [selectedWorkspace, setSelectedWorkspace] = useState(null); const router = useRouter(); const { setToastAlert } = useToast(); useEffect(() => { if (data) setSelectedWorkspace(data); else { const timer = setTimeout(() => { setSelectedWorkspace(null); clearTimeout(timer); }, 350); } }, [data]); const canDelete = confirmWorkspaceName === data?.name && confirmDeleteMyWorkspace; const handleClose = () => { onClose(); setIsDeleteLoading(false); }; const handleDeletion = async () => { setIsDeleteLoading(true); if (!data || !canDelete) return; await workspaceService .deleteWorkspace(data.slug) .then(() => { handleClose(); router.push("/"); mutate(USER_WORKSPACES, (prevData) => prevData?.filter((workspace) => workspace.id !== data.id) ); setToastAlert({ type: "success", message: "Workspace deleted successfully", title: "Success", }); }) .catch((error) => { console.log(error); setIsDeleteLoading(false); }); }; return (

Delete Workspace

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

Enter the workspace name{" "} {selectedWorkspace?.name}{" "} to continue:

{ setConfirmWorkspaceName(e.target.value); }} name="workspaceName" />

To confirm, type{" "} delete my workspace{" "} below:

{ if (e.target.value === "delete my workspace") { setConfirmDeleteMyWorkspace(true); } else { setConfirmDeleteMyWorkspace(false); } }} name="typeDelete" />
Cancel {isDeleteLoading ? "Deleting..." : "Delete Workspace"}
); };