import React, { useState } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // services import cyclesService from "services/cycles.service"; // hooks import useToast from "hooks/use-toast"; import useUserAuth from "hooks/use-user-auth"; import useLocalStorage from "hooks/use-local-storage"; // components import { CreateUpdateCycleModal, CyclesListGanttChartView, DeleteCycleModal, SingleCycleCard, SingleCycleList, } from "components/cycles"; // ui import { Loader } from "components/ui"; // helpers import { getDateRangeStatus } from "helpers/date-time.helper"; // types import { ICycle } from "types"; // fetch-keys import { COMPLETED_CYCLES_LIST, CURRENT_CYCLE_LIST, CYCLES_LIST, DRAFT_CYCLES_LIST, UPCOMING_CYCLES_LIST, } from "constants/fetch-keys"; type Props = { cycles: ICycle[] | undefined; viewType: string | null; }; export const CyclesView: React.FC = ({ cycles, viewType }) => { const [createUpdateCycleModal, setCreateUpdateCycleModal] = useState(false); const [selectedCycleToUpdate, setSelectedCycleToUpdate] = useState(null); const [deleteCycleModal, setDeleteCycleModal] = useState(false); const [selectedCycleToDelete, setSelectedCycleToDelete] = useState(null); const { storedValue: cycleTab } = useLocalStorage("cycleTab", "All"); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { user } = useUserAuth(); const { setToastAlert } = useToast(); const handleEditCycle = (cycle: ICycle) => { setSelectedCycleToUpdate(cycle); setCreateUpdateCycleModal(true); }; const handleDeleteCycle = (cycle: ICycle) => { setSelectedCycleToDelete(cycle); setDeleteCycleModal(true); }; const handleAddToFavorites = (cycle: ICycle) => { if (!workspaceSlug || !projectId) return; const cycleStatus = getDateRangeStatus(cycle.start_date, cycle.end_date); const fetchKey = cycleStatus === "current" ? CURRENT_CYCLE_LIST(projectId as string) : cycleStatus === "upcoming" ? UPCOMING_CYCLES_LIST(projectId as string) : cycleStatus === "completed" ? COMPLETED_CYCLES_LIST(projectId as string) : DRAFT_CYCLES_LIST(projectId as string); mutate( fetchKey, (prevData) => (prevData ?? []).map((c) => ({ ...c, is_favorite: c.id === cycle.id ? true : c.is_favorite, })), false ); mutate( CYCLES_LIST(projectId as string), (prevData: any) => (prevData ?? []).map((c: any) => ({ ...c, is_favorite: c.id === cycle.id ? true : c.is_favorite, })), false ); cyclesService .addCycleToFavorites(workspaceSlug as string, projectId as string, { cycle: cycle.id, }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't add the cycle to favorites. Please try again.", }); }); }; const handleRemoveFromFavorites = (cycle: ICycle) => { if (!workspaceSlug || !projectId) return; const cycleStatus = getDateRangeStatus(cycle.start_date, cycle.end_date); const fetchKey = cycleStatus === "current" ? CURRENT_CYCLE_LIST(projectId as string) : cycleStatus === "upcoming" ? UPCOMING_CYCLES_LIST(projectId as string) : cycleStatus === "completed" ? COMPLETED_CYCLES_LIST(projectId as string) : DRAFT_CYCLES_LIST(projectId as string); mutate( fetchKey, (prevData) => (prevData ?? []).map((c) => ({ ...c, is_favorite: c.id === cycle.id ? false : c.is_favorite, })), false ); mutate( CYCLES_LIST(projectId as string), (prevData: any) => (prevData ?? []).map((c: any) => ({ ...c, is_favorite: c.id === cycle.id ? false : c.is_favorite, })), false ); cyclesService .removeCycleFromFavorites(workspaceSlug as string, projectId as string, cycle.id) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't remove the cycle from favorites. Please try again.", }); }); }; return ( <> setCreateUpdateCycleModal(false)} data={selectedCycleToUpdate} user={user} /> {cycles ? ( cycles.length > 0 ? ( viewType === "list" ? (
{cycles.map((cycle) => (
handleDeleteCycle(cycle)} handleEditCycle={() => handleEditCycle(cycle)} handleAddToFavorites={() => handleAddToFavorites(cycle)} handleRemoveFromFavorites={() => handleRemoveFromFavorites(cycle)} />
))}
) : viewType === "board" ? (
{cycles.map((cycle) => ( handleDeleteCycle(cycle)} handleEditCycle={() => handleEditCycle(cycle)} handleAddToFavorites={() => handleAddToFavorites(cycle)} handleRemoveFromFavorites={() => handleRemoveFromFavorites(cycle)} /> ))}
) : ( ) ) : (

{cycleTab === "All" ? "No cycles" : `No ${cycleTab === "Drafts" ? "draft" : cycleTab?.toLowerCase()} cycles`}

) ) : viewType === "list" ? ( ) : viewType === "board" ? ( ) : ( )} ); };