import React, { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { mutate } from "swr"; // react-hook-form import { Controller, useForm } from "react-hook-form"; // icons import { CalendarDaysIcon, ChartPieIcon, LinkIcon, PlusIcon, Squares2X2Icon, TrashIcon, } from "@heroicons/react/24/outline"; import { Popover, Transition } from "@headlessui/react"; import DatePicker from "react-datepicker"; // services import modulesService from "services/modules.service"; // hooks import useToast from "hooks/use-toast"; // components import { LinkModal, LinksList, SidebarProgressStats } from "components/core"; import { DeleteModuleModal, SidebarLeadSelect, SidebarMembersSelect } from "components/modules"; import ProgressChart from "components/core/sidebar/progress-chart"; // components // ui import { CustomSelect, Loader, ProgressBar } from "components/ui"; // helpers import { renderDateFormat, renderShortNumericDateFormat, timeAgo } from "helpers/date-time.helper"; import { copyTextToClipboard } from "helpers/string.helper"; import { groupBy } from "helpers/array.helper"; // types import { IIssue, IModule, ModuleIssueResponse, ModuleLink, UserAuth } from "types"; // fetch-keys import { MODULE_DETAILS } from "constants/fetch-keys"; // constant import { MODULE_STATUS } from "constants/module"; const defaultValues: Partial = { lead: "", members_list: [], start_date: null, target_date: null, status: null, }; type Props = { issues: IIssue[]; module?: IModule; isOpen: boolean; moduleIssues: ModuleIssueResponse[] | undefined; userAuth: UserAuth; }; export const ModuleDetailsSidebar: React.FC = ({ issues, module, isOpen, moduleIssues, userAuth, }) => { const [moduleDeleteModal, setModuleDeleteModal] = useState(false); const [moduleLinkModal, setModuleLinkModal] = useState(false); const [startDateRange, setStartDateRange] = useState(new Date()); const [endDateRange, setEndDateRange] = useState(null); const router = useRouter(); const { workspaceSlug, projectId, moduleId } = router.query; const { setToastAlert } = useToast(); const { reset, watch, control } = useForm({ defaultValues, }); const groupedIssues = { backlog: [], unstarted: [], started: [], cancelled: [], completed: [], ...groupBy(moduleIssues ?? [], "issue_detail.state_detail.group"), }; const submitChanges = (data: Partial) => { if (!workspaceSlug || !projectId || !moduleId) return; mutate( MODULE_DETAILS(moduleId as string), (prevData) => ({ ...(prevData as IModule), ...data, }), false ); modulesService .patchModule(workspaceSlug as string, projectId as string, moduleId as string, data) .then((res) => { console.log(res); mutate(MODULE_DETAILS(moduleId as string)); }) .catch((e) => { console.log(e); }); }; const handleCreateLink = async (formData: ModuleLink) => { if (!workspaceSlug || !projectId || !moduleId) return; const previousLinks = module?.link_module.map((l) => ({ title: l.title, url: l.url })); const payload: Partial = { links_list: [...(previousLinks ?? []), formData], }; await modulesService .patchModule(workspaceSlug as string, projectId as string, moduleId as string, payload) .then((res) => { mutate(MODULE_DETAILS(moduleId as string)); }) .catch((err) => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't create the link. Please try again.", }); }); }; const handleDeleteLink = (linkId: string) => { if (!module) return; const updatedLinks = module.link_module.filter((l) => l.id !== linkId); submitChanges({ links_list: updatedLinks }); }; useEffect(() => { if (module) reset({ ...module, members_list: module.members_list ?? module.members_detail?.map((m) => m.id), }); }, [module, reset]); const isStartValid = new Date(`${module?.start_date}`) <= new Date(); const isEndValid = new Date(`${module?.target_date}`) >= new Date(`${module?.start_date}`); return ( <> setModuleLinkModal(false)} onFormSubmit={handleCreateLink} />
{module ? ( <>
( {watch("status")} } value={value} onChange={(value: any) => { submitChanges({ status: value }); }} > {MODULE_STATUS.map((option) => ( {option.label} ))} )} />
{({ open }) => ( <> {renderShortNumericDateFormat(`${module?.start_date}`) ? renderShortNumericDateFormat(`${module?.start_date}`) : "N/A"} { submitChanges({ start_date: renderDateFormat(date), }); setStartDateRange(date); }} selectsStart startDate={startDateRange} endDate={endDateRange} inline /> )} {({ open }) => ( <> -{" "} {renderShortNumericDateFormat(`${module?.target_date}`) ? renderShortNumericDateFormat(`${module?.target_date}`) : "N/A"} { submitChanges({ target_date: renderDateFormat(date), }); setEndDateRange(date); }} selectsEnd startDate={startDateRange} endDate={endDateRange} minDate={startDateRange} inline /> )}

{module.name}

Progress

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

Links

{module.link_module && module.link_module.length > 0 ? ( ) : null}
{isStartValid && isEndValid ? ( ) : ( "" )} {issues.length > 0 ? ( ) : ( "" )}
) : (
)}
); };