Skip to content

Commit

Permalink
[#192] Check updated payload value for segments
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Jan 4, 2024
1 parent bb50f7c commit 8f67585
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
25 changes: 5 additions & 20 deletions frontend/src/pages/cases/components/CaseProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
selectProps,
yesNoOptions,
DebounceSelect,
removeUndefinedObjectValue,
} from "./";
import { api } from "../../../lib";
import { UIState, UserState } from "../../../store";
Expand Down Expand Up @@ -457,27 +458,11 @@ const CaseProfile = ({
tags: values.tags || null,
};

// check current value with update value
const filteredCurrentCaseProfile = Object.entries(
currentCaseProfile
).reduce((acc, [key, value]) => {
if (typeof value !== "undefined") {
acc[key] = value;
}
return acc;
}, {});
const filteredValues = Object.entries(values).reduce(
(acc, [key, value]) => {
if (typeof value !== "undefined") {
acc[key] = value;
}
return acc;
},
{}
);
// detect is payload updated
const filteredCurrentCaseProfile =
removeUndefinedObjectValue(currentCaseProfile);
const filteredValues = removeUndefinedObjectValue(values);
const isUpdated = !isEqual(filteredCurrentCaseProfile, filteredValues);
// console.log(filteredCurrentCaseProfile, "FormData");
// console.log(filteredValues, "payload");
console.info(isUpdated, "UPDATED");

const paramCaseId = caseId ? caseId : currentCaseId;
Expand Down
39 changes: 37 additions & 2 deletions frontend/src/pages/cases/components/IncomeDriverDataEntry.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import React, { useState, useEffect } from "react";
import { Row, Col, Tabs, message } from "antd";
import { PlusCircleFilled } from "@ant-design/icons";
import { DataFields, generateSegmentPayloads } from "./";
import {
DataFields,
generateSegmentPayloads,
removeUndefinedObjectValue,
} from "./";
import { api } from "../../../lib";
import { orderBy } from "lodash";
import { orderBy, isEqual } from "lodash";

const IncomeDriverDataEntry = ({
commodityList,
Expand All @@ -24,6 +28,7 @@ const IncomeDriverDataEntry = ({
const [formValues, setFormValues] = useState([]);
const [messageApi, contextHolder] = message.useMessage();
const [isSaving, setIsSaving] = useState(false);
const [currentValues, setCurrentValues] = useState([]);

useEffect(() => {
const formValeusWithTotalCurrentIncomeAnswer = formValues.map(
Expand Down Expand Up @@ -55,6 +60,28 @@ const IncomeDriverDataEntry = ({
const apiCalls = [];
const postFormValues = formValues.filter((fv) => !fv.currentSegmentId);
const putFormValues = formValues.filter((fv) => fv.currentSegmentId);

// detect is payload updated
const isUpdated = currentValues
.map((cv) => {
cv = {
...cv,
answers: removeUndefinedObjectValue(cv.answers),
};
let findPayload = formValues.find((fv) => fv.key === cv.key);
findPayload = {
...findPayload,
answers: removeUndefinedObjectValue(findPayload.answers),
};
const equal = isEqual(
removeUndefinedObjectValue(cv),
removeUndefinedObjectValue(findPayload)
);
return !equal;
})
.filter((x) => x);
console.info(isUpdated, "UPDATED");

if (postFormValues.length) {
const postPayloads = generateSegmentPayloads(
postFormValues,
Expand Down Expand Up @@ -104,6 +131,7 @@ const IncomeDriverDataEntry = ({
};
});
setFormValues(transformFormValues);
setCurrentValues(transformFormValues);
messageApi.open({
type: "success",
content: "Segments saved successfully.",
Expand Down Expand Up @@ -184,6 +212,7 @@ const IncomeDriverDataEntry = ({
}
setItems(itemsTmp);
setFormValues(formValuesTmp);
setCurrentValues(formValuesTmp);
})
.catch(() => {
// default items if no segments in database
Expand All @@ -203,6 +232,12 @@ const IncomeDriverDataEntry = ({
answers: {},
},
]);
setCurrentValues({
key: "1",
label: "Segment 1",
currentSegmentId: null,
answers: {},
});
});
});
}, [commodityList, setQuestionGroups, currentCaseId, enableEditCase]);
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/pages/cases/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ export const yAxisFormula = {
"#9002": "( #9001 + ( ( #5 * #2 ) + ( #26 * #3 * #2 ) ) ) - ( #2 * #4 * #3 )", // diversified
};

export const removeUndefinedObjectValue = (obj) => {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (typeof value !== "undefined") {
acc[key] = value;
}
return acc;
}, {});
};

export { default as AreaUnitFields } from "./AreaUnitFields";
export { default as SideMenu } from "./SideMenu";
export { default as CaseProfile } from "./CaseProfile";
Expand Down

0 comments on commit 8f67585

Please # to comment.