Skip to content
Snippets Groups Projects
Unverified Commit 04273d80 authored by Neil's avatar Neil Committed by GitHub
Browse files

Date Range error catch (#2769)

* Date Range error catch

* notes

* update checkDate
parent 3f1fd55a
No related branches found
No related tags found
No related merge requests found
...@@ -3,35 +3,56 @@ import React, { type ReactElement } from 'react'; ...@@ -3,35 +3,56 @@ import React, { type ReactElement } from 'react';
import * as d from 'date-fns'; import * as d from 'date-fns';
import { theme } from '../../style'; import { theme } from '../../style';
import { styles } from '../../style/styles';
import { Block } from '../common/Block'; import { Block } from '../common/Block';
import { Text } from '../common/Text';
type DateRangeProps = { type DateRangeProps = {
start: string; start: string;
end: string; end: string;
}; };
export function DateRange({ function checkDate(date: string) {
start: startProp, const dateParsed = new Date(date);
end: endProp, if (dateParsed.toString() !== 'Invalid Date') {
}: DateRangeProps): ReactElement { return d.format(dateParsed, 'yyyy-MM-dd');
const start = d.parseISO(startProp); } else {
const end = d.parseISO(endProp); return null;
}
}
export function DateRange({ start, end }: DateRangeProps): ReactElement {
const checkStart = checkDate(start);
const checkEnd = checkDate(end);
let startDate;
let endDate;
if (checkStart && checkEnd) {
startDate = d.parseISO(checkStart);
endDate = d.parseISO(checkEnd);
} else {
return (
<Text style={{ ...styles.mediumText, color: theme.errorText }}>
There was a problem loading your date range
</Text>
);
}
let content: string | ReactElement; let content: string | ReactElement;
if (start.getFullYear() !== end.getFullYear()) { if (startDate.getFullYear() !== endDate.getFullYear()) {
content = ( content = (
<div> <div>
{d.format(start, 'MMM yyyy')} - {d.format(end, 'MMM yyyy')} {d.format(startDate, 'MMM yyyy')} - {d.format(endDate, 'MMM yyyy')}
</div> </div>
); );
} else if (start.getMonth() !== end.getMonth()) { } else if (startDate.getMonth() !== endDate.getMonth()) {
content = ( content = (
<div> <div>
{d.format(start, 'MMM')} - {d.format(end, 'MMM yyyy')} {d.format(startDate, 'MMM yyyy')} - {d.format(endDate, 'MMM yyyy')}
</div> </div>
); );
} else { } else {
content = d.format(end, 'MMMM yyyy'); content = d.format(endDate, 'MMMM yyyy');
} }
return <Block style={{ color: theme.pageTextSubdued }}>{content}</Block>; return <Block style={{ color: theme.pageTextSubdued }}>{content}</Block>;
......
...@@ -108,13 +108,13 @@ function boundedRange( ...@@ -108,13 +108,13 @@ function boundedRange(
latest = monthUtils.currentWeek(firstDayOfWeekIdx); latest = monthUtils.currentWeek(firstDayOfWeekIdx);
break; break;
case 'Monthly': case 'Monthly':
latest = monthUtils.currentMonth() + '-31'; latest = monthUtils.getMonthEnd(monthUtils.currentDay());
break; break;
case 'Yearly': case 'Yearly':
latest = monthUtils.currentDay(); latest = monthUtils.currentDay();
break; break;
default: default:
latest = monthUtils.currentMonth(); latest = monthUtils.currentDay();
break; break;
} }
......
---
category: Bugfix
authors: [carkom]
---
Updating daterange element to catch any incorrectly formated dates. Current state crashes app when dates are invalid.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment