import React, { useEffect, useRef, useState } from "react"; // next import { useRouter } from "next/router"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import workspaceService from "lib/services/workspace.service"; // hooks import useUser from "lib/hooks/useUser"; import useToast from "lib/hooks/useToast"; // icons import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; // ui import { Button, Input } from "ui"; // types import type { IWorkspace } from "types"; type Props = { isOpen: boolean; data: IWorkspace | null; onClose: () => void; }; const ConfirmWorkspaceDeletion: React.FC = ({ isOpen, data, onClose }) => { const router = useRouter(); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [selectedWorkspace, setSelectedWorkspace] = useState(null); const [confirmProjectName, setConfirmProjectName] = useState(""); const [confirmDeleteMyProject, setConfirmDeleteMyProject] = useState(false); const canDelete = confirmProjectName === data?.name && confirmDeleteMyProject; const { mutateWorkspaces } = useUser(); const { setToastAlert } = useToast(); const cancelButtonRef = useRef(null); const handleClose = () => { onClose(); setIsDeleteLoading(false); }; const handleDeletion = async () => { setIsDeleteLoading(true); if (!data || !canDelete) return; await workspaceService .deleteWorkspace(data.slug) .then(() => { handleClose(); mutateWorkspaces((prevData) => { return (prevData ?? []).filter((workspace: IWorkspace) => workspace.slug !== data.slug); }, false); setToastAlert({ type: "success", message: "Workspace deleted successfully", title: "Success", }); router.push("/"); }) .catch((error) => { console.log(error); setIsDeleteLoading(false); }); }; useEffect(() => { if (data) setSelectedWorkspace(data); else { const timer = setTimeout(() => { setSelectedWorkspace(null); clearTimeout(timer); }, 350); } }, [data]); 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:

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

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

{ if (e.target.value === "delete my workspace") { setConfirmDeleteMyProject(true); } else { setConfirmDeleteMyProject(false); } }} name="typeDelete" />
); }; export default ConfirmWorkspaceDeletion;