diff --git a/src/components/engagement_form_fields/engagement_start_end_date.tsx b/src/components/engagement_form_fields/engagement_start_end_date.tsx index b103dda2e..5e8d0b838 100644 --- a/src/components/engagement_form_fields/engagement_start_end_date.tsx +++ b/src/components/engagement_form_fields/engagement_start_end_date.tsx @@ -37,30 +37,6 @@ export function EngagementStartEndDateFormField({ 'env_grace_period_max' ] as number) ?? 0; const { start_date, end_date, archive_date } = engagement ?? {}; - const getRetirementDate = useCallback(() => { - if (!end_date || !(end_date instanceof Date)) { - return undefined; - } else if (end_date && archive_date && archive_date <= end_date) { - return addDays(end_date, gracePeriodInDays); - } else if ( - archive_date && - end_date && - archive_date >= addDays(end_date, maxGracePeriodInDays) - ) { - return addDays(end_date, maxGracePeriodInDays); - } else if (archive_date || retirementDateChanged) { - return archive_date; - } else if (end_date) { - return addDays(end_date, gracePeriodInDays); - } - return undefined; - }, [ - archive_date, - end_date, - gracePeriodInDays, - maxGracePeriodInDays, - retirementDateChanged, - ]); const [startDateText, setStartDateText] = useState( getFormattedDate(start_date) || getFormattedDate(new Date()) @@ -69,39 +45,56 @@ export function EngagementStartEndDateFormField({ getFormattedDate(end_date) || '' ); const [archiveDateText, setArchiveDateText] = useState( - getFormattedDate(getRetirementDate()) - ); - useEffect( - () => - setStartDateText( - getFormattedDate(start_date) || getFormattedDate(new Date()) - ), - [start_date, setStartDateText] + getFormattedDate(archive_date) || getFormattedDate(new Date()) ); - useEffect( - () => - setEndDateText( - getFormattedDate(end_date) || getFormattedDate(new Date()) - ), - [end_date, setEndDateText] + + const normalizeRetirementDate = useCallback( + (retirementDate: Date) => { + if (!end_date || !(end_date instanceof Date)) { + return new Date(); + } else if (end_date && retirementDate && retirementDate <= end_date) { + return addDays(end_date, gracePeriodInDays); + } else if ( + retirementDate && + end_date && + retirementDate >= addDays(end_date, maxGracePeriodInDays) + ) { + return addDays(end_date, maxGracePeriodInDays); + } else if (retirementDate || retirementDateChanged) { + return retirementDate; + } else if (end_date) { + return addDays(end_date, gracePeriodInDays); + } + return new Date(); + }, + [end_date, gracePeriodInDays, maxGracePeriodInDays, retirementDateChanged] ); + + // if the end date is less than the start date, set the end date to the start date useEffect(() => { - const formattedArchiveDate = getFormattedDate(getRetirementDate()); - if (formattedArchiveDate !== archiveDateText) { - setArchiveDateText(getFormattedDate(getRetirementDate())); + if (end_date < start_date) { + onChange('end_date', start_date); } - }, [setArchiveDateText, getRetirementDate, archiveDateText]); + }, [start_date, end_date, onChange]); + // when the archive date or end date changes, check that it is within bounds. If not, update it useEffect(() => { - if (end_date) { - const retirementDate = getRetirementDate(); - if (getFormattedDate(retirementDate) !== getFormattedDate(archive_date)) { - onChange('archive_date', retirementDate); - } - } else { - onChange('end_date', new Date()); + const normalizedRetirementDate = normalizeRetirementDate(archive_date); + + if (archive_date?.valueOf() !== normalizedRetirementDate.valueOf()) { + onChange('archive_date', normalizedRetirementDate); } - }, [onChange, getRetirementDate, end_date, archive_date]); + }, [archive_date, normalizeRetirementDate, onChange, end_date]); + + useEffect(() => { + setEndDateText(getFormattedDate(end_date)); + }, [end_date]); + useEffect(() => { + setStartDateText(getFormattedDate(start_date)); + }, [start_date]); + useEffect(() => { + setArchiveDateText(getFormattedDate(archive_date)); + }, [archive_date]); const getMaxRetirementDate = () => { return maxGracePeriodInDays @@ -171,15 +164,17 @@ export function EngagementStartEndDateFormField({ > { - onChange( - 'archive_date', - parseDate(e.target.value, 'yyyy-MM-dd', 0) - ); + const parsedDate = parseDate(e.target.value, 'yyyy-MM-dd', 0); + const retirementDate = normalizeRetirementDate(parsedDate); + validate('archive_date')(retirementDate); + onChange('archive_date', retirementDate); }} onChange={e => { setRetirementDateChanged(true);