Skip to content

Commit

Permalink
fix: remove end date autofill and fix validation (#3377)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtawast authored Oct 7, 2024
1 parent 57e5eca commit 8d334ef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
3 changes: 1 addition & 2 deletions frontend/benefit/applicant/browser-tests/page-model/step2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ class Step2 extends WizardStep {
startDate: string,
endDate: string
): Promise<void> {
await t.typeText(this.endDate, endDate);

await t.typeText(this.startDate, startDate);
await t.typeText(this.endDate, endDate);
}

public async fillEmploymentInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const ApplicationFormStep2: React.FC<DynamicFormStepComponentProps> = ({
clearBenefitValues,
clearCommissionValues,
clearPaySubsidyValues,
setEndDate,
handleSubmit,
handleSave,
handleBack,
Expand Down Expand Up @@ -70,7 +69,6 @@ const ApplicationFormStep2: React.FC<DynamicFormStepComponentProps> = ({
clearBenefitValues,
clearCommissionValues,
clearPaySubsidyValues,
setEndDate,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@ import {
} from 'benefit-shared/constants';
import { Application } from 'benefit-shared/types/application';
import { getErrorText } from 'benefit-shared/utils/forms';
import isAfter from 'date-fns/isAfter';
import isWithinInterval from 'date-fns/isWithinInterval';
import { FormikProps, useFormik } from 'formik';
import fromPairs from 'lodash/fromPairs';
import { TFunction } from 'next-i18next';
import React, { useState } from 'react';
import { Field } from 'shared/components/forms/fields/types';
import {
convertToUIDateFormat,
formatDate,
parseDate,
} from 'shared/utils/date.utils';
import { formatDate, parseDate } from 'shared/utils/date.utils';
import { focusAndScroll } from 'shared/utils/dom.utils';
import { isString } from 'shared/utils/type-guards';

Expand Down Expand Up @@ -55,7 +49,6 @@ type UseApplicationFormStep2Props = {
clearBenefitValues: () => void;
clearCommissionValues: () => void;
clearPaySubsidyValues: () => void;
setEndDate: () => void;
organizationType: ORGANIZATION_TYPES;
formik: FormikProps<Partial<Application>>;
};
Expand Down Expand Up @@ -198,29 +191,7 @@ const useApplicationFormStep2 = (
]);

const minEndDate = getMinEndDate(values.startDate);
const minEndDateFormatted = convertToUIDateFormat(minEndDate);
const maxEndDate = getMaxEndDate(values.startDate);
const endDate = parseDate(values.endDate);
const isEndDateEligible =
endDate &&
(maxEndDate
? isWithinInterval(endDate, { start: minEndDate, end: maxEndDate })
: isAfter(endDate, minEndDate));

const setEndDate = React.useCallback(() => {
if (!values.startDate && values.endDate) {
void setFieldValue(fields.endDate.name, '');
} else if (values.startDate && !isEndDateEligible) {
void setFieldValue(fields.endDate.name, minEndDateFormatted);
}
}, [
values.startDate,
values.endDate,
fields.endDate.name,
isEndDateEligible,
minEndDateFormatted,
setFieldValue,
]);

let language = SUPPORTED_LANGUAGES.FI;
switch (i18n.language) {
Expand Down Expand Up @@ -249,7 +220,6 @@ const useApplicationFormStep2 = (
clearCommissionValues,
clearPaySubsidyValues,
getErrorMessage,
setEndDate,
organizationType,
handleSubmit,
handleSave,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
PAY_SUBSIDY_GRANTED,
VALIDATION_MESSAGE_KEYS,
} from 'benefit-shared/constants';
import { validateDateWithinMonths } from 'benefit-shared/utils/dates';
import {
getDateFromDateString,
validateDateWithinMonths,
validateIsAfterOrOnDate,
} from 'benefit-shared/utils/dates';
import addDays from 'date-fns/addDays';
import { FinnishSSN } from 'finnish-ssn';
import { TFunction } from 'next-i18next';
import { NAMES_REGEX } from 'shared/constants';
Expand Down Expand Up @@ -51,9 +56,27 @@ export const getValidationSchema = (
test: (value = '') =>
validateDateWithinMonths(value, APPLICATION_START_DATE_WITHIN_MONTHS),
}),
[APPLICATION_FIELDS_STEP2_KEYS.END_DATE]: Yup.string().required(
t(VALIDATION_MESSAGE_KEYS.REQUIRED)
),
[APPLICATION_FIELDS_STEP2_KEYS.END_DATE]: Yup.string()
.required(t(VALIDATION_MESSAGE_KEYS.REQUIRED))
.test({
message: t(VALIDATION_MESSAGE_KEYS.DATE_MIN, {
min: '30 päivää aloituspäivästä',
}),
test: (endDate: string, context) =>
validateIsAfterOrOnDate(
endDate,
convertToUIDateFormat(
addDays(
getDateFromDateString(
String(
(context?.parent as { startDate?: string })?.startDate
) || '30.12.2100'
),
30
)
)
),
}),
[APPLICATION_FIELDS_STEP2_KEYS.ASSOCIATION_IMMEDIATE_MANAGER_CHECK]:
Yup.boolean()
.nullable()
Expand Down

0 comments on commit 8d334ef

Please sign in to comment.