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

[Demo] Fix chart gets empty at the end of the month #5465

Merged
merged 2 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions examples/demo/src/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
} from 'react';
import { useVersion, useDataProvider } from 'react-admin';
import { useMediaQuery, Theme } from '@material-ui/core';
import { subDays } from 'date-fns';

import Welcome from './Welcome';
import MonthlyRevenue from './MonthlyRevenue';
Expand Down Expand Up @@ -62,8 +63,7 @@ const Dashboard: FC = () => {
);

const fetchOrders = useCallback(async () => {
const aMonthAgo = new Date();
aMonthAgo.setDate(aMonthAgo.getDate() - 30);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, JS isn't smart enough to change month when we remove more days than the current day in the month.

const aMonthAgo = subDays(new Date(), 30);
const { data: recentOrders } = await dataProvider.getList<Order>(
'commands',
{
Expand Down
21 changes: 8 additions & 13 deletions examples/demo/src/dashboard/OrderChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,22 @@ import {
Tooltip,
} from 'recharts';
import { useTranslate } from 'react-admin';
import { format, subDays } from 'date-fns';

import { Order } from '../types';

const lastDay = new Date(new Date().toDateString()).getTime();
const oneDay = 24 * 60 * 60 * 1000;
const lastMonthDays = Array.from(
{ length: 30 },
(_, i) => lastDay - i * oneDay
).reverse();
const aMonthAgo = new Date();
aMonthAgo.setDate(aMonthAgo.getDate() - 30);
const lastDay = new Date();
const lastMonthDays = Array.from({ length: 30 }, (_, i) => subDays(lastDay, i));
const aMonthAgo = subDays(new Date(), 30);

const dateFormatter = (date: number): string =>
new Date(date).toLocaleDateString();

const aggregateOrdersByDay = (orders: Order[]): { [key: number]: number } =>
const aggregateOrdersByDay = (orders: Order[]): { [key: string]: number } =>
orders
.filter((order: Order) => order.status !== 'cancelled')
.reduce((acc, curr) => {
const day = new Date(new Date(curr.date).toDateString()).getTime();
const day = format(curr.date, 'YYYY-MM-DD');
if (!acc[day]) {
acc[day] = 0;
}
Expand All @@ -41,8 +37,8 @@ const aggregateOrdersByDay = (orders: Order[]): { [key: number]: number } =>
const getRevenuePerDay = (orders: Order[]): TotalByDay[] => {
const daysWithRevenue = aggregateOrdersByDay(orders);
return lastMonthDays.map(date => ({
date,
total: daysWithRevenue[date] || 0,
date: date.getTime(),
total: daysWithRevenue[format(date, 'YYYY-MM-DD')] || 0,
}));
};

Expand Down Expand Up @@ -87,7 +83,6 @@ const OrderChart: FC<{ orders?: Order[] }> = ({ orders }) => {
new Date().getTime(),
]}
tickFormatter={dateFormatter}
reversed
/>
<YAxis dataKey="total" name="Revenue" unit="€" />
<CartesianGrid strokeDasharray="3 3" />
Expand Down