import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; // services import estimatesService from "services/estimates.service"; import projectService from "services/project.service"; // hooks import useToast from "hooks/use-toast"; import useProjectDetails from "hooks/use-project-details"; // components import { EstimatePointsModal, DeleteEstimateModal } from "components/estimates"; // ui import { CustomMenu } from "components/ui"; //icons import { PencilIcon, TrashIcon, SquaresPlusIcon, ListBulletIcon, } from "@heroicons/react/24/outline"; // types import { IEstimate, IProject } from "types"; // fetch-keys import { ESTIMATE_POINTS_LIST } from "constants/fetch-keys"; import { orderArrayBy } from "helpers/array.helper"; type Props = { estimate: IEstimate; editEstimate: (estimate: IEstimate) => void; handleEstimateDelete: (estimateId: string) => void; }; export const SingleEstimate: React.FC = ({ estimate, editEstimate, handleEstimateDelete, }) => { const [isEstimatePointsModalOpen, setIsEstimatePointsModalOpen] = useState(false); const [isDeleteEstimateModalOpen, setIsDeleteEstimateModalOpen] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { projectDetails, mutateProjectDetails } = useProjectDetails(); const { data: estimatePoints } = useSWR( workspaceSlug && projectId ? ESTIMATE_POINTS_LIST(estimate.id) : null, workspaceSlug && projectId ? () => estimatesService.getEstimatesPointsList( workspaceSlug as string, projectId as string, estimate.id ) : null ); const handleUseEstimate = async () => { if (!workspaceSlug || !projectId) return; const payload = { estimate: estimate.id, }; mutateProjectDetails((prevData) => { if (!prevData) return prevData; return { ...prevData, estimate: estimate.id }; }, false); await projectService .updateProject(workspaceSlug as string, projectId as string, payload) .catch(() => { setToastAlert({ type: "error", title: "Error!", message: "Estimate points could not be used. Please try again.", }); }); }; return ( <> setIsEstimatePointsModalOpen(false)} data={estimatePoints ? orderArrayBy(estimatePoints, "key") : undefined} />
{estimate.name} {projectDetails?.estimate && projectDetails?.estimate === estimate.id && ( In use )}

{estimate.description}

{projectDetails?.estimate !== estimate.id && estimatePoints && estimatePoints.length > 0 && (
Use estimate
)} setIsEstimatePointsModalOpen(true)}>
{estimatePoints && estimatePoints?.length > 0 ? "Edit points" : "Create points"}
{ editEstimate(estimate); }} >
Edit estimate
{projectDetails?.estimate !== estimate.id && ( { setIsDeleteEstimateModalOpen(true); }} >
Delete estimate
)}
{estimatePoints && estimatePoints.length > 0 ? (
Estimate points( {estimatePoints.map((point, index) => (
{point.value} {index !== estimatePoints.length - 1 && ","}{" "}
))} )
) : (

No estimate points

)}
setIsDeleteEstimateModalOpen(false)} data={estimate} handleDelete={() => { handleEstimateDelete(estimate.id); setIsDeleteEstimateModalOpen(false); }} /> ); };