Skip to content

Commit

Permalink
[#391] Fix EnterIncomeData page onLoad & onValuesChange
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Jan 17, 2025
1 parent 3af78ff commit 287f805
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
10 changes: 6 additions & 4 deletions frontend/src/components/layout/ContentLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const ContentLayout = ({
justify="start"
className="title-wrapper"
>
<Col span={titleRighContent ? 12 : 24}>
<Col span={titleRighContent ? 8 : 24}>
{title ? (
<div data-testid="title" className="title">
{title}
Expand All @@ -111,9 +111,11 @@ const ContentLayout = ({
)}
</Col>
{titleRighContent && (
<Col span={12}>
<Row justify="end">
<Col>{titleRighContent}</Col>
<Col span={16}>
<Row>
<Col span={24} align="end">
{titleRighContent}
</Col>
</Row>
</Col>
)}
Expand Down
79 changes: 75 additions & 4 deletions frontend/src/pages/cases/components/EnterIncomeDataForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ const EnterIncomeDataDriver = ({
// Handle aggregator questions
if (!question?.parent && question?.question_type === "aggregator") {
updateSectionTotalValues(fieldName, changedValue[key]);
// Update the current segment state with all values
updateCurrentSegmentState({
answers: { ...segment?.answers, ...allValues },
});
return;
}

Expand All @@ -289,6 +293,10 @@ const EnterIncomeDataDriver = ({
return value ? acc + value : acc;
}, 0);
updateSectionTotalValues(fieldName, sumAllDiversifiedValues);
// Update the current segment state with all values
updateCurrentSegmentState({
answers: { ...segment?.answers, ...allValues },
});
return;
}

Expand Down Expand Up @@ -336,14 +344,77 @@ const EnterIncomeDataDriver = ({
if (!isEmpty(initialDriverValues)) {
setTimeout(() => {
Object.keys(initialDriverValues).forEach((key) => {
const value = initialDriverValues[key];
// TODO :: Fix this onLoad function, this make the initial value rerender into 0 onLoad
// onValuesChange({ [key]: value }, form.getFieldsValue());
const [fieldName, caseCommodityId, questionId] = key.split("-");
const fieldKey = `${fieldName}-${caseCommodityId}`;

// Find the current question and its parent
const question = flattenQuestionList.find(
(q) => q.id === parseInt(questionId)
);
const parentQuestion = flattenQuestionList.find(
(q) => q.id === question?.parent
);

// Handle aggregator questions
if (!question?.parent && question?.question_type === "aggregator") {
updateSectionTotalValues(fieldName, initialDriverValues[key]);
return;
}

// Handle diversified questions
if (!question?.parent && question?.question_type === "diversified") {
const diversifiedQuestions = flattenQuestionList.filter(
(q) => q.question_type === "diversified"
);
const diversifiedQids = diversifiedQuestions.map(
(q) => `${fieldKey}-${q.id}`
);
const sumAllDiversifiedValues = diversifiedQids.reduce(
(acc, id) => {
const value = initialDriverValues?.[id];
return value ? acc + value : acc;
},
0
);
updateSectionTotalValues(fieldName, sumAllDiversifiedValues);
return;
}

// Gather all children question values
const childrenQuestions = flattenQuestionList.filter(
(q) => q.parent === question?.parent
);
const allChildrensIds = childrenQuestions.map(
(q) => `${fieldKey}-${q.id}`
);
const allChildrensValues = allChildrensIds.reduce((acc, id) => {
const value = initialDriverValues?.[id];
if (value) {
acc.push({ id, value });
}
return acc;
}, []);

// Calculate sum of children's values
const sumAllChildrensValues = parentQuestion?.default_value
? getFunctionDefaultValue(
parentQuestion,
fieldKey,
allChildrensValues
)
: allChildrensValues.reduce((acc, { value }) => acc + value, 0);

// Update the parent question value
const parentQuestionField = `${fieldKey}-${question?.parent}`;
if (parentQuestion) {
form.setFieldValue(parentQuestionField, sumAllChildrensValues);
updateSectionTotalValues(fieldName, sumAllChildrensValues);
}
});
}, 500);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [initialDriverValues]);

return (
<Row align="middle">
Expand Down
1 change: 1 addition & 0 deletions frontend/src/pages/cases/steps/SetIncomeTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const SetIncomeTarget = ({ segment, setbackfunction, setnextfunction }) => {
}
});
return values;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const updateCurrentSegmentState = useCallback(
Expand Down

0 comments on commit 287f805

Please sign in to comment.