Skip to content

Commit

Permalink
[#192] Check visualization payload updated
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Jan 5, 2024
1 parent 02cec3e commit 073ff85
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
41 changes: 40 additions & 1 deletion frontend/src/pages/cases/components/IncomeDriverDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
DashboardIncomeOverview,
DashboardSensitivityAnalysis,
DashboardScenarioModeling,
removeUndefinedObjectValue,
} from "./";
import { api } from "../../../lib";
import { StepBackwardOutlined } from "@ant-design/icons";
import { isEmpty } from "lodash";
import { isEmpty, isEqual } from "lodash";

const IncomeDriverDashboard = ({
commodityList,
Expand All @@ -25,12 +26,14 @@ const IncomeDriverDashboard = ({

// sensitivity analysis data
const [binningData, setBinningData] = useState({});
const [currentBinningData, setCurrentBinningData] = useState({});

// scenario modeling data
const [percentage, setPercentage] = useState(true);
const [scenarioData, setScenarioData] = useState([
{ key: 1, name: "Scenario 1", description: null, scenarioValues: [] },
]);
const [currentScenarioData, setCurrentScenarioData] = useState([]);

useEffect(() => {
if (currentCaseId) {
Expand All @@ -52,13 +55,18 @@ const IncomeDriverDashboard = ({
...prev,
...sensitivityAnalysisConfig,
}));
setCurrentBinningData((prev) => ({
...prev,
...sensitivityAnalysisConfig,
}));
}
// Scenario modeling
const scenarioModelingConfig =
data.find((v) => v.tab === "scenario_modeling")?.config || {};
if (!isEmpty(scenarioModelingConfig)) {
setPercentage(scenarioModelingConfig.percentage);
setScenarioData(scenarioModelingConfig.scenarioData);
setCurrentScenarioData(scenarioModelingConfig.scenarioData);
}
setTimeout(() => {
setLoading(false);
Expand Down Expand Up @@ -98,6 +106,37 @@ const IncomeDriverDashboard = ({
},
},
];

// sensitivity analysis
const isBinningDataUpdated = !isEqual(
removeUndefinedObjectValue(currentBinningData),
removeUndefinedObjectValue(binningData)
);
// scenario modeler
const currentScenarioDataTmp = currentScenarioData.map((d) => {
const scenarioValues = d.scenarioValues.map((v) => {
const filterAllNewValues = v?.allNewValues
? removeUndefinedObjectValue(v.allNewValues)
: {};
return { ...v, allNewValues: filterAllNewValues };
});
return { ...d, scenarioValues: scenarioValues };
});
const scenarioDataTmp = scenarioData.map((d) => {
const scenarioValues = d.scenarioValues.map((v) => {
const filterAllNewValues = v?.allNewValues
? removeUndefinedObjectValue(v.allNewValues)
: {};
return { ...v, allNewValues: filterAllNewValues };
});
return { ...d, scenarioValues: scenarioValues };
});
const isScenarioDataUpdated = !isEqual(
currentScenarioDataTmp,
scenarioDataTmp
);
const isUpdated = isBinningDataUpdated || isScenarioDataUpdated;
console.info(isUpdated);
// Save
api
.post("visualization", payloads)
Expand Down
37 changes: 19 additions & 18 deletions frontend/src/pages/cases/components/IncomeDriverDataEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,25 @@ const IncomeDriverDataEntry = ({
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);
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)?.length > 0;
console.info(isUpdated, "UPDATED");

if (postFormValues.length) {
Expand Down

0 comments on commit 073ff85

Please # to comment.