Skip to content

Commit b182bcd

Browse files
[#700[ Fix onload initialDriverValues calculation
1 parent 7ddd285 commit b182bcd

File tree

2 files changed

+123
-85
lines changed

2 files changed

+123
-85
lines changed

frontend/src/pages/cases/components/EnterIncomeDataForm.js

Lines changed: 122 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -344,96 +344,133 @@ const EnterIncomeDataDriver = ({
344344

345345
useEffect(() => {
346346
if (!isEmpty(initialDriverValues)) {
347-
const onLoad = ({ key }) => {
348-
const [fieldName, caseCommodityId, questionId] = key.split("-");
349-
const fieldKey = `${fieldName}-${caseCommodityId}`;
350-
351-
const commodity = currentCase.case_commodities.find(
352-
(cc) => cc.id === parseInt(caseCommodityId)
353-
);
354-
355-
const question = flattenQuestionList.find(
356-
(q) => q.id === parseInt(questionId)
357-
);
358-
const parentQuestion = flattenQuestionList.find(
359-
(q) => q.id === question?.parent
360-
);
361-
362-
handleQuestionType(
363-
question,
364-
commodity,
365-
fieldName,
366-
initialDriverValues,
367-
fieldKey,
368-
flattenQuestionList,
369-
updateSectionTotalValues
370-
);
371-
372-
const parentQuestionField = `${fieldKey}-${question?.parent}`;
373-
374-
// TODO :: remove this code to fix issue total income/section total (DONT REMOVE)
375-
/**
376-
* e.g. primary section total should be 1400
377-
* but if this part of code active the section total become 1600
378-
* ONLY RUN THIS IF PARENT QUESTION VALUE === 0 TO RE-EVALUATE THE TOTAL INCOME
379-
*/
380-
const parentQuestionValue =
381-
initialDriverValues?.[parentQuestionField] || 0;
382-
383-
if (
384-
parentQuestion?.question_type === "aggregator" &&
385-
!parentQuestionValue &&
386-
currentCase?.import_id
387-
) {
388-
const allChildrensValues = calculateChildrenValues(
389-
question,
390-
fieldKey,
391-
initialDriverValues
392-
);
393-
394-
const sumAllChildrensValues = parentQuestion?.default_value
395-
? getFunctionDefaultValue(
396-
parentQuestion,
397-
fieldKey,
398-
allChildrensValues
399-
)
400-
: allChildrensValues.reduce((acc, { value }) => acc + value, 0);
401-
if (parentQuestion) {
402-
// use parent value if they already have value
403-
const formValue = parentQuestionValue
404-
? roundToDecimal(parentQuestionValue)
405-
: roundToDecimal(sumAllChildrensValues);
406-
// EOL use parent value if they already have value
407-
form.setFieldValue(parentQuestionField, formValue);
408-
// trigger on change
347+
const formKeys = Object.keys(form.getFieldsValue());
348+
const initialDriverValuesKeys = Object.keys(initialDriverValues);
349+
const keysToProcess = [
350+
...new Set([...formKeys, ...initialDriverValuesKeys]),
351+
];
352+
353+
// Process keys in parallel using `Promise.all`
354+
Promise.all(
355+
keysToProcess.map((key) => {
356+
return new Promise((resolve) => {
409357
onValuesChange(
410-
{ [parentQuestionField]: formValue },
411-
form.getFieldsValue()
358+
{ [key]: initialDriverValues?.[key] || 0 },
359+
initialDriverValues
412360
);
413-
// eol trigger on change
414-
updateSectionTotalValues(
415-
commodity.commodity_type,
416-
fieldName,
417-
sumAllChildrensValues
361+
resolve();
362+
});
363+
})
364+
).then(() => {
365+
// Wait a bit to allow previous computations to finish
366+
setTimeout(() => {
367+
keysToProcess.forEach((key) => {
368+
onValuesChange(
369+
{ [key]: initialDriverValues?.[key] || 0 },
370+
initialDriverValues
418371
);
419-
}
420-
}
421-
// EOL RECALCULATE TOTAL INCOME
422-
// EOL remove this code to fix issue total income/section total
423-
424-
if (parentQuestion?.parent) {
425-
onLoad({ key: parentQuestionField });
426-
}
427-
};
428-
429-
setTimeout(() => {
430-
Object.keys(initialDriverValues).forEach((key) => {
431-
onLoad({ key });
432-
});
433-
}, 500);
372+
});
373+
}, 500);
374+
});
434375
}
435376
// eslint-disable-next-line react-hooks/exhaustive-deps
436-
}, [initialDriverValues, currentCase?.import_id]);
377+
}, [initialDriverValues]);
378+
379+
// TODO:: OLD INITIAL VALUE LOAD USE EFFECT (REMOVE)
380+
// useEffect(() => {
381+
// if (!isEmpty(initialDriverValues)) {
382+
// const onLoad = ({ key }) => {
383+
// const [fieldName, caseCommodityId, questionId] = key.split("-");
384+
// const fieldKey = `${fieldName}-${caseCommodityId}`;
385+
386+
// const commodity = currentCase.case_commodities.find(
387+
// (cc) => cc.id === parseInt(caseCommodityId)
388+
// );
389+
390+
// const question = flattenQuestionList.find(
391+
// (q) => q.id === parseInt(questionId)
392+
// );
393+
// const parentQuestion = flattenQuestionList.find(
394+
// (q) => q.id === question?.parent
395+
// );
396+
397+
// handleQuestionType(
398+
// question,
399+
// commodity,
400+
// fieldName,
401+
// initialDriverValues,
402+
// fieldKey,
403+
// flattenQuestionList,
404+
// updateSectionTotalValues
405+
// );
406+
407+
// const parentQuestionField = `${fieldKey}-${question?.parent}`;
408+
409+
// // TODO :: remove this code to fix issue total income/section total (DONT REMOVE)
410+
// /**
411+
// * e.g. primary section total should be 1400
412+
// * but if this part of code active the section total become 1600
413+
// * ONLY RUN THIS IF PARENT QUESTION VALUE === 0 TO RE-EVALUATE THE TOTAL INCOME
414+
// */
415+
// const parentQuestionValue =
416+
// initialDriverValues?.[parentQuestionField] || 0;
417+
418+
// if (
419+
// // parentQuestion?.question_type === "aggregator" &&
420+
// !parentQuestionValue &&
421+
// currentCase?.import_id
422+
// ) {
423+
// const allChildrensValues = calculateChildrenValues(
424+
// question,
425+
// fieldKey,
426+
// initialDriverValues
427+
// );
428+
429+
// const sumAllChildrensValues = parentQuestion?.default_value
430+
// ? getFunctionDefaultValue(
431+
// parentQuestion,
432+
// fieldKey,
433+
// allChildrensValues
434+
// )
435+
// : allChildrensValues.reduce((acc, { value }) => acc + value, 0);
436+
// if (parentQuestion) {
437+
// // use parent value if they already have value
438+
// const formValue = parentQuestionValue
439+
// ? roundToDecimal(parentQuestionValue)
440+
// : roundToDecimal(sumAllChildrensValues);
441+
// // EOL use parent value if they already have value
442+
// form.setFieldValue(parentQuestionField, formValue);
443+
// console.log(parentQuestionField, formValue);
444+
// // trigger on change
445+
// onValuesChange(
446+
// { [parentQuestionField]: formValue },
447+
// form.getFieldsValue()
448+
// );
449+
// // eol trigger on change
450+
// updateSectionTotalValues(
451+
// commodity.commodity_type,
452+
// fieldName,
453+
// sumAllChildrensValues
454+
// );
455+
// }
456+
// }
457+
// // EOL RECALCULATE TOTAL INCOME
458+
// // EOL remove this code to fix issue total income/section total
459+
460+
// if (parentQuestion?.parent) {
461+
// onLoad({ key: parentQuestionField });
462+
// }
463+
// };
464+
465+
// setTimeout(() => {
466+
// Object.keys(initialDriverValues).forEach((key) => {
467+
// onLoad({ key });
468+
// });
469+
// }, 500);
470+
// }
471+
// // eslint-disable-next-line react-hooks/exhaustive-deps
472+
// }, [initialDriverValues, currentCase?.import_id]);
473+
// EOL OLD INITIAL VALUE LOAD USE EFFECT
437474

438475
return (
439476
<Row align="middle">

frontend/src/pages/cases/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const handleQuestionType = (
99
updateSectionTotalValues = (/*commodity_type, fieldName, value*/) => {}
1010
) => {
1111
if (!question?.parent) {
12+
// handle aggregator and diversified questions
1213
if (question?.question_type === "aggregator") {
1314
const value = values?.[`${fieldKey}-${question.id}`] || 0;
1415
updateSectionTotalValues(commodity.commodity_type, fieldName, value);

0 commit comments

Comments
 (0)