From 073ff8551896a62af5bdd89296758067b7f0494e Mon Sep 17 00:00:00 2001 From: wayangalihpratama Date: Fri, 5 Jan 2024 11:55:24 +0800 Subject: [PATCH] [#192] Check visualization payload updated --- .../cases/components/IncomeDriverDashboard.js | 41 ++++++++++++++++++- .../cases/components/IncomeDriverDataEntry.js | 37 +++++++++-------- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/frontend/src/pages/cases/components/IncomeDriverDashboard.js b/frontend/src/pages/cases/components/IncomeDriverDashboard.js index 6079b16d..00aba6a2 100644 --- a/frontend/src/pages/cases/components/IncomeDriverDashboard.js +++ b/frontend/src/pages/cases/components/IncomeDriverDashboard.js @@ -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, @@ -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) { @@ -52,6 +55,10 @@ const IncomeDriverDashboard = ({ ...prev, ...sensitivityAnalysisConfig, })); + setCurrentBinningData((prev) => ({ + ...prev, + ...sensitivityAnalysisConfig, + })); } // Scenario modeling const scenarioModelingConfig = @@ -59,6 +66,7 @@ const IncomeDriverDashboard = ({ if (!isEmpty(scenarioModelingConfig)) { setPercentage(scenarioModelingConfig.percentage); setScenarioData(scenarioModelingConfig.scenarioData); + setCurrentScenarioData(scenarioModelingConfig.scenarioData); } setTimeout(() => { setLoading(false); @@ -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) diff --git a/frontend/src/pages/cases/components/IncomeDriverDataEntry.js b/frontend/src/pages/cases/components/IncomeDriverDataEntry.js index 9d1fd999..c6ade35a 100644 --- a/frontend/src/pages/cases/components/IncomeDriverDataEntry.js +++ b/frontend/src/pages/cases/components/IncomeDriverDataEntry.js @@ -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) {