import { FC, useState } from "react"; import useSWR from "swr"; import { IEstimate } from "@plane/types"; import { Button, Loader, TOAST_TYPE, setToast } from "@plane/ui"; // components import { EmptyState } from "@/components/empty-state"; import { DeleteEstimateModal, EstimateListItem } from "@/components/estimates"; // constants import { EmptyStateType } from "@/constants/empty-state"; // ee components import { CreateEstimateModal } from "@/ee/components/estimates"; // hooks import { useProject, useProjectEstimates } from "@/hooks/store"; type TEstimateRoot = { workspaceSlug: string; projectId: string; }; export const EstimateRoot: FC = (props) => { const { workspaceSlug, projectId } = props; // hooks const { updateProject, currentProjectDetails } = useProject(); const { loader, projectEstimateIds, estimateById, getAllEstimates } = useProjectEstimates(projectId); // states const [isEstimateCreateModalOpen, setIsEstimateCreateModalOpen] = useState(true); const [isEstimateDeleteModalOpen, setIsEstimateDeleteModalOpen] = useState(null); const [estimateToUpdate, setEstimateToUpdate] = useState(); const { isLoading: isSWRLoading } = useSWR( workspaceSlug && projectId ? `PROJECT_ESTIMATES_${workspaceSlug}_${projectId}` : null, workspaceSlug && projectId ? () => getAllEstimates() : null ); const editEstimate = (estimate: IEstimate) => { setIsEstimateCreateModalOpen(true); console.log("estimate", estimate); // Order the points array by key before updating the estimate to update state // setEstimateToUpdate({ // ...estimate, // points: orderArrayBy(estimate.points, "key"), // }); }; const disableEstimates = () => { if (!workspaceSlug || !projectId) return; updateProject(workspaceSlug.toString(), projectId.toString(), { estimate: null }).catch((err) => { const error = err?.error; const errorString = Array.isArray(error) ? error[0] : error; setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: errorString ?? "Estimate could not be disabled. Please try again", }); }); }; if (!workspaceSlug || !projectId) return <>; return (
{/* modals */} {/* create modal */} { setIsEstimateCreateModalOpen(false); setEstimateToUpdate(undefined); }} /> {/* edit modal */} {/* delete modal */} setIsEstimateDeleteModalOpen(null)} data={ null // getProjectEstimateById(isEstimateDeleteModalOpen!) } /> {/* handling the estimates list */} {loader === "init-loader" || isSWRLoading ? ( ) : ( <> {/* header section */}

Estimates

{currentProjectDetails?.estimate && ( )}
{/* listing of estimates */} {!projectEstimateIds || (projectEstimateIds && projectEstimateIds.length <= 0) ? (
) : (
{projectEstimateIds && projectEstimateIds.map((estimateId: string) => { const estimate = estimateById(estimateId); if (!estimate) return <>; return ( editEstimate(estimate)} deleteEstimate={(estimateId) => setIsEstimateDeleteModalOpen(estimateId)} /> ); })}
)} )}
); };