2024-06-10 09:43:10 +00:00
|
|
|
"use client";
|
|
|
|
|
2024-06-10 18:17:30 +00:00
|
|
|
import { Dispatch, FC, SetStateAction, useCallback } from "react";
|
2024-06-10 06:46:23 +00:00
|
|
|
import { observer } from "mobx-react";
|
|
|
|
import { Plus } from "lucide-react";
|
|
|
|
import { TEstimatePointsObject, TEstimateSystemKeys } from "@plane/types";
|
|
|
|
import { Button, Sortable } from "@plane/ui";
|
|
|
|
// components
|
|
|
|
import { EstimatePointCreate, EstimatePointItemPreview } from "@/components/estimates/points";
|
|
|
|
// constants
|
|
|
|
import { maxEstimatesCount } from "@/constants/estimates";
|
|
|
|
|
|
|
|
type TEstimatePointCreateRoot = {
|
|
|
|
workspaceSlug: string;
|
|
|
|
projectId: string;
|
|
|
|
estimateId: string | undefined;
|
|
|
|
estimateType: TEstimateSystemKeys;
|
|
|
|
estimatePoints: TEstimatePointsObject[];
|
|
|
|
setEstimatePoints: Dispatch<SetStateAction<TEstimatePointsObject[] | undefined>>;
|
2024-06-10 18:17:30 +00:00
|
|
|
estimatePointCreate: TEstimatePointsObject[] | undefined;
|
|
|
|
setEstimatePointCreate: Dispatch<SetStateAction<TEstimatePointsObject[] | undefined>>;
|
|
|
|
estimatePointCreateError: number[];
|
2024-06-10 06:46:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const EstimatePointCreateRoot: FC<TEstimatePointCreateRoot> = observer((props) => {
|
|
|
|
// props
|
2024-06-10 18:17:30 +00:00
|
|
|
const {
|
|
|
|
workspaceSlug,
|
|
|
|
projectId,
|
|
|
|
estimateId,
|
|
|
|
estimateType,
|
|
|
|
estimatePoints,
|
|
|
|
setEstimatePoints,
|
|
|
|
estimatePointCreate,
|
|
|
|
setEstimatePointCreate,
|
|
|
|
estimatePointCreateError,
|
|
|
|
} = props;
|
2024-06-10 06:46:23 +00:00
|
|
|
|
|
|
|
const handleEstimatePoint = useCallback(
|
|
|
|
(mode: "add" | "remove" | "update", value: TEstimatePointsObject) => {
|
|
|
|
switch (mode) {
|
|
|
|
case "add":
|
|
|
|
setEstimatePoints((prevValue) => {
|
|
|
|
prevValue = prevValue ? [...prevValue] : [];
|
|
|
|
return [...prevValue, value];
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "update":
|
|
|
|
setEstimatePoints((prevValue) => {
|
|
|
|
prevValue = prevValue ? [...prevValue] : [];
|
|
|
|
return prevValue.map((item) => (item.key === value.key ? { ...item, value: value.value } : item));
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "remove":
|
|
|
|
setEstimatePoints((prevValue) => {
|
|
|
|
prevValue = prevValue ? [...prevValue] : [];
|
|
|
|
return prevValue.filter((item) => item.key !== value.key);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[setEstimatePoints]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleEstimatePointCreate = (mode: "add" | "remove", value: TEstimatePointsObject) => {
|
|
|
|
switch (mode) {
|
|
|
|
case "add":
|
|
|
|
setEstimatePointCreate((prevValue) => {
|
|
|
|
prevValue = prevValue ? [...prevValue] : [];
|
|
|
|
return [...prevValue, value];
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "remove":
|
|
|
|
setEstimatePointCreate((prevValue) => {
|
|
|
|
prevValue = prevValue ? [...prevValue] : [];
|
|
|
|
return prevValue.filter((item) => item.key !== value.key);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDragEstimatePoints = (updatedEstimatedOrder: TEstimatePointsObject[]) => {
|
|
|
|
const updatedEstimateKeysOrder = updatedEstimatedOrder.map((item, index) => ({ ...item, key: index + 1 }));
|
|
|
|
setEstimatePoints(() => updatedEstimateKeysOrder);
|
|
|
|
};
|
|
|
|
|
2024-06-10 18:17:30 +00:00
|
|
|
const handleCreate = () => {
|
|
|
|
if (estimatePoints && estimatePoints.length + (estimatePointCreate?.length || 0) <= maxEstimatesCount - 1) {
|
|
|
|
handleEstimatePointCreate("add", {
|
|
|
|
id: undefined,
|
|
|
|
key: estimatePoints.length + (estimatePointCreate?.length || 0) + 1,
|
|
|
|
value: "",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-10 06:46:23 +00:00
|
|
|
if (!workspaceSlug || !projectId) return <></>;
|
|
|
|
return (
|
2024-06-10 18:17:30 +00:00
|
|
|
<div className="space-y-1">
|
2024-06-10 06:46:23 +00:00
|
|
|
<div className="text-sm font-medium text-custom-text-200 capitalize">{estimateType}</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<Sortable
|
|
|
|
data={estimatePoints}
|
|
|
|
render={(value: TEstimatePointsObject) => (
|
|
|
|
<EstimatePointItemPreview
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
projectId={projectId}
|
|
|
|
estimateId={estimateId}
|
|
|
|
estimateType={estimateType}
|
|
|
|
estimatePointId={value?.id}
|
|
|
|
estimatePoints={estimatePoints}
|
|
|
|
estimatePoint={value}
|
|
|
|
handleEstimatePointValueUpdate={(estimatePointValue: string) =>
|
|
|
|
handleEstimatePoint("update", { ...value, value: estimatePointValue })
|
|
|
|
}
|
|
|
|
handleEstimatePointValueRemove={() => handleEstimatePoint("remove", value)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
onChange={(data: TEstimatePointsObject[]) => handleDragEstimatePoints(data)}
|
|
|
|
keyExtractor={(item: TEstimatePointsObject) => item?.id?.toString() || item.value.toString()}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{estimatePointCreate &&
|
|
|
|
estimatePointCreate.map((estimatePoint) => (
|
|
|
|
<EstimatePointCreate
|
|
|
|
key={estimatePoint?.key}
|
|
|
|
workspaceSlug={workspaceSlug}
|
|
|
|
projectId={projectId}
|
|
|
|
estimateId={estimateId}
|
|
|
|
estimateType={estimateType}
|
|
|
|
estimatePoints={estimatePoints}
|
|
|
|
handleEstimatePointValue={(estimatePointValue: string) =>
|
|
|
|
handleEstimatePoint("add", { ...estimatePoint, value: estimatePointValue })
|
|
|
|
}
|
|
|
|
closeCallBack={() => handleEstimatePointCreate("remove", estimatePoint)}
|
2024-06-10 18:17:30 +00:00
|
|
|
handleCreateCallback={() => estimatePointCreate.length === 1 && handleCreate()}
|
|
|
|
isError={estimatePointCreateError.includes(estimatePoint.key) ? true : false}
|
2024-06-10 06:46:23 +00:00
|
|
|
/>
|
|
|
|
))}
|
2024-06-10 18:17:30 +00:00
|
|
|
{estimatePoints && estimatePoints.length + (estimatePointCreate?.length || 0) <= maxEstimatesCount - 1 && (
|
|
|
|
<Button variant="link-primary" size="sm" prependIcon={<Plus />} onClick={handleCreate}>
|
2024-06-10 06:46:23 +00:00
|
|
|
Add {estimateType}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|