Skip to content

Commit

Permalink
fix: adding validation to interim storage end date field
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaseta committed Aug 1, 2024
1 parent 4257042 commit 83428b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(
pageTexts.storageDate.invalidText
);

const today = new Date();
const maxDate = today.toISOString().split('T')[0].replace(/-/g, '/');
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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"
/>
Expand Down

0 comments on commit 83428b4

Please sign in to comment.