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 } 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"; 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 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)} />
{estimate.name} {projectDetails?.estimate && projectDetails?.estimate === estimate.id && ( In use )}

{estimate.description}

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

No estimate points

)}
); };