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"; // services import modulesService from "services/modules.service"; // hooks 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, DocumentDuplicateIcon, PencilIcon, StarIcon, TrashIcon, UserCircleIcon, UserGroupIcon, } from "@heroicons/react/24/outline"; // helpers import { copyTextToClipboard, truncateText } from "helpers/string.helper"; import { renderShortDateWithYearFormat } from "helpers/date-time.helper"; // types import { IModule } from "types"; // fetch-key import { MODULE_ISSUES, MODULE_LIST } from "constants/fetch-keys"; type Props = { module: IModule; handleEditModule: () => void; }; export const SingleModuleCard: React.FC = ({ module, handleEditModule }) => { const [moduleDeleteModal, setModuleDeleteModal] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { data: moduleIssues } = useSWR( workspaceSlug && projectId && module.id ? MODULE_ISSUES(module.id as string) : null, workspaceSlug && projectId && module.id ? () => modulesService.getModuleIssues(workspaceSlug as string, projectId as string, module.id) : null ); const completedIssues = (moduleIssues ?? []).filter( (i) => i.state_detail.group === "completed" || i.state_detail.group === "cancelled" ).length; const completionPercentage = (completedIssues / (moduleIssues ?? []).length) * 100; const handleDeleteModule = () => { if (!module) return; setModuleDeleteModal(true); }; 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)}

{module?.status?.replace("-", " ")}
{module.is_favorite ? ( ) : ( )} Edit Module Delete Module Copy Module Link
Start: {renderShortDateWithYearFormat(startDate)}
End: {renderShortDateWithYearFormat(endDate)}
Lead:
{module.lead_detail ? (
{module.lead_detail.first_name}
) : (
N/A N/A
)}
Members:
{module.members && module.members.length > 0 ? ( ) : (
N/A N/A
)}
Progress
{isNaN(completionPercentage) ? 0 : completionPercentage.toFixed(2)}%
); };