chore: handled estimate edit

This commit is contained in:
guru_sainath 2024-05-28 16:48:57 +05:30
parent 1c1620a822
commit e6fa881721
4 changed files with 50 additions and 16 deletions

View File

@ -36,7 +36,7 @@ export const EstimatePointDelete: FC<TEstimatePointDelete> = observer((props) =>
try {
setLoader(true);
setError(undefined);
await deleteEstimatePoint(workspaceSlug, projectId, estimateId, estimateInputValue);
await deleteEstimatePoint(workspaceSlug, projectId, estimatePointId, estimateInputValue);
setLoader(false);
setError(undefined);
handleClose();

View File

@ -23,7 +23,7 @@ export const EstimatePointEditRoot: FC<TEstimatePointEditRoot> = observer((props
// hooks
const { asJson: estimate, estimatePointIds, estimatePointById, updateEstimateSortOrder } = useEstimate(estimateId);
// states
const [estimatePointCreateToggle, setEstimatePointCreateToggle] = useState(false);
const [estimatePointCreate, setEstimatePointCreate] = useState<TEstimatePointsObject[] | undefined>(undefined);
const estimatePoints: TEstimatePointsObject[] =
estimatePointIds && estimatePointIds.length > 0
@ -33,6 +33,25 @@ export const EstimatePointEditRoot: FC<TEstimatePointEditRoot> = observer((props
}) as TEstimatePointsObject[])
: ([] as TEstimatePointsObject[]);
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 }));
updateEstimateSortOrder(workspaceSlug, projectId, updatedEstimateKeysOrder);
@ -60,21 +79,28 @@ export const EstimatePointEditRoot: FC<TEstimatePointEditRoot> = observer((props
keyExtractor={(item: TEstimatePointsObject) => item?.id?.toString() || item.value.toString()}
/>
{estimatePointCreateToggle && (
<EstimatePointCreate
workspaceSlug={workspaceSlug}
projectId={projectId}
estimateId={estimateId}
callback={() => setEstimatePointCreateToggle(false)}
/>
)}
{estimatePoints && estimatePoints.length <= maxEstimatesCount && (
{estimatePointCreate &&
estimatePointCreate.map((estimatePoint) => (
<EstimatePointCreate
key={estimatePoint?.key}
workspaceSlug={workspaceSlug}
projectId={projectId}
estimateId={estimateId}
callback={() => handleEstimatePointCreate("remove", estimatePoint)}
/>
))}
{estimatePoints && estimatePoints.length + (estimatePointCreate?.length || 0) <= maxEstimatesCount && (
<Button
variant="link-primary"
size="sm"
prependIcon={<Plus />}
onClick={() => setEstimatePointCreateToggle(true)}
disabled={estimatePointCreateToggle}
onClick={() =>
handleEstimatePointCreate("add", {
id: undefined,
key: estimatePoints.length + (estimatePointCreate?.length || 0) + 1,
value: "",
})
}
>
Add {estimate?.type}
</Button>

View File

@ -114,7 +114,7 @@ export class EstimateService extends APIService {
estimateId: string,
estimatePointId: string,
params?: { new_estimate_id: string | undefined }
): Promise<void> {
): Promise<IEstimatePoint[] | undefined> {
return this.delete(
`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/${estimateId}/estimate-points/${estimatePointId}/`,
params

View File

@ -50,7 +50,7 @@ export interface IEstimate extends IEstimateType {
projectId: string,
estimatePointId: string,
newEstimatePointId: string | undefined
) => Promise<void>;
) => Promise<IEstimatePointType[] | undefined>;
}
export class Estimate implements IEstimate {
@ -252,7 +252,7 @@ export class Estimate implements IEstimate {
projectId: string,
estimatePointId: string,
newEstimatePointId: string | undefined
) => {
): Promise<IEstimatePointType[] | undefined> => {
try {
if (!this.id) return;
@ -267,6 +267,14 @@ export class Estimate implements IEstimate {
runInAction(() => {
unset(this.estimatePoints, [estimatePointId]);
});
if (deleteEstimatePoint && deleteEstimatePoint.length > 0) {
runInAction(() => {
deleteEstimatePoint.map((estimatePoint) => {
if (estimatePoint.id)
set(this.estimatePoints, [estimatePoint.id], new EstimatePoint(this.store, this.data, estimatePoint));
});
});
}
return deleteEstimatePoint;
} catch (error) {