Skip to content

Commit

Permalink
fix: calculation and pay subsidies would not reset if changed on edit…
Browse files Browse the repository at this point in the history
… (hl-1110, hl-1127, hl-1130) (#2847)
  • Loading branch information
sirtawast authored Feb 21, 2024
1 parent 2d9c08e commit 7f88cc5
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 32 deletions.
8 changes: 7 additions & 1 deletion frontend/benefit/handler/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@
"phoneNumber": {
"label": "Puhelin"
},
"coOperationNegotiations":{
"coOperationNegotiations": {
"label": "Muutosneuvottelut"
},
"coOperationNegotiationsDescription": {
Expand All @@ -1321,6 +1321,12 @@
},
"endDate": {
"label": "Työsuhde päättyy"
},
"paySubsidyGranted": {
"label": "Palkkatuki"
},
"apprenticeshipProgram": {
"label": "Oppisopimus"
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion frontend/benefit/handler/public/locales/fi/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@
"alternativeCompanyStreetAddress": {
"label": "Osoite"
},
"coOperationNegotiations":{
"coOperationNegotiations": {
"label": "Muutosneuvottelut"
},
"coOperationNegotiationsDescription": {
Expand All @@ -1321,6 +1321,12 @@
},
"endDate": {
"label": "Työsuhde päättyy"
},
"paySubsidyGranted": {
"label": "Palkkatuki"
},
"apprenticeshipProgram": {
"label": "Oppisopimus"
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion frontend/benefit/handler/public/locales/sv/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@
"phoneNumber": {
"label": "Puhelin"
},
"coOperationNegotiations":{
"coOperationNegotiations": {
"label": "Muutosneuvottelut"
},
"coOperationNegotiationsDescription": {
Expand All @@ -1321,6 +1321,12 @@
},
"endDate": {
"label": "Työsuhde päättyy"
},
"paySubsidyGranted": {
"label": "Palkkatuki"
},
"apprenticeshipProgram": {
"label": "Oppisopimus"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const useCompanySearch = (): ExtendedComponentProps => {
const [companies, setCompanies] = useState<CompanySearchResult[]>([]);
const [selectedCompany, setSelectedCompany] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(false);
const { onCompanySelected } = useFormActions({});
const { onCompanySelected } = useFormActions({}, {});

const onCompanyChange = (businessId: string): void => {
setSelectedCompany(businessId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ export const useApplicationForm = (): ExtendedComponentProps => {
const [initialApplication, setInitialApplication] =
React.useState<Application>(null);

const { onSave, onQuietSave, onSubmit, onNext, onDelete } =
useFormActions(application);
const { onSave, onQuietSave, onSubmit, onNext, onDelete } = useFormActions(
application,
initialApplication
);

React.useEffect(() => {
if (id) {
Expand Down
10 changes: 10 additions & 0 deletions frontend/benefit/handler/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
BENEFIT_TYPES,
CALCULATION_ROW_TYPES,
EMPLOYEE_KEYS,
PAY_SUBSIDY_OPTIONS,
} from 'benefit-shared/constants';

export enum ROUTES {
Expand Down Expand Up @@ -204,3 +205,12 @@ export enum LOCAL_STORAGE_KEYS {
export enum APPLICATION_ACTIONS {
HANDLER_ALLOW_APPLICATION_EDIT = 'HANDLER_ALLOW_APPLICATION_EDIT',
}

export const PAY_SUBSIDIES_OVERRIDE = {
startDate: null,
endDate: null,
paySubsidyPercent: PAY_SUBSIDY_OPTIONS[0],
workTimePercent: 100,
disabilityOrIllness: false,
durationInMonthsRounded: '',
};
95 changes: 69 additions & 26 deletions frontend/benefit/handler/src/hooks/useFormActions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
APPLICATION_ACTIONS,
APPLICATION_FIELD_KEYS,
PAY_SUBSIDIES_OVERRIDE,
ROUTES,
} from 'benefit/handler/constants';
import DeMinimisContext from 'benefit/handler/context/DeMinimisContext';
Expand All @@ -12,7 +13,12 @@ import {
PAY_SUBSIDY_GRANTED,
PAY_SUBSIDY_OPTIONS,
} from 'benefit-shared/constants';
import { ApplicationData, Employee } from 'benefit-shared/types/application';
import {
ApplicationData,
Calculation,
Employee,
PaySubsidy,
} from 'benefit-shared/types/application';
import { prettyPrintObject } from 'benefit-shared/utils/errors';
import camelcaseKeys from 'camelcase-keys';
import { useRouter } from 'next/router';
Expand Down Expand Up @@ -94,7 +100,10 @@ const getErrorContent = (
}
};

const useFormActions = (application: Partial<Application>): FormActions => {
const useFormActions = (
application: Partial<Application>,
initialApplication: Partial<Application>
): FormActions => {
const router = useRouter();

const { mutateAsync: createApplication, error: createApplicationError } =
Expand Down Expand Up @@ -150,6 +159,54 @@ const useFormActions = (application: Partial<Application>): FormActions => {

const { deMinimisAids } = React.useContext(DeMinimisContext);

const getCalculationValuesOnPaySubsidyChange = (
values: Partial<Application>
): Calculation => ({
...values.calculation,
monthlyPay: stringToFloatValue(values.employee.monthlyPay),
vacationMoney: stringToFloatValue(values.employee.vacationMoney),
otherExpenses: stringToFloatValue(values.employee.otherExpenses),
startDate: null,
endDate: null,
stateAidMaxPercentage: null,
grantedAsDeMinimisAid: false,
targetGroupCheck: false,
overrideMonthlyBenefitAmount: null,
overrideMonthlyBenefitAmountComment: '',
});

const getPayloadForCalculation = (
values: Partial<Application>
): Calculation => {
// Return the calculation values as they are
if (values.paySubsidyGranted === initialApplication?.paySubsidyGranted) {
return {
...values.calculation,
monthlyPay: stringToFloatValue(values.employee.monthlyPay),
otherExpenses: stringToFloatValue(values.employee.otherExpenses),
vacationMoney: stringToFloatValue(values.employee.vacationMoney),
overrideMonthlyBenefitAmount: stringToFloatValue(
values.calculation.overrideMonthlyBenefitAmount
),
};
}
return getCalculationValuesOnPaySubsidyChange(values);
};

const getPayloadForPaySubsidies = (
values: Partial<Application>
): PaySubsidy[] => {
if (values.paySubsidyGranted === initialApplication?.paySubsidyGranted) {
return [...values.paySubsidies];
}
return [
PAY_SUBSIDY_GRANTED.GRANTED,
PAY_SUBSIDY_GRANTED.GRANTED_AGED,
].includes(values.paySubsidyGranted)
? [PAY_SUBSIDIES_OVERRIDE]
: [];
};

// eslint-disable-next-line sonarjs/cognitive-complexity
const getNormalizedValues = (currentValues: Application): Application => {
const employee: Employee | undefined = currentValues?.employee ?? undefined;
Expand All @@ -161,6 +218,8 @@ const useFormActions = (application: Partial<Application>): FormActions => {
apprenticeshipProgram,
paperApplicationDate,
changeReason,
calculation,
paySubsidies,
} = currentValues;

const paySubsidyPercent =
Expand Down Expand Up @@ -212,34 +271,20 @@ const useFormActions = (application: Partial<Application>): FormActions => {
changeReason,
deMinimisAidSet: deMinimisAids,
action: APPLICATION_ACTIONS.HANDLER_ALLOW_APPLICATION_EDIT,
benefitType: BENEFIT_TYPES.SALARY,
calculation: calculation
? getPayloadForCalculation(currentValues)
: undefined,
paySubsidies: paySubsidies
? getPayloadForPaySubsidies(currentValues)
: undefined,
};
};

const prepareDataForSubmission = (
values: Partial<Application>
): ApplicationData =>
snakecaseKeys(
{
...values,
benefit_type: BENEFIT_TYPES.SALARY,
calculation: values.calculation
? {
...values.calculation,
monthlyPay: stringToFloatValue(values.calculation.monthlyPay),
otherExpenses: stringToFloatValue(
values.calculation.otherExpenses
),
vacationMoney: stringToFloatValue(
values.calculation.vacationMoney
),
overrideMonthlyBenefitAmount: stringToFloatValue(
values.calculation.overrideMonthlyBenefitAmount
),
}
: undefined,
},
{ deep: true }
) as ApplicationData;
snakecaseKeys(values, { deep: true }) as ApplicationData;

const onSubmit = async (
currentValues: Application,
Expand Down Expand Up @@ -290,7 +335,6 @@ const useFormActions = (application: Partial<Application>): FormActions => {
applicationId: string | undefined
): Promise<ApplicationData | void> => {
const data = prepareDataForSubmission(getNormalizedValues(currentValues));

try {
const result = applicationId
? await updateApplication(data)
Expand Down Expand Up @@ -339,7 +383,6 @@ const useFormActions = (application: Partial<Application>): FormActions => {
applicationId: string | undefined
): Promise<ApplicationData | void> => {
const data = prepareDataForSubmission(getNormalizedValues(currentValues));

try {
const result = applicationId
? await updateApplication(data)
Expand Down

0 comments on commit 7f88cc5

Please sign in to comment.