import React, { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; 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 { LinkIcon, PencilIcon, StarIcon, TrashIcon } from "@heroicons/react/24/outline"; import { CalendarMonthIcon, TargetIcon } from "components/icons"; // 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; mutate( MODULE_LIST(projectId as string), (prevData) => (prevData ?? []).map((m) => ({ ...m, is_favorite: m.id === module.id ? true : m.is_favorite, })), false ); modulesService .addModuleToFavorites(workspaceSlug as string, projectId as string, { module: module.id, }) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Couldn't add the module to favorites. Please try again.", }); }); }; const handleRemoveFromFavorites = () => { if (!workspaceSlug || !projectId || !module) return; mutate( MODULE_LIST(projectId as string), (prevData) => (prevData ?? []).map((m) => ({ ...m, is_favorite: m.id === module.id ? false : m.is_favorite, })), false ); modulesService .removeModuleFromFavorites(workspaceSlug as string, projectId as string, module.id) .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 ?? ""); const lastUpdated = new Date(module.updated_at ?? ""); return ( <>

{truncateText(module.name, 75)}

{module?.status?.replace("-", " ")}
{module.is_favorite ? ( ) : ( )} Edit Module Delete Module Copy Module Link
Start: {renderShortDateWithYearFormat(startDate)}
End: {renderShortDateWithYearFormat(endDate)}
Progress
{isNaN(completionPercentage) ? 0 : completionPercentage.toFixed(0)}%

Last updated: {renderShortDateWithYearFormat(lastUpdated)}

{module.members_detail.length > 0 && (
)}
); };