import { useEffect } from "react"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // icons import { CalendarDaysIcon, ChartPieIcon, LinkIcon, UserIcon } from "@heroicons/react/24/outline"; // services import cyclesService from "services/cycles.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Loader, CustomDatePicker } from "components/ui"; //progress-bar import { CircularProgressbar } from "react-circular-progressbar"; import "react-circular-progressbar/dist/styles.css"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; import { groupBy } from "helpers/array.helper"; // types import { CycleIssueResponse, ICycle } from "types"; // fetch-keys import { CYCLE_LIST } from "constants/fetch-keys"; type Props = { cycle: ICycle | undefined; isOpen: boolean; cycleIssues: CycleIssueResponse[]; }; const defaultValues: Partial = { start_date: new Date().toString(), end_date: new Date().toString(), }; const CycleDetailSidebar: React.FC = ({ cycle, isOpen, cycleIssues }) => { const router = useRouter(); const { workspaceSlug, projectId, cycleId } = router.query; const { setToastAlert } = useToast(); const { reset, control } = useForm({ defaultValues, }); const groupedIssues = { backlog: [], unstarted: [], started: [], cancelled: [], completed: [], ...groupBy(cycleIssues ?? [], "issue_detail.state_detail.group"), }; const submitChanges = (data: Partial) => { if (!workspaceSlug || !projectId || !module) return; mutate( projectId && CYCLE_LIST(projectId as string), (prevData) => (prevData ?? []).map((tempCycle) => { if (tempCycle.id === cycleId) return { ...tempCycle, ...data }; return tempCycle; }), false ); cyclesService .patchCycle(workspaceSlug as string, projectId as string, cycle?.id ?? "", data) .then((res) => { console.log(res); mutate(CYCLE_LIST(projectId as string)); }) .catch((e) => { console.log(e); }); }; useEffect(() => { if (cycle) reset({ ...cycle, }); }, [cycle, reset]); return (
{cycle ? ( <>

{cycle.name}

Owned by

{cycle.owned_by.first_name !== "" ? ( <> {cycle.owned_by.first_name} {cycle.owned_by.last_name} ) : ( cycle.owned_by.email )}

Progress

{groupedIssues.completed.length}/{cycleIssues?.length}

Start date

( { submitChanges({ start_date: val ? `${val.getFullYear()}-${val.getMonth() + 1}-${val.getDate()}` : null, }); }} isClearable={false} /> )} />

End date

( { submitChanges({ end_date: val ? `${val.getFullYear()}-${val.getMonth() + 1}-${val.getDate()}` : null, }); }} isClearable={false} /> )} />
) : (
)}
); }; export default CycleDetailSidebar;