Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date Range error catch #2769

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions packages/desktop-client/src/components/reports/DateRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,56 @@ import React, { type ReactElement } from 'react';
import * as d from 'date-fns';

import { theme } from '../../style';
import { styles } from '../../style/styles';
import { Block } from '../common/Block';
import { Text } from '../common/Text';

type DateRangeProps = {
start: string;
end: string;
};

export function DateRange({
start: startProp,
end: endProp,
}: DateRangeProps): ReactElement {
const start = d.parseISO(startProp);
const end = d.parseISO(endProp);
function checkDate(date: string) {
const dateParsed = new Date(date);
if (dateParsed.toString() !== 'Invalid Date') {
return d.format(dateParsed, 'yyyy-MM-dd');
} else {
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;
if (start.getFullYear() !== end.getFullYear()) {
if (startDate.getFullYear() !== endDate.getFullYear()) {
content = (
<div>
{d.format(start, 'MMM yyyy')} - {d.format(end, 'MMM yyyy')}
{d.format(startDate, 'MMM yyyy')} - {d.format(endDate, 'MMM yyyy')}
</div>
);
} else if (start.getMonth() !== end.getMonth()) {
} else if (startDate.getMonth() !== endDate.getMonth()) {
content = (
<div>
{d.format(start, 'MMM')} - {d.format(end, 'MMM yyyy')}
{d.format(startDate, 'MMM yyyy')} - {d.format(endDate, 'MMM yyyy')}
</div>
);
} else {
content = d.format(end, 'MMMM yyyy');
content = d.format(endDate, 'MMMM yyyy');
}

return <Block style={{ color: theme.pageTextSubdued }}>{content}</Block>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ function boundedRange(
latest = monthUtils.currentWeek(firstDayOfWeekIdx);
break;
case 'Monthly':
latest = monthUtils.currentMonth() + '-31';
latest = monthUtils.getMonthEnd(monthUtils.currentDay());
break;
case 'Yearly':
latest = monthUtils.currentDay();
break;
default:
latest = monthUtils.currentMonth();
latest = monthUtils.currentDay();
break;
}

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2769.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [carkom]
---

Updating daterange element to catch any incorrectly formated dates. Current state crashes app when dates are invalid.
Loading