import React, { useState } from "react"; // next import { useRouter } from "next/router"; // swr import { mutate } from "swr"; // headless ui import { Dialog, Transition } from "@headlessui/react"; // services import cycleService from "services/cycles.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, ICycle } from "types"; type TConfirmCycleDeletionProps = { isOpen: boolean; setIsOpen: React.Dispatch>; data?: ICycle | null; user: ICurrentUserResponse | undefined; }; // fetch-keys import { COMPLETED_CYCLES_LIST, CURRENT_CYCLE_LIST, CYCLES_LIST, DRAFT_CYCLES_LIST, UPCOMING_CYCLES_LIST, } from "constants/fetch-keys"; import { getDateRangeStatus } from "helpers/date-time.helper"; export const DeleteCycleModal: 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 () => { if (!data || !workspaceSlug) return; setIsDeleteLoading(true); await cycleService .deleteCycle(workspaceSlug as string, data.project, data.id, user) .then(() => { const cycleType = getDateRangeStatus(data.start_date, data.end_date); const fetchKey = cycleType === "current" ? CURRENT_CYCLE_LIST(projectId as string) : cycleType === "upcoming" ? UPCOMING_CYCLES_LIST(projectId as string) : cycleType === "completed" ? COMPLETED_CYCLES_LIST(projectId as string) : DRAFT_CYCLES_LIST(projectId as string); mutate( fetchKey, (prevData) => { if (!prevData) return; return prevData.filter((cycle) => cycle.id !== data?.id); }, false ); mutate( CYCLES_LIST(projectId as string), (prevData: any) => { if (!prevData) return; return prevData.filter((cycle: any) => cycle.id !== data?.id); }, false ); handleClose(); setToastAlert({ title: "Success", type: "success", message: "Cycle deleted successfully", }); }) .catch(() => { setIsDeleteLoading(false); }); }; return (
Delete Cycle

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

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