Skip to content

Commit

Permalink
fix: validation and fixes to manual calculation (hl-1107) (#3202)
Browse files Browse the repository at this point in the history
* feat: add validation for manual calculation fields: sum per month required

* fix: manual calculation would not be shown if normal is calculated first
  • Loading branch information
sirtawast authored Aug 12, 2024
1 parent 0d59e8d commit 13f1f5b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { Field } from 'shared/components/forms/fields/types';
import { $GridCell } from 'shared/components/forms/section/FormSection.sc';
import { formatStringFloatValue } from 'shared/utils/string.utils';

import { $CalculatorText } from '../ApplicationReview.sc';

const SalaryBenefitManualCalculatorView: React.FC<
SalaryBenefitManualCalculatorViewProps
> = ({ formik, fields, getErrorMessage }) => {
Expand All @@ -18,13 +16,11 @@ const SalaryBenefitManualCalculatorView: React.FC<

return (
<>
<$GridCell $colStart={1} $colSpan={2}>
<$CalculatorText>{overrideMonthlyBenefitAmount?.label}</$CalculatorText>
</$GridCell>
<$GridCell $colStart={1} $colSpan={2}>
<TextInput
id={overrideMonthlyBenefitAmount?.name}
name={overrideMonthlyBenefitAmount?.name}
label={overrideMonthlyBenefitAmount?.label}
onChange={(e) =>
formik.setFieldValue(
overrideMonthlyBenefitAmount?.name,
Expand All @@ -37,6 +33,7 @@ const SalaryBenefitManualCalculatorView: React.FC<
invalid={!!getErrorMessage(overrideMonthlyBenefitAmount?.name)}
aria-invalid={!!getErrorMessage(overrideMonthlyBenefitAmount?.name)}
errorText={getErrorMessage(overrideMonthlyBenefitAmount?.name)}
required
/>
</$GridCell>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const SalaryCalculatorResults: React.FC<ApplicationReviewViewProps> = ({
return null;
}

if (rowsWithoutTotal.length === 0) {
if (rowsWithoutTotal.length === 0 && !isManualCalculator) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import {
} from 'shared/utils/date.utils';
import { v4 as uuidv4 } from 'uuid';

import { getValidationSchema } from './utils/validation';
import {
getManualValidationSchema,
getValidationSchema,
} from './utils/validation';

type ExtendedComponentProps = {
formik: FormikProps<CalculationFormProps>;
Expand Down Expand Up @@ -105,7 +108,9 @@ const useSalaryBenefitCalculatorData = (
? application?.trainingCompensations
: [],
},
validationSchema: getValidationSchema(t),
validationSchema: isManualCalculator
? getManualValidationSchema(t)
: getValidationSchema(t),
validateOnChange: true,
validateOnBlur: true,
enableReinitialize: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { validateNumberField } from '@frontend/benefit-shared/src/utils/validation';
import { CALCULATION_SALARY_KEYS } from 'benefit/handler/constants';
import {
CALCULATION_EMPLOYMENT_KEYS,
EMPLOYEE_KEYS,
MAX_MONTHLY_PAY,
VALIDATION_MESSAGE_KEYS,
} from 'benefit-shared/constants';
import { CalculationCommon } from 'benefit-shared/types/application';
import { validateDateWithinMonths } from 'benefit-shared/utils/dates';
import { subMonths } from 'date-fns';
import { TFunction } from 'next-i18next';
import { convertToUIDateFormat } from 'shared/utils/date.utils';
import * as Yup from 'yup';

export const getValidationSchema = (
Expand All @@ -15,9 +19,16 @@ export const getValidationSchema = (
Yup.object().shape({
[CALCULATION_EMPLOYMENT_KEYS.START_DATE]: Yup.string()
.typeError(VALIDATION_MESSAGE_KEYS.DATE_FORMAT)
.required(VALIDATION_MESSAGE_KEYS.REQUIRED),
.required(VALIDATION_MESSAGE_KEYS.REQUIRED)
.test({
message: t(VALIDATION_MESSAGE_KEYS.DATE_MIN, {
min: convertToUIDateFormat(subMonths(new Date(), 60)),
}),
test: (value = '') => validateDateWithinMonths(value, 60),
}),
[CALCULATION_EMPLOYMENT_KEYS.END_DATE]: Yup.string()
.typeError(VALIDATION_MESSAGE_KEYS.DATE_FORMAT)
.matches(/^\d+\.\d+\.20\d{2}$/, VALIDATION_MESSAGE_KEYS.DATE_FORMAT)
.required(VALIDATION_MESSAGE_KEYS.REQUIRED),
[EMPLOYEE_KEYS.VACATION_MONEY]: validateNumberField(0, MAX_MONTHLY_PAY, {
required: t(VALIDATION_MESSAGE_KEYS.REQUIRED),
Expand All @@ -32,3 +43,16 @@ export const getValidationSchema = (
typeError: t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID),
}),
});

export const getManualValidationSchema = (
t: TFunction
): Yup.SchemaOf<CalculationCommon> =>
getValidationSchema(t).shape({
[CALCULATION_SALARY_KEYS.OVERRIDE_MONTHLY_BENEFIT_AMOUNT]:
validateNumberField(0, 800, {
required: t(VALIDATION_MESSAGE_KEYS.REQUIRED),
typeError: t(VALIDATION_MESSAGE_KEYS.NUMBER_INVALID),
}),
[CALCULATION_SALARY_KEYS.OVERRIDE_MONTHLY_BENEFIT_AMOUNT_COMMENT]:
Yup.string().required(t(VALIDATION_MESSAGE_KEYS.REQUIRED)),
});

0 comments on commit 13f1f5b

Please sign in to comment.