plane/web/components/estimates/create/modal.tsx

135 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-05-23 08:11:30 +00:00
import { FC, useEffect, useMemo, useState } from "react";
import { observer } from "mobx-react";
import { ChevronLeft } from "lucide-react";
2024-05-27 04:17:31 +00:00
import { IEstimateFormData, TEstimateSystemKeys, TEstimatePointsObject } from "@plane/types";
import { Button, TOAST_TYPE, setToast } from "@plane/ui";
2024-05-23 08:11:30 +00:00
// components
import { EModalPosition, EModalWidth, ModalCore } from "@/components/core";
2024-05-29 05:51:45 +00:00
import { EstimateCreateStageOne, EstimatePointCreateRoot } from "@/components/estimates";
2024-05-23 08:11:30 +00:00
// constants
2024-05-27 04:17:31 +00:00
import { EEstimateSystem, ESTIMATE_SYSTEMS } from "@/constants/estimates";
// hooks
import { useProjectEstimates } from "@/hooks/store";
2024-05-23 08:11:30 +00:00
type TCreateEstimateModal = {
workspaceSlug: string;
projectId: string;
2024-05-23 08:11:30 +00:00
isOpen: boolean;
handleClose: () => void;
};
export const CreateEstimateModal: FC<TCreateEstimateModal> = observer((props) => {
2024-05-23 08:11:30 +00:00
// props
const { workspaceSlug, projectId, isOpen, handleClose } = props;
// hooks
const { createEstimate } = useProjectEstimates();
2024-05-23 08:11:30 +00:00
// states
const [estimateSystem, setEstimateSystem] = useState<TEstimateSystemKeys>(EEstimateSystem.POINTS);
const [estimatePoints, setEstimatePoints] = useState<TEstimatePointsObject[] | undefined>(undefined);
const [buttonLoader, setButtonLoader] = useState(false);
2024-05-23 08:11:30 +00:00
2024-05-27 04:17:31 +00:00
const handleUpdatePoints = (newPoints: TEstimatePointsObject[] | undefined) => setEstimatePoints(newPoints);
2024-05-23 08:11:30 +00:00
useEffect(() => {
2024-05-29 09:36:39 +00:00
if (isOpen) {
setEstimateSystem(EEstimateSystem.POINTS);
2024-05-23 08:11:30 +00:00
setEstimatePoints(undefined);
}
}, [isOpen]);
const handleCreateEstimate = async () => {
try {
2024-05-31 11:31:50 +00:00
if (!workspaceSlug || !projectId || !estimatePoints) return;
setButtonLoader(true);
2024-05-31 11:31:50 +00:00
const payload: IEstimateFormData = {
estimate: {
name: ESTIMATE_SYSTEMS[estimateSystem]?.name,
type: estimateSystem,
last_used: true,
},
estimate_points: estimatePoints,
};
await createEstimate(workspaceSlug, projectId, payload);
2024-05-28 09:36:22 +00:00
setButtonLoader(false);
2024-05-31 11:31:50 +00:00
setToast({
type: TOAST_TYPE.SUCCESS,
title: "Estimate created",
message: "A new estimate has been added in your project.",
2024-05-31 11:31:50 +00:00
});
handleClose();
} catch (error) {
setButtonLoader(false);
setToast({
type: TOAST_TYPE.ERROR,
2024-05-31 11:31:50 +00:00
title: "Estimate creation failed",
message: "We were unable to create the new estimate, please try again.",
});
}
};
2024-05-29 05:51:45 +00:00
// derived values
const renderEstimateStepsCount = useMemo(() => (estimatePoints ? "2" : "1"), [estimatePoints]);
2024-05-23 08:11:30 +00:00
return (
<ModalCore isOpen={isOpen} handleClose={handleClose} position={EModalPosition.TOP} width={EModalWidth.XXL}>
<div className="relative space-y-6 py-5">
{/* heading */}
<div className="relative flex justify-between items-center gap-2 px-5">
<div className="relative flex items-center gap-1">
{estimatePoints && (
<div
onClick={() => {
setEstimateSystem(EEstimateSystem.POINTS);
2024-05-23 08:11:30 +00:00
handleUpdatePoints(undefined);
}}
className="flex-shrink-0 cursor-pointer w-5 h-5 flex justify-center items-center"
>
<ChevronLeft className="w-4 h-4" />
</div>
)}
2024-05-29 05:51:45 +00:00
<div className="text-xl font-medium text-custom-text-100">New Estimate System</div>
2024-05-23 08:11:30 +00:00
</div>
<div className="text-xs text-gray-400">Step {renderEstimateStepsCount} of 2</div>
2024-05-23 08:11:30 +00:00
</div>
{/* estimate steps */}
<div className="px-5">
{!estimatePoints && (
<EstimateCreateStageOne
estimateSystem={estimateSystem}
handleEstimateSystem={setEstimateSystem}
handleEstimatePoints={(templateType: string) =>
2024-05-23 08:11:30 +00:00
handleUpdatePoints(ESTIMATE_SYSTEMS[estimateSystem].templates[templateType].values)
}
/>
)}
{estimatePoints && (
<>
<EstimatePointCreateRoot
workspaceSlug={workspaceSlug}
projectId={projectId}
estimateId={undefined}
estimateType={estimateSystem}
estimatePoints={estimatePoints}
setEstimatePoints={setEstimatePoints}
/>
</>
2024-05-23 08:11:30 +00:00
)}
</div>
2024-05-29 09:06:04 +00:00
<div className="relative flex justify-end items-center gap-3 px-5 pt-5 border-t border-custom-border-200">
<Button variant="neutral-primary" size="sm" onClick={handleClose} disabled={buttonLoader}>
2024-05-23 08:11:30 +00:00
Cancel
</Button>
{estimatePoints && (
<Button variant="primary" size="sm" onClick={handleCreateEstimate} disabled={buttonLoader}>
{buttonLoader ? `Creating` : `Create Estimate`}
2024-05-23 08:11:30 +00:00
</Button>
)}
</div>
</div>
</ModalCore>
);
});