import React from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // 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 { ICurrentUserResponse, IWorkspace } from "types"; // fetch-keys import { USER_WORKSPACES } from "constants/fetch-keys"; type Props = { isOpen: boolean; data: IWorkspace | null; onClose: () => void; user: ICurrentUserResponse | undefined; }; const defaultValues = { workspaceName: "", confirmDelete: "", }; export const DeleteWorkspaceModal: React.FC = ({ isOpen, data, onClose, user }) => { const router = useRouter(); const { setToastAlert } = useToast(); const { control, formState: { isSubmitting }, handleSubmit, reset, watch, } = useForm({ defaultValues }); const canDelete = watch("workspaceName") === data?.name && watch("confirmDelete") === "delete my workspace"; const handleClose = () => { const timer = setTimeout(() => { reset(defaultValues); clearTimeout(timer); }, 350); onClose(); }; const onSubmit = async () => { if (!data || !canDelete) return; await workspaceService .deleteWorkspace(data.slug, user) .then(() => { handleClose(); router.push("/"); mutate( USER_WORKSPACES, (prevData) => prevData?.filter((workspace) => workspace.id !== data.id) ); setToastAlert({ type: "success", title: "Success!", message: "Workspace deleted successfully.", }); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again later.", }) ); }; 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{" "} {data?.name} to continue:

( )} />

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

( )} />
Cancel {isSubmitting ? "Deleting..." : "Delete Workspace"}
); };