import React, { useState } from "react"; // ui import { CustomMenu, PrimaryButton } from "components/ui"; // types import { IEstimate, IProject } from "types"; //icons import { PencilIcon, TrashIcon, SquaresPlusIcon, ListBulletIcon } from "@heroicons/react/24/outline"; import useSWR, { mutate } from "swr"; import useToast from "hooks/use-toast"; import estimatesService from "services/estimates.service"; import projectService from "services/project.service"; import { EstimatePointsModal } from "./estimate-points-modal"; import { useRouter } from "next/router"; import { ESTIMATE_POINTS_LIST } from "constants/fetch-keys"; import { PlusIcon } from "components/icons"; interface IEstimatePoints { key: string; value: string; } type Props = { estimate: IEstimate; editEstimate: (estimate: IEstimate) => void; handleEstimateDelete: (estimateId: string) => void; activeEstimate: IEstimate | null; setActiveEstimate: React.Dispatch>; }; export const SingleEstimate: React.FC = ({ estimate, editEstimate, handleEstimateDelete, activeEstimate, setActiveEstimate, }) => { const [isEstimatePointsModalOpen, setIsEstimatePointsModalOpen] = useState(false); const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); 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 handleActiveEstimate = async () => { if (!workspaceSlug || !projectId || !estimate) return; const payload: Partial = { estimate: estimate.id, }; setActiveEstimate(estimate); 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}

{estimate.description}

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

)}
); };