import React, { useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import pagesService from "services/pages.service"; // hooks import useToast from "hooks/use-toast"; // ui import { DangerButton, SecondaryButton } from "components/ui"; // icons import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; // types import type { ICurrentUserResponse, IPage } from "types"; // fetch-keys import { ALL_PAGES_LIST, FAVORITE_PAGES_LIST, MY_PAGES_LIST, RECENT_PAGES_LIST, } from "constants/fetch-keys"; type TConfirmPageDeletionProps = { isOpen: boolean; setIsOpen: React.Dispatch>; data?: IPage | null; user: ICurrentUserResponse | undefined; }; export const DeletePageModal: React.FC = ({ isOpen, setIsOpen, data, user, }) => { const [isDeleteLoading, setIsDeleteLoading] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const handleClose = () => { setIsOpen(false); setIsDeleteLoading(false); }; const handleDeletion = async () => { setIsDeleteLoading(true); if (!data || !workspaceSlug || !projectId) return; await pagesService .deletePage(workspaceSlug as string, data.project, data.id, user) .then(() => { mutate(RECENT_PAGES_LIST(projectId as string)); mutate( MY_PAGES_LIST(projectId as string), (prevData) => (prevData ?? []).filter((page) => page.id !== data?.id), false ); mutate( ALL_PAGES_LIST(projectId as string), (prevData) => (prevData ?? []).filter((page) => page.id !== data?.id), false ); mutate( FAVORITE_PAGES_LIST(projectId as string), (prevData) => (prevData ?? []).filter((page) => page.id !== data?.id), false ); handleClose(); setToastAlert({ type: "success", title: "Success!", message: "Page deleted successfully.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Page could not be deleted. Please try again.", }); }) .finally(() => { setIsDeleteLoading(false); }); }; return (
Delete Page

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

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