diff --git a/frontend/benefit/applicant/browser-tests/page-model/step2.ts b/frontend/benefit/applicant/browser-tests/page-model/step2.ts index f745d606c0..af6d47ac02 100644 --- a/frontend/benefit/applicant/browser-tests/page-model/step2.ts +++ b/frontend/benefit/applicant/browser-tests/page-model/step2.ts @@ -131,9 +131,8 @@ class Step2 extends WizardStep { startDate: string, endDate: string ): Promise { - await t.typeText(this.endDate, endDate); - await t.typeText(this.startDate, startDate); + await t.typeText(this.endDate, endDate); } public async fillEmploymentInfo( diff --git a/frontend/benefit/applicant/src/components/applications/forms/application/step2/ApplicationFormStep2.tsx b/frontend/benefit/applicant/src/components/applications/forms/application/step2/ApplicationFormStep2.tsx index e8b52674be..697957a78e 100644 --- a/frontend/benefit/applicant/src/components/applications/forms/application/step2/ApplicationFormStep2.tsx +++ b/frontend/benefit/applicant/src/components/applications/forms/application/step2/ApplicationFormStep2.tsx @@ -41,7 +41,6 @@ const ApplicationFormStep2: React.FC = ({ clearBenefitValues, clearCommissionValues, clearPaySubsidyValues, - setEndDate, handleSubmit, handleSave, handleBack, @@ -70,7 +69,6 @@ const ApplicationFormStep2: React.FC = ({ clearBenefitValues, clearCommissionValues, clearPaySubsidyValues, - setEndDate, } ); diff --git a/frontend/benefit/applicant/src/components/applications/forms/application/step2/useApplicationFormStep2.ts b/frontend/benefit/applicant/src/components/applications/forms/application/step2/useApplicationFormStep2.ts index e1a19551c1..a2e36bff7a 100644 --- a/frontend/benefit/applicant/src/components/applications/forms/application/step2/useApplicationFormStep2.ts +++ b/frontend/benefit/applicant/src/components/applications/forms/application/step2/useApplicationFormStep2.ts @@ -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'; @@ -55,7 +49,6 @@ type UseApplicationFormStep2Props = { clearBenefitValues: () => void; clearCommissionValues: () => void; clearPaySubsidyValues: () => void; - setEndDate: () => void; organizationType: ORGANIZATION_TYPES; formik: FormikProps>; }; @@ -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) { @@ -249,7 +220,6 @@ const useApplicationFormStep2 = ( clearCommissionValues, clearPaySubsidyValues, getErrorMessage, - setEndDate, organizationType, handleSubmit, handleSave, diff --git a/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts b/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts index 6cd6a65077..8a2f5440d9 100644 --- a/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts +++ b/frontend/benefit/applicant/src/components/applications/forms/application/step2/utils/validation.ts @@ -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'; @@ -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()