chore: update sorting in the estimates

This commit is contained in:
guru_sainath 2024-05-27 17:13:33 +05:30
parent 1cd31f4705
commit 9f329e1346
6 changed files with 56 additions and 35 deletions

View File

@ -62,8 +62,9 @@ class BulkEstimatePointEndpoint(BaseViewSet):
path="/api/workspaces/:slug/estimates/", url_params=True, user=False path="/api/workspaces/:slug/estimates/", url_params=True, user=False
) )
def create(self, request, slug, project_id): def create(self, request, slug, project_id):
estimate_name = request.data.get("name", generate_random_name()) estimate = request.data.get('estimate')
last_used = request.data.get("last_used", False) estimate_name = estimate.get("name", generate_random_name())
last_used = estimate.get("last_used", False)
estimate = Estimate.objects.create( estimate = Estimate.objects.create(
name=estimate_name, project_id=project_id, last_used=last_used name=estimate_name, project_id=project_id, last_used=last_used
) )

View File

@ -46,6 +46,12 @@ export const EstimateCreateStageTwo: FC<TEstimateCreateStageTwo> = observer((pro
handleEstimatePoints(newEstimationPoints); handleEstimatePoints(newEstimationPoints);
}; };
const replaceEstimateItem = (index: number, value: TEstimatePointsObject) => {
const newEstimationPoints = estimatePoints;
newEstimationPoints[index] = value;
handleEstimatePoints(newEstimationPoints);
};
const updatedSortedKeys = (updatedEstimatePoints: TEstimatePointsObject[]) => { const updatedSortedKeys = (updatedEstimatePoints: TEstimatePointsObject[]) => {
const sortedEstimatePoints = updatedEstimatePoints.map((item, index) => ({ const sortedEstimatePoints = updatedEstimatePoints.map((item, index) => ({
...item, ...item,
@ -67,10 +73,9 @@ export const EstimateCreateStageTwo: FC<TEstimateCreateStageTwo> = observer((pro
estimateId={undefined} estimateId={undefined}
mode={EEstimateUpdateStages.CREATE} mode={EEstimateUpdateStages.CREATE}
item={value} item={value}
estimatePoints={estimatePoints}
editItem={(value: string) => editEstimationPoint(index, value)} editItem={(value: string) => editEstimationPoint(index, value)}
replaceEstimateItem={(value: TEstimatePointsObject) => replaceEstimateItem(index, value)}
deleteItem={() => deleteEstimationPoint(index)} deleteItem={() => deleteEstimationPoint(index)}
handleEstimatePoints={handleEstimatePoints}
/> />
)} )}
onChange={(data: TEstimatePointsObject[]) => handleEstimatePoints(updatedSortedKeys(data))} onChange={(data: TEstimatePointsObject[]) => handleEstimatePoints(updatedSortedKeys(data))}

View File

@ -16,25 +16,14 @@ type TEstimatePointItem = {
estimateId: string | undefined; estimateId: string | undefined;
mode: EEstimateUpdateStages; mode: EEstimateUpdateStages;
item: TEstimatePointsObject; item: TEstimatePointsObject;
estimatePoints: TEstimatePointsObject[];
editItem: (value: string) => void; editItem: (value: string) => void;
replaceEstimateItem: (value: TEstimatePointsObject) => void;
deleteItem: () => void; deleteItem: () => void;
handleEstimatePoints: (value: TEstimatePointsObject[]) => void;
}; };
export const EstimatePointItem: FC<TEstimatePointItem> = observer((props) => { export const EstimatePointItem: FC<TEstimatePointItem> = observer((props) => {
// props // props
const { const { workspaceSlug, projectId, estimateId, mode, item, editItem, replaceEstimateItem, deleteItem } = props;
workspaceSlug,
projectId,
estimateId,
mode,
item,
estimatePoints,
editItem,
deleteItem,
handleEstimatePoints,
} = props;
const { id, key, value } = item; const { id, key, value } = item;
// hooks // hooks
const { asJson: estimate, creteEstimatePoint, deleteEstimatePoint } = useEstimate(estimateId); const { asJson: estimate, creteEstimatePoint, deleteEstimatePoint } = useEstimate(estimateId);
@ -63,8 +52,9 @@ export const EstimatePointItem: FC<TEstimatePointItem> = observer((props) => {
try { try {
setEstimateEditLoader(true); setEstimateEditLoader(true);
const estimatePoint = await creteEstimatePoint(workspaceSlug, projectId, { key: key, value: inputValue }); const estimatePoint = await creteEstimatePoint(workspaceSlug, projectId, { key: key, value: inputValue });
if (estimatePoint) if (estimatePoint && estimatePoint.key && estimatePoint.value) {
handleEstimatePoints([...estimatePoints, { id: estimatePoint.id, key: key, value: inputValue }]); replaceEstimateItem({ id: estimatePoint.id, key: estimatePoint.key, value: estimatePoint.value });
}
setIsEstimateEditing(false); setIsEstimateEditing(false);
setEstimateEditLoader(false); setEstimateEditLoader(false);
} catch (error) { } catch (error) {
@ -77,7 +67,11 @@ export const EstimatePointItem: FC<TEstimatePointItem> = observer((props) => {
if (id) { if (id) {
try { try {
setEstimateEditLoader(true); setEstimateEditLoader(true);
await updateEstimatePoint(workspaceSlug, projectId, { key: key, value: inputValue }); const estimatePoint = await updateEstimatePoint(workspaceSlug, projectId, {
key: key,
value: inputValue || "",
});
if (estimatePoint) if (estimatePoint) editItem(inputValue || "");
setIsEstimateEditing(false); setIsEstimateEditing(false);
setEstimateEditLoader(false); setEstimateEditLoader(false);
} catch (error) { } catch (error) {
@ -93,6 +87,7 @@ export const EstimatePointItem: FC<TEstimatePointItem> = observer((props) => {
try { try {
setEstimateEditLoader(true); setEstimateEditLoader(true);
await deleteEstimatePoint(workspaceSlug, projectId, id, deletedEstimateValue); await deleteEstimatePoint(workspaceSlug, projectId, id, deletedEstimateValue);
deleteItem();
setIsEstimateDeleting(false); setIsEstimateDeleting(false);
setEstimateEditLoader(false); setEstimateEditLoader(false);
} catch (error) { } catch (error) {

View File

@ -8,6 +8,7 @@ import { Button, Sortable } from "@plane/ui";
import { EstimatePointItem } from "@/components/estimates"; import { EstimatePointItem } from "@/components/estimates";
// constants // constants
import { EEstimateUpdateStages, maxEstimatesCount } from "@/constants/estimates"; import { EEstimateUpdateStages, maxEstimatesCount } from "@/constants/estimates";
import { useEstimate } from "@/hooks/store";
type TEstimateUpdateStageTwo = { type TEstimateUpdateStageTwo = {
workspaceSlug: string; workspaceSlug: string;
@ -20,6 +21,8 @@ type TEstimateUpdateStageTwo = {
export const EstimateUpdateStageTwo: FC<TEstimateUpdateStageTwo> = observer((props) => { export const EstimateUpdateStageTwo: FC<TEstimateUpdateStageTwo> = observer((props) => {
const { workspaceSlug, projectId, estimate, estimateEditType, estimatePoints, handleEstimatePoints } = props; const { workspaceSlug, projectId, estimate, estimateEditType, estimatePoints, handleEstimatePoints } = props;
// hooks
const { updateEstimate: updateEstimateRequest } = useEstimate(estimate?.id);
const currentEstimateSystem = estimate || undefined; const currentEstimateSystem = estimate || undefined;
@ -39,7 +42,7 @@ export const EstimateUpdateStageTwo: FC<TEstimateUpdateStageTwo> = observer((pro
}; };
const deleteEstimationPoint = (index: number) => { const deleteEstimationPoint = (index: number) => {
let newEstimationPoints = estimatePoints; let newEstimationPoints = cloneDeep(estimatePoints);
newEstimationPoints.splice(index, 1); newEstimationPoints.splice(index, 1);
newEstimationPoints = newEstimationPoints.map((item, index) => ({ newEstimationPoints = newEstimationPoints.map((item, index) => ({
...item, ...item,
@ -48,12 +51,25 @@ export const EstimateUpdateStageTwo: FC<TEstimateUpdateStageTwo> = observer((pro
handleEstimatePoints(newEstimationPoints); handleEstimatePoints(newEstimationPoints);
}; };
const replaceEstimateItem = (index: number, value: TEstimatePointsObject) => {
const newEstimationPoints = cloneDeep(estimatePoints);
newEstimationPoints[index].id = value.id;
newEstimationPoints[index].key = value.key;
newEstimationPoints[index].value = value.value;
handleEstimatePoints(newEstimationPoints);
};
const updatedSortedKeys = (updatedEstimatePoints: TEstimatePointsObject[]) => { const updatedSortedKeys = (updatedEstimatePoints: TEstimatePointsObject[]) => {
const sortedEstimatePoints = updatedEstimatePoints.map((item, index) => ({ try {
...item, const sortedEstimatePoints = cloneDeep(updatedEstimatePoints).map((item, index) => ({
key: index + 1, ...item,
})) as TEstimatePointsObject[]; key: index + 1,
return sortedEstimatePoints; })) as TEstimatePointsObject[];
handleEstimatePoints(sortedEstimatePoints);
updateEstimateRequest(workspaceSlug, projectId, { estimate_points: sortedEstimatePoints });
} catch (error) {
console.log(error);
}
}; };
if (!estimateEditType) return <></>; if (!estimateEditType) return <></>;
@ -72,13 +88,12 @@ export const EstimateUpdateStageTwo: FC<TEstimateUpdateStageTwo> = observer((pro
estimateId={estimate?.id || undefined} estimateId={estimate?.id || undefined}
mode={estimateEditType} mode={estimateEditType}
item={value} item={value}
estimatePoints={estimatePoints}
editItem={(value: string) => editEstimationPoint(index, value)} editItem={(value: string) => editEstimationPoint(index, value)}
replaceEstimateItem={(value: TEstimatePointsObject) => replaceEstimateItem(index, value)}
deleteItem={() => deleteEstimationPoint(index)} deleteItem={() => deleteEstimationPoint(index)}
handleEstimatePoints={handleEstimatePoints}
/> />
)} )}
onChange={(data: TEstimatePointsObject[]) => handleEstimatePoints(updatedSortedKeys(data))} onChange={(data: TEstimatePointsObject[]) => updatedSortedKeys(data)}
keyExtractor={(item: TEstimatePointsObject) => item?.id?.toString() || item.value.toString()} keyExtractor={(item: TEstimatePointsObject) => item?.id?.toString() || item.value.toString()}
/> />
{estimateEditType === EEstimateUpdateStages.EDIT && ( {estimateEditType === EEstimateUpdateStages.EDIT && (

View File

@ -60,7 +60,7 @@ export class EstimateService extends APIService {
workspaceSlug: string, workspaceSlug: string,
projectId: string, projectId: string,
estimateId: string, estimateId: string,
payload: Partial<IEstimate> payload: Partial<IEstimateFormData>
): Promise<IEstimate | undefined> { ): Promise<IEstimate | undefined> {
try { try {
const { data } = await this.patch( const { data } = await this.patch(
@ -98,7 +98,7 @@ export class EstimateService extends APIService {
payload: Partial<IEstimatePoint> payload: Partial<IEstimatePoint>
): Promise<IEstimatePoint | undefined> { ): Promise<IEstimatePoint | undefined> {
try { try {
const { data } = await this.post( const { data } = await this.patch(
`/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/${estimateId}/estimate-points/${estimatePointId}/`, `/api/workspaces/${workspaceSlug}/projects/${projectId}/estimates/${estimateId}/estimate-points/${estimatePointId}/`,
payload payload
); );

View File

@ -2,7 +2,12 @@ import set from "lodash/set";
import unset from "lodash/unset"; import unset from "lodash/unset";
import { action, computed, makeObservable, observable, runInAction } from "mobx"; import { action, computed, makeObservable, observable, runInAction } from "mobx";
import { computedFn } from "mobx-utils"; import { computedFn } from "mobx-utils";
import { IEstimate as IEstimateType, IEstimatePoint as IEstimatePointType, TEstimateSystemKeys } from "@plane/types"; import {
IEstimate as IEstimateType,
IEstimatePoint as IEstimatePointType,
TEstimateSystemKeys,
IEstimateFormData,
} from "@plane/types";
// services // services
import { EstimateService } from "@/services/project/estimate.service"; import { EstimateService } from "@/services/project/estimate.service";
// store // store
@ -26,7 +31,7 @@ export interface IEstimate extends IEstimateType {
updateEstimate: ( updateEstimate: (
workspaceSlug: string, workspaceSlug: string,
projectId: string, projectId: string,
payload: Partial<IEstimateType> payload: Partial<IEstimateFormData>
) => Promise<IEstimateType | undefined>; ) => Promise<IEstimateType | undefined>;
creteEstimatePoint: ( creteEstimatePoint: (
workspaceSlug: string, workspaceSlug: string,
@ -149,13 +154,13 @@ export class Estimate implements IEstimate {
* @description update an estimate * @description update an estimate
* @param { string } workspaceSlug * @param { string } workspaceSlug
* @param { string } projectId * @param { string } projectId
* @param { Partial<IEstimateType> } payload * @param { Partial<IEstimateFormData> } payload
* @returns { IEstimateType | undefined } * @returns { IEstimateType | undefined }
*/ */
updateEstimate = async ( updateEstimate = async (
workspaceSlug: string, workspaceSlug: string,
projectId: string, projectId: string,
payload: Partial<IEstimateType> payload: Partial<IEstimateFormData>
): Promise<IEstimateType | undefined> => { ): Promise<IEstimateType | undefined> => {
try { try {
if (!this.id || !payload) return; if (!this.id || !payload) return;