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 projectService from "services/project.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, IProject } from "types"; // fetch-keys import { PROJECTS_LIST } from "constants/fetch-keys"; type TConfirmProjectDeletionProps = { isOpen: boolean; onClose: () => void; onSuccess?: () => void; data: IProject | null; user: ICurrentUserResponse | undefined; }; const defaultValues = { projectName: "", confirmDelete: "", }; export const DeleteProjectModal: React.FC = ({ isOpen, data, onClose, onSuccess, user, }) => { const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { control, formState: { isSubmitting }, handleSubmit, reset, watch, } = useForm({ defaultValues }); const canDelete = watch("projectName") === data?.name && watch("confirmDelete") === "delete my project"; const handleClose = () => { const timer = setTimeout(() => { reset(defaultValues); clearTimeout(timer); }, 350); onClose(); }; const onSubmit = async () => { if (!data || !workspaceSlug || !canDelete) return; await projectService .deleteProject(workspaceSlug.toString(), data.id, user) .then(() => { handleClose(); mutate( PROJECTS_LIST(workspaceSlug.toString(), { is_favorite: "all" }), (prevData) => prevData?.filter((project: IProject) => project.id !== data.id), false ); if (onSuccess) onSuccess(); if (projectId && projectId === data.id) router.push(`/${workspaceSlug}/projects`); }) .catch(() => setToastAlert({ type: "error", title: "Error!", message: "Something went wrong. Please try again later.", }) ); }; return (

Delete Project

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

Enter the project name{" "} {data?.name} to continue:

( )} />

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

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