Skip to content

Commit

Permalink
date bugfixes (#241)
Browse files Browse the repository at this point in the history
* date bugfixes

* consolidated end date and archive date effects
  • Loading branch information
sdstolworthy authored Jul 6, 2020
1 parent e7867cd commit 4b6892d
Showing 1 changed file with 49 additions and 54 deletions.
103 changes: 49 additions & 54 deletions src/components/engagement_form_fields/engagement_start_end_date.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -171,15 +164,17 @@ export function EngagementStartEndDateFormField({
>
<InputGroup label="Retirement Date">
<TextInput
id="archive_date"
isDisabled={!hasFeature(APP_FEATURES.writer)}
type="date"
name="archive_date"
aria-label="Environment Retirement Date"
value={archiveDateText}
onBlur={e => {
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);
Expand Down

0 comments on commit 4b6892d

Please sign in to comment.