import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import Image from "next/image"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; import { Popover, Transition } from "@headlessui/react"; import DatePicker from "react-datepicker"; // icons import { CalendarDaysIcon, ChartPieIcon, LinkIcon, Squares2X2Icon, TrashIcon, UserIcon, } from "@heroicons/react/24/outline"; // ui import { CustomSelect, Loader, ProgressBar } from "components/ui"; // hooks import useToast from "hooks/use-toast"; // services import cyclesService from "services/cycles.service"; // components import { SidebarProgressStats } from "components/core"; import ProgressChart from "components/core/sidebar/progress-chart"; import { DeleteCycleModal } from "components/cycles"; // helpers import { copyTextToClipboard } from "helpers/string.helper"; import { groupBy } from "helpers/array.helper"; import { renderDateFormat, renderShortNumericDateFormat } from "helpers/date-time.helper"; // types import { CycleIssueResponse, ICycle, IIssue } from "types"; // fetch-keys import { CYCLE_DETAILS } from "constants/fetch-keys"; // constants import { CYCLE_STATUS } from "constants/cycle"; type Props = { issues: IIssue[]; cycle: ICycle | undefined; isOpen: boolean; cycleIssues: CycleIssueResponse[]; }; export const CycleDetailsSidebar: React.FC = ({ issues, cycle, isOpen, cycleIssues }) => { const [cycleDeleteModal, setCycleDeleteModal] = useState(false); const router = useRouter(); const { workspaceSlug, projectId, cycleId } = router.query; const [startDateRange, setStartDateRange] = useState(new Date()); const [endDateRange, setEndDateRange] = useState(null); const { setToastAlert } = useToast(); const defaultValues: Partial = { start_date: new Date().toString(), end_date: new Date().toString(), status: cycle?.status, }; const groupedIssues = { backlog: [], unstarted: [], started: [], cancelled: [], completed: [], ...groupBy(cycleIssues ?? [], "issue_detail.state_detail.group"), }; const { reset, watch, control } = useForm({ defaultValues, }); const submitChanges = (data: Partial) => { if (!workspaceSlug || !projectId || !cycleId) return; mutate( CYCLE_DETAILS(cycleId as string), (prevData) => ({ ...(prevData as ICycle), ...data }), false ); cyclesService .patchCycle(workspaceSlug as string, projectId as string, cycleId as string, data) .then((res) => { console.log(res); mutate(CYCLE_DETAILS(cycleId as string)); }) .catch((e) => { console.log(e); }); }; useEffect(() => { if (cycle) reset({ ...cycle, }); }, [cycle, reset]); const isStartValid = new Date(`${cycle?.start_date}`) <= new Date(); const isEndValid = new Date(`${cycle?.end_date}`) >= new Date(`${cycle?.start_date}`); return ( <>
{cycle ? ( <>
( {watch("status")} } value={value} onChange={(value: any) => { submitChanges({ status: value }); }} > {CYCLE_STATUS.map((option) => ( {option.label} ))} )} />
{({ open }) => ( <> {renderShortNumericDateFormat(`${cycle.start_date}`) ? renderShortNumericDateFormat(`${cycle.start_date}`) : "N/A"} { submitChanges({ start_date: renderDateFormat(date), }); setStartDateRange(date); }} selectsStart startDate={startDateRange} endDate={endDateRange} inline /> )} {({ open }) => ( <> -{" "} {renderShortNumericDateFormat(`${cycle.end_date}`) ? renderShortNumericDateFormat(`${cycle.end_date}`) : "N/A"} { submitChanges({ end_date: renderDateFormat(date), }); setEndDateRange(date); }} selectsEnd startDate={startDateRange} endDate={endDateRange} minDate={startDateRange} inline /> )}

{cycle.name}

Owned by

{cycle.owned_by && (cycle.owned_by.avatar && cycle.owned_by.avatar !== "" ? (
{cycle.owned_by?.first_name}
) : (
{cycle.owned_by?.first_name && cycle.owned_by.first_name !== "" ? cycle.owned_by.first_name.charAt(0) : cycle.owned_by?.email.charAt(0)}
))} {cycle.owned_by.first_name !== "" ? cycle.owned_by.first_name : cycle.owned_by.email}

Progress

{groupedIssues.completed.length}/{cycleIssues?.length}
{isStartValid && isEndValid ? (
) : ( "" )} {issues.length > 0 ? ( ) : ( "" )}
) : (
)}
); };