import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import Image from "next/image"; import { 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_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 completionPercentage = (module.completed_issues / module.total_issues) * 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
); };