From 83428b43f5a4c0a2ed0c8f0ce303d88280fa2645 Mon Sep 17 00:00:00 2001 From: mgaseta Date: Thu, 1 Aug 2024 14:54:52 -0300 Subject: [PATCH] fix: adding validation to interim storage end date field --- .../InterimStep/constants.ts | 3 ++- .../InterimStep/index.tsx | 23 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/frontend/src/components/SeedlotRegistrationSteps/InterimStep/constants.ts b/frontend/src/components/SeedlotRegistrationSteps/InterimStep/constants.ts index fb422f581..7d633b842 100644 --- a/frontend/src/components/SeedlotRegistrationSteps/InterimStep/constants.ts +++ b/frontend/src/components/SeedlotRegistrationSteps/InterimStep/constants.ts @@ -29,7 +29,8 @@ export const pageTexts = { labelTextEnd: 'Storage end date', placeholder: 'yyyy/mm/dd', helperText: 'year/month/day', - invalidText: 'Please enter a valid date' + invalidText: 'Please enter a valid date', + invalidDateBeforeCollection: 'The storage end date can\'t be set before collection end date' }, storageFacility: { labelText: 'Storage facility type', diff --git a/frontend/src/components/SeedlotRegistrationSteps/InterimStep/index.tsx b/frontend/src/components/SeedlotRegistrationSteps/InterimStep/index.tsx index 732d7a03f..5886c1429 100644 --- a/frontend/src/components/SeedlotRegistrationSteps/InterimStep/index.tsx +++ b/frontend/src/components/SeedlotRegistrationSteps/InterimStep/index.tsx @@ -42,11 +42,15 @@ const InterimStep = ({ isReview }:InterimStepProps) => { allStepData: { interimStep: state }, allStepData: { collectionStep: { collectorAgency } }, allStepData: { collectionStep: { locationCode: collectorCode } }, + allStepData: { collectionStep: { endDate } }, setStepData, isFormSubmitted } = useContext(ClassAContext); const [otherChecked, setOtherChecked] = useState(state.facilityType.value === 'OTH'); + const [endDateErrMessage, setEndDateErrMessage] = useState( + pageTexts.storageDate.invalidText + ); const today = new Date(); const maxDate = today.toISOString().split('T')[0].replace(/-/g, '/'); @@ -85,6 +89,21 @@ const InterimStep = ({ isReview }:InterimStepProps) => { const isInvalid = validateStorageDates(clonedState); clonedState.startDate.isInvalid = isInvalid; clonedState.endDate.isInvalid = isInvalid; + if (clonedState.endDate.isInvalid + && endDateErrMessage === pageTexts.storageDate.invalidDateBeforeCollection + ) { + setEndDateErrMessage(pageTexts.storageDate.invalidText); + } + // Validate if end date is after collection end date + if (!isStart && !isInvalid) { + clonedState.endDate.isInvalid = moment(clonedState.endDate.value, 'YYYY/MM/DD') + .isBefore(moment(endDate.value, 'YYYY/MM/DD')); + if (clonedState.endDate.isInvalid + && endDateErrMessage === pageTexts.storageDate.invalidText + ) { + setEndDateErrMessage(pageTexts.storageDate.invalidDateBeforeCollection); + } + } setStepData('interimStep', clonedState); }; @@ -218,8 +237,8 @@ const InterimStep = ({ isReview }:InterimStepProps) => { labelText={pageTexts.storageDate.labelTextEnd} helperText={pageTexts.storageDate.helperText} placeholder={pageTexts.storageDate.placeholder} - invalid={state.startDate.isInvalid} - invalidText={pageTexts.storageDate.invalidText} + invalid={state.endDate.isInvalid} + invalidText={endDateErrMessage} readOnly={isFormSubmitted} autoComplete="off" />