1+ import { useCallback } from 'react' ;
12import { CheckboxMultipleBlankFillIcon } from '@ifrc-go/icons' ;
23import {
34 Button ,
@@ -14,11 +15,13 @@ import {
1415 useBooleanState ,
1516 useTranslation ,
1617} from '@ifrc-go/ui/hooks' ;
18+ import { sumSafe } from '@ifrc-go/ui/utils' ;
1719import {
1820 type EntriesAsList ,
1921 type Error ,
2022 getErrorObject ,
2123 getErrorString ,
24+ type SetBaseValueArg ,
2225} from '@togglecorp/toggle-form' ;
2326
2427import GoSingleFileInput from '#components/domain/GoSingleFileInput' ;
@@ -33,6 +36,7 @@ import i18n from './i18n.json';
3336interface Props {
3437 value : PartialEapFullFormType ;
3538 setFieldValue : ( ...entries : EntriesAsList < PartialEapFullFormType > ) => void ;
39+ setValue : ( value : SetBaseValueArg < PartialEapFullFormType > ) => void ;
3640 error : Error < PartialEapFullFormType > | undefined ;
3741 disabled ?: boolean ;
3842 fileIdToUrlMap : Record < number , string > ;
@@ -47,6 +51,7 @@ function FinanceLogistics(props: Props) {
4751 const {
4852 value,
4953 setFieldValue,
54+ setValue,
5055 error : formError ,
5156 disabled,
5257 fileIdToUrlMap,
@@ -73,23 +78,50 @@ function FinanceLogistics(props: Props) {
7378 } ,
7479 } ) ;
7580
81+ const setBudgetValue = useCallback (
82+ (
83+ budgetValue : number | undefined ,
84+ name :
85+ | 'readiness_budget'
86+ | 'pre_positioning_budget'
87+ | 'early_action_budget' ,
88+ ) => {
89+ setValue ( ( prevValue ) => {
90+ const newBudgetValue = {
91+ readiness_budget : prevValue . readiness_budget ,
92+ pre_positioning_budget : prevValue . pre_positioning_budget ,
93+ early_action_budget : prevValue . early_action_budget ,
94+ [ name ] : budgetValue ,
95+ } ;
96+
97+ return {
98+ ...prevValue ,
99+ [ name ] : budgetValue ,
100+ total_budget : sumSafe ( [
101+ newBudgetValue . readiness_budget ,
102+ newBudgetValue . pre_positioning_budget ,
103+ newBudgetValue . early_action_budget ,
104+ ] ) ,
105+ } ;
106+ } ) ;
107+ } ,
108+ [ setValue ] ,
109+ ) ;
110+
76111 return (
77112 < TabPage spacingOffset = { - 6 } >
78113 < InlineLayout
79114 after = { (
80115 < Button
81116 name = { undefined }
82117 onClick = { setShowQualityCriteriaTrue }
83- after = { ( < CheckboxMultipleBlankFillIcon /> ) }
118+ after = { < CheckboxMultipleBlankFillIcon /> }
84119 >
85120 { strings . financeCriteriaButtonLabel }
86121 </ Button >
87122 ) }
88123 />
89- < ListView
90- layout = "block"
91- spacing = "xs"
92- >
124+ < ListView layout = "block" spacing = "xs" >
93125 < Heading variant = "form" > { strings . financeHeading } </ Heading >
94126 < InputSection
95127 title = { strings . financeBudgetTitle }
@@ -117,7 +149,8 @@ function FinanceLogistics(props: Props) {
117149 error = { error ?. total_budget }
118150 onChange = { setFieldValue }
119151 disabled = { disabled }
120- readOnly = { readOnly }
152+ readOnly
153+ required
121154 />
122155 < TextArea
123156 label = { strings . financeDescriptionLabel }
@@ -131,12 +164,7 @@ function FinanceLogistics(props: Props) {
131164 </ InputSection >
132165 < InputSection
133166 description = { (
134- < Link
135- external
136- href = { templateUrl ?. url }
137- withUnderline
138- withLinkIcon
139- >
167+ < Link external href = { templateUrl ?. url } withUnderline withLinkIcon >
140168 { strings . financeDownloadDescription }
141169 </ Link >
142170 ) }
@@ -194,9 +222,10 @@ function FinanceLogistics(props: Props) {
194222 name = "readiness_budget"
195223 value = { value ?. readiness_budget }
196224 error = { error ?. readiness_budget }
197- onChange = { setFieldValue }
225+ onChange = { setBudgetValue }
198226 disabled = { disabled }
199227 readOnly = { readOnly }
228+ required
200229 />
201230 < TextArea
202231 label = { strings . financeDescriptionLabel }
@@ -244,9 +273,10 @@ function FinanceLogistics(props: Props) {
244273 name = "pre_positioning_budget"
245274 value = { value ?. pre_positioning_budget }
246275 error = { error ?. pre_positioning_budget }
247- onChange = { setFieldValue }
276+ onChange = { setBudgetValue }
248277 disabled = { disabled }
249278 readOnly = { readOnly }
279+ required
250280 />
251281 < TextArea
252282 label = { strings . financeDescriptionLabel }
@@ -296,9 +326,10 @@ function FinanceLogistics(props: Props) {
296326 name = "early_action_budget"
297327 value = { value ?. early_action_budget }
298328 error = { error ?. early_action_budget }
299- onChange = { setFieldValue }
329+ onChange = { setBudgetValue }
300330 disabled = { disabled }
301331 readOnly = { readOnly }
332+ required
302333 />
303334 < TextArea
304335 label = { strings . financeDescriptionLabel }
@@ -311,10 +342,7 @@ function FinanceLogistics(props: Props) {
311342 />
312343 </ InputSection >
313344 </ ListView >
314- < ListView
315- layout = "block"
316- spacing = "xs"
317- >
345+ < ListView layout = "block" spacing = "xs" >
318346 < Heading variant = "form" > { strings . financeEapEndorsementHeading } </ Heading >
319347 < InputSection
320348 title = { strings . financeEapEndorsementTitle }
0 commit comments