Skip to content

Commit

Permalink
fix: Unwrap the custom form array for the prposed action
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeyash07 authored and samshara committed Jan 24, 2025
1 parent 1b0d1f7 commit 7dd8233
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ function ProposedActionsInput(props: Props) {
<div className={styles.proposedActionsInput}>
<div className={styles.activity}>
<SelectInput
className={styles.input}
required
className={styles.input}
label={strings.drefFormProposedActionActivityLabel}
name="activity"
readOnly
Expand All @@ -82,8 +82,8 @@ function ProposedActionsInput(props: Props) {
labelSelector={activityLabelSelector}
/>
<NumberInput
className={styles.input}
required
className={styles.input}
name="budget"
value={value.budget}
label={strings.drefFormProposedActionBudgetLabel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

display: flex;
gap: var(--go-ui-spacing-md);
flex-wrap: wrap;
align-items: center;
flex-wrap: wrap;

.activity {
display: flex;
flex-grow: 1;
flex-wrap: wrap;
gap: var(--go-ui-spacing-md);
flex-grow: 1;

.input {
flex-grow: 1;
}
}

.button {
flex-grow: 0;
align-self: flex-end;
flex-grow: 0;
padding: var(--go-ui-spacing-xs);
}
}
2 changes: 1 addition & 1 deletion app/src/views/DrefApplicationForm/Operation/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"drefFormTotalTargetedPopulation": "Total targeted population is not equal to sum of other population fields",
"drefFormUploadTargetingSupportingDocument": "Upload any additional support document (Optional)",
"drefFormUploadTargetingDocumentButtonLabel": "Upload document",
"drefFormProposedActionSelectBudgetNote": "If Surge Personnel are deployed, costs include CHF 10,000 for deployment and CHF 5,800 in indirect costs. Without deployment, only CHF 5,000 in indirect costs apply.",
"drefFormProposedActionSelectBudgetNote": "Please note that if Surge Personnel are deployed, the Surge Deployment cost will be CHF 10,000, and the Indirect Costs will be CHF 5,800. Conversely, if Surge Personnel are not deployed, the Surge Deployment cost will not be applicable, and the Indirect Costs will be CHF 5,000.",
"drefFormProposedActionSelectActivitiesLabel": "Select the activity",
"drefFormProposedActionEarlyActionsLabel": "Early Actions",
"drefFormProposedActionEarlyResponseLabel": "Early Response",
Expand Down
99 changes: 45 additions & 54 deletions app/src/views/DrefApplicationForm/Operation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,55 +109,51 @@ function Operation(props: Props) {
setValue,
} = props;

function useProposedActionsFormArray() {
const onProposedActionChange = useCallback(
(val: SetValueArg<ProposedActionsFormFields>, index: number | undefined) => {
setValue((oldVal) => {
const newProposedValue = [...(oldVal.proposed_action ?? [])];
if (isNotDefined(index)) {
newProposedValue.push(
isCallable(val) ? val(undefined) : val,
);
} else {
newProposedValue[index] = isCallable(val)
? val(newProposedValue[index])
: val;
}

const newValue = {
...oldVal,
proposed_action: newProposedValue,
};
const onProposedActionChange = useCallback(
(val: SetValueArg<ProposedActionsFormFields>, index: number | undefined) => {
setValue((oldVal) => {
const newProposedValue = [...(oldVal.proposed_action ?? [])];
if (isNotDefined(index)) {
newProposedValue.push(
isCallable(val) ? val(undefined) : val,
);
} else {
newProposedValue[index] = isCallable(val)
? val(newProposedValue[index])
: val;
}

return {
...newValue,
...recalculateProposedActionValues(newValue),
};
}, true);
},
[],
);
const newValue = {
...oldVal,
proposed_action: newProposedValue,
};

const onProposedActionRemove = useCallback(
(index: number) => {
setValue(
(oldValue) => {
const newProposedValue = [...(oldValue.proposed_action ?? [])];
newProposedValue.splice(index, 1);

return {
...oldValue,
proposed_action: newProposedValue,
};
},
true,
);
},
[],
);
return {
...newValue,
...recalculateProposedActionValues(newValue),
};
}, true);
},
[setValue],
);

return { onProposedActionChange, onProposedActionRemove };
}
const onProposedActionRemove = useCallback(
(index: number) => {
setValue(
(oldValue) => {
const newProposedValue = [...(oldValue.proposed_action ?? [])];
newProposedValue.splice(index, 1);

return {
...oldValue,
proposed_action: newProposedValue,
};
},
true,
);
},
[setValue],
);

const error = getErrorObject(formError);

Expand All @@ -184,11 +180,6 @@ function Operation(props: Props) {
setFieldValue,
);

const {
onProposedActionChange,
onProposedActionRemove,
} = useProposedActionsFormArray();

const {
setValue: onRiskSecurityChange,
removeValue: onRiskSecurityRemove,
Expand Down Expand Up @@ -946,11 +937,11 @@ function Operation(props: Props) {
<NumberInput
required
readOnly
name="surge_deployment"
name="surge_deployment_cost"
onChange={setFieldValue}
label={strings.drefFormProposedActionSurgeDeployment}
value={value.surge_deployment}
error={error?.surge_deployment}
value={value.surge_deployment_cost}
error={error?.surge_deployment_cost}
disabled={disabled}
/>
)}
Expand Down
13 changes: 10 additions & 3 deletions app/src/views/DrefApplicationForm/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,23 @@ export const recalculateProposedActionValues = (val: PartialDref) => {
const subTotal = sumSafe(
val.proposed_action?.map((pa) => pa.budget),
) ?? 0;
const surgeDeployment = val.is_surge_personnel_deployed ? 10000 : undefined;

// NOTE: if Surge Personnel are deployed,
// the Surge Deployment cost will be CHF 10,000,
// and the Indirect Costs will be CHF 5,800. Conversely,
// if Surge Personnel are not deployed,
// the Surge Deployment cost will not be applicable,
// and the Indirect Costs will be CHF 5,000
const surgeDeploymentCost = val.is_surge_personnel_deployed ? 10000 : undefined;
const indirectCost = val.is_surge_personnel_deployed ? 5800 : 5000;

const total = sumSafe(
[subTotal, indirectCost, surgeDeployment],
[subTotal, indirectCost, surgeDeploymentCost],
);
return {
sub_total: subTotal,
indirect_cost: indirectCost,
surge_deployment: surgeDeployment,
surge_deployment_cost: surgeDeploymentCost,
total,
};
};
Expand Down
10 changes: 5 additions & 5 deletions app/src/views/DrefApplicationForm/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ const schema: DrefFormSchema = {
'human_resource',
'is_surge_personnel_deployed',
'sub_total',
'surge_deployment',
'surge_deployment_cost',
'indirect_cost',
'total',
] as const;
Expand Down Expand Up @@ -639,7 +639,7 @@ const schema: DrefFormSchema = {
is_surge_personnel_deployed: { forceValue: nullValue },
has_child_safeguarding_risk_analysis_assessment: { forceValue: nullValue },
sub_total: { forceValue: nullValue },
surge_deployment: { forceValue: nullValue },
surge_deployment_cost: { forceValue: nullValue },
indirect_cost: { forceValue: nullValue },
total: { forceValue: nullValue },
};
Expand Down Expand Up @@ -788,11 +788,11 @@ const schema: DrefFormSchema = {
conditionalFields,
formValue,
['is_surge_personnel_deployed'],
['indirect_cost', 'surge_deployment'],
['indirect_cost', 'surge_deployment_cost'],
(value) => {
if (value?.is_surge_personnel_deployed) {
return {
surge_deployment: { required: true },
surge_deployment_cost: { required: true },
indirect_cost: {
required: true,
validations: [
Expand All @@ -803,7 +803,7 @@ const schema: DrefFormSchema = {
};
}
return {
surge_deployment: { forceValue: nullValue },
surge_deployment_cost: { forceValue: nullValue },
indirect_cost: {
required: true,
validations: [
Expand Down

0 comments on commit 7dd8233

Please sign in to comment.