import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import Image from "next/image"; import useSWR, { mutate } from "swr"; // toast import useToast from "hooks/use-toast"; // components import { DeleteModuleModal } from "components/modules"; // ui import { AssigneesList, Avatar, CustomMenu, Tooltip } from "components/ui"; // icons import User from "public/user.png"; import { CalendarDaysIcon, StarIcon, UserCircleIcon, UserGroupIcon, } from "@heroicons/react/24/outline"; // helpers import { renderShortDateWithYearFormat, renderShortNumericDateFormat, } from "helpers/date-time.helper"; // services import stateService from "services/state.service"; import modulesService from "services/modules.service"; // types import { IModule } from "types"; // fetch-key import { MODULE_LIST, STATE_LIST } from "constants/fetch-keys"; // helper import { copyTextToClipboard, truncateText } from "helpers/string.helper"; import { getStatesList } from "helpers/state.helper"; type Props = { module: IModule; handleEditModule: () => void; }; const stateGroupColours: { [key: string]: string; } = { backlog: "#3f76ff", unstarted: "#ff9e9e", started: "#d687ff", cancelled: "#ff5353", completed: "#096e8d", }; export const SingleModuleCard: React.FC = ({ module, handleEditModule }) => { const [moduleDeleteModal, setModuleDeleteModal] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const handleDeleteModule = () => { if (!module) return; setModuleDeleteModal(true); }; const { data: stateGroups } = useSWR( workspaceSlug && module.project_detail.id ? STATE_LIST(module.project_detail.id) : null, workspaceSlug && module.project_detail.id ? () => stateService.getStates(workspaceSlug as string, module.project_detail.id) : null ); const states = getStatesList(stateGroups ?? {}); const selectedOption = states?.find( (s) => s.name.replace(" ", "-").toLowerCase() === module.status?.replace(" ", "-").toLowerCase() ); const handleAddToFavorites = () => { if (!workspaceSlug && !projectId && !module) return; modulesService .addModuleToFavorites(workspaceSlug as string, projectId as string, { module: module.id, }) .then(() => { mutate( MODULE_LIST(projectId as string), (prevData) => (prevData ?? []).map((m) => ({ ...m, is_favorite: m.id === module.id ? true : m.is_favorite, })), false ); setToastAlert({ type: "success", title: "Success!", message: "Successfully added the module to favorites.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't add the module to favorites. Please try again.", }); }); }; const handleRemoveFromFavorites = () => { if (!workspaceSlug || !module) return; modulesService .removeModuleFromFavorites(workspaceSlug as string, projectId as string, module.id) .then(() => { mutate( MODULE_LIST(projectId as string), (prevData) => (prevData ?? []).map((m) => ({ ...m, is_favorite: m.id === module.id ? false : m.is_favorite, })), false ); setToastAlert({ type: "success", title: "Success!", message: "Successfully removed the module from favorites.", }); }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't remove the module from favorites. Please try again.", }); }); }; const handleCopyText = () => { const originURL = typeof window !== "undefined" && window.location.origin ? window.location.origin : ""; copyTextToClipboard( `${originURL}/${workspaceSlug}/projects/${projectId}/modules/${module.id}` ).then(() => { setToastAlert({ type: "success", title: "Link Copied!", message: "Module link copied to clipboard.", }); }); }; const endDate = new Date(module.target_date ?? ""); const startDate = new Date(module.start_date ?? ""); return ( <>
{truncateText(module.name, 75)}
Start : {renderShortDateWithYearFormat(startDate)}
End : {renderShortDateWithYearFormat(endDate)}
Lead :
{module.lead_detail ? (
{module.lead_detail.first_name}
) : (
N/A N/A
)}
Members
{module.members_detail && module.members_detail.length > 0 ? ( ) : (
N/A N/A
)}
{module?.status?.replace("-", " ")}
{module.is_favorite ? ( ) : ( )} Edit module Delete module Copy module link
); };