diff --git a/frontend/src/pages/cases/components/IncomeDriverTarget.js b/frontend/src/pages/cases/components/IncomeDriverTarget.js index d52d6c95..e7679d12 100644 --- a/frontend/src/pages/cases/components/IncomeDriverTarget.js +++ b/frontend/src/pages/cases/components/IncomeDriverTarget.js @@ -40,6 +40,8 @@ const IncomeDriverTarget = ({ const [messageApi, contextHolder] = message.useMessage(); + const [notificationShown, setNotificationShown] = useState(false); + const calculateHouseholdSize = ({ household_adult = 0, household_children = 0, @@ -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, @@ -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 }; @@ -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; } // @@ -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 @@ -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; @@ -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; @@ -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(() => { @@ -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