Skip to content

Commit

Permalink
Merge pull request #352 from akvo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wayangalihpratama authored Jun 24, 2024
2 parents 957c08f + 134d3e6 commit 29fe3d3
Showing 1 changed file with 86 additions and 23 deletions.
109 changes: 86 additions & 23 deletions frontend/src/pages/cases/components/IncomeDriverTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const IncomeDriverTarget = ({

const [messageApi, contextHolder] = message.useMessage();

const [notificationShown, setNotificationShown] = useState(false);

const calculateHouseholdSize = ({
household_adult = 0,
household_children = 0,
Expand Down Expand Up @@ -106,15 +108,17 @@ const IncomeDriverTarget = ({
}, [segmentItem, currentSegmentId, form, regionOptions]);

const resetBenchmark = useCallback(
({ regionData }) => {
({ region }) => {
form.setFieldsValue({
region: region,
target: null,
household_adult: null,
household_children: null,
});
setHouseholdSize(0);
setIncomeTarget(0);
updateFormValues({
...regionData,
region: region,
target: 0,
benchmark: {},
adult: null,
Expand All @@ -125,6 +129,16 @@ const IncomeDriverTarget = ({
[form, setBenchmark, updateFormValues]
);

const showBenchmarNotification = useCallback(
({ currentCase }) => {
return messageApi.open({
type: "error",
content: `Benchmark not available in ${currentCase.currency} for the year ${currentCase.year}.`,
});
},
[messageApi]
);

const fetchBenchmark = useCallback(
({ region }) => {
const regionData = { region: region };
Expand All @@ -137,12 +151,9 @@ const IncomeDriverTarget = ({
const { data } = res;
// if data value by currency not found or 0
// return a NA notif
if (!data.value?.[currentCase.currency.toLowerCase()] === 0) {
resetBenchmark({ regionData });
messageApi.open({
type: "error",
content: `Benchmark not available in ${currentCase.currency} for the year ${currentCase.year}.`,
});
if (data?.value?.[currentCase.currency.toLowerCase()] === 0) {
resetBenchmark({ region: region });
showBenchmarNotification({ currentCase });
return;
}
//
Expand All @@ -161,6 +172,7 @@ const IncomeDriverTarget = ({
});
//
const targetHH = data.household_equiv;
// Use LCU if currency if not USE/EUR
const targetValue =
data.value?.[currentCase.currency.toLowerCase()] || data.value.lcu;
// with CPI calculation
Expand Down Expand Up @@ -195,7 +207,7 @@ const IncomeDriverTarget = ({
})
.catch((e) => {
// reset field and benchmark value
resetBenchmark({ regionData });
resetBenchmark({ region: region });
// show notification
const { statusText, data } = e.response;
const content = data?.detail || statusText;
Expand All @@ -212,12 +224,28 @@ const IncomeDriverTarget = ({
updateFormValues,
setBenchmark,
resetBenchmark,
showBenchmarNotification,
]
);

useEffect(() => {
// handle income target value when householdSize updated
if (benchmark && !isEmpty(benchmark) && benchmark !== "NA") {
// show benchmark notification
if (
benchmark?.value?.[currentCase.currency.toLowerCase()] === 0 &&
!notificationShown
) {
showBenchmarNotification({ currentCase });
setNotificationShown(true);
setTimeout(() => {
resetBenchmark({ region: null });
setNotificationShown(false); // Reset the flag after the benchmark is reset
}, 600);
return;
}

// Use LCU if currency if not USE/EUR
const targetValue =
benchmark.value?.[currentCase.currency.toLowerCase()] ||
benchmark.value.lcu;
Expand Down Expand Up @@ -249,7 +277,16 @@ const IncomeDriverTarget = ({
) {
fetchBenchmark({ region: segmentItem?.benchmark?.region });
}
}, [benchmark, householdSize, currentCase, fetchBenchmark, segmentItem]);
}, [
benchmark,
householdSize,
currentCase,
fetchBenchmark,
segmentItem,
resetBenchmark,
notificationShown,
showBenchmarNotification,
]);

// call region api
useEffect(() => {
Expand Down Expand Up @@ -280,28 +317,54 @@ const IncomeDriverTarget = ({
};

const onValuesChange = (changedValues, allValues) => {
const { target, region } = allValues;
const { target, region, manual_target } = allValues;
const HHSize = calculateHouseholdSize(allValues);
setHouseholdSize(HHSize);
// eslint-disable-next-line no-undefined
if (changedValues.manual_target !== undefined) {
// manual target
// comment for now
// setDisableTarget(!changedValues.manual_target);
// if (changedValues.manual_target && target) {
// form.setFieldsValue({ region: null });
// setIncomeTarget(target);
// updateFormValues({ region: null, target: target });
// }
// if (!changedValues.manual_target) {
// form.setFieldsValue({ target: null });
// setIncomeTarget(segmentItem?.target || 0);
// updateFormValues({ region: null, target: 0 });
// }

// new version
setDisableTarget(!changedValues.manual_target);
if (changedValues.manual_target && target) {
form.setFieldsValue({ region: null });
setIncomeTarget(target);
updateFormValues({ region: null, target: target });
}
if (!changedValues.manual_target) {
form.setFieldsValue({ target: null });
setIncomeTarget(segmentItem?.target || 0);
updateFormValues({ region: null, target: 0 });
}
form.setFieldsValue({
region: null,
target: null,
household_adult: null,
household_children: null,
});
setIncomeTarget(0);
updateFormValues({
region: null,
target: 0,
adult: null,
child: null,
benchmark: {},
});
setBenchmark("NA");
}
// manual target
if ((changedValues.target || !changedValues.target) && !disableTarget) {
// eslint-disable-next-line no-undefined
if (manual_target && changedValues.target !== undefined && !disableTarget) {
setIncomeTarget(target);
updateFormValues({ region: null, target: target });
updateFormValues({
region: null,
target: target,
adult: null,
child: null,
benchmark: {},
});
}
if (changedValues.region && disableTarget) {
// get from API
Expand Down

0 comments on commit 29fe3d3

Please # to comment.