Skip to content

Commit 943111e

Browse files
authored
Merge pull request #5465 from marmelab/fix-demo-revenue-chart
[Demo] Fix chart gets empty at the end of the month
2 parents 8a29b67 + a4c809d commit 943111e

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

examples/demo/src/dashboard/Dashboard.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, {
77
} from 'react';
88
import { useVersion, useDataProvider } from 'react-admin';
99
import { useMediaQuery, Theme } from '@material-ui/core';
10+
import { subDays } from 'date-fns';
1011

1112
import Welcome from './Welcome';
1213
import MonthlyRevenue from './MonthlyRevenue';
@@ -62,8 +63,7 @@ const Dashboard: FC = () => {
6263
);
6364

6465
const fetchOrders = useCallback(async () => {
65-
const aMonthAgo = new Date();
66-
aMonthAgo.setDate(aMonthAgo.getDate() - 30);
66+
const aMonthAgo = subDays(new Date(), 30);
6767
const { data: recentOrders } = await dataProvider.getList<Order>(
6868
'commands',
6969
{

examples/demo/src/dashboard/NewCustomers.tsx

+7-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { makeStyles } from '@material-ui/core/styles';
1313
import CustomerIcon from '@material-ui/icons/PersonAdd';
1414
import { Link } from 'react-router-dom';
1515
import { useTranslate, useQueryWithStore } from 'react-admin';
16+
import { subDays } from 'date-fns';
1617

1718
import CardWithIcon from './CardWithIcon';
1819
import { Customer } from '../types';
@@ -21,15 +22,12 @@ const NewCustomers = () => {
2122
const translate = useTranslate();
2223
const classes = useStyles();
2324

24-
const aMonthAgo = useMemo(() => {
25-
const date = new Date();
26-
date.setDate(date.getDate() - 30);
27-
date.setHours(0);
28-
date.setMinutes(0);
29-
date.setSeconds(0);
30-
date.setMilliseconds(0);
31-
return date;
32-
}, []);
25+
const aMonthAgo = subDays(new Date(), 30);
26+
aMonthAgo.setDate(aMonthAgo.getDate() - 30);
27+
aMonthAgo.setHours(0);
28+
aMonthAgo.setMinutes(0);
29+
aMonthAgo.setSeconds(0);
30+
aMonthAgo.setMilliseconds(0);
3331

3432
const { loaded, data: visitors } = useQueryWithStore({
3533
type: 'getList',

examples/demo/src/dashboard/OrderChart.tsx

+9-14
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,22 @@ import {
1111
Tooltip,
1212
} from 'recharts';
1313
import { useTranslate } from 'react-admin';
14+
import { format, subDays, addDays } from 'date-fns';
1415

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

17-
const lastDay = new Date(new Date().toDateString()).getTime();
18-
const oneDay = 24 * 60 * 60 * 1000;
19-
const lastMonthDays = Array.from(
20-
{ length: 30 },
21-
(_, i) => lastDay - i * oneDay
22-
).reverse();
23-
const aMonthAgo = new Date();
24-
aMonthAgo.setDate(aMonthAgo.getDate() - 30);
18+
const lastDay = new Date();
19+
const lastMonthDays = Array.from({ length: 30 }, (_, i) => subDays(lastDay, i));
20+
const aMonthAgo = subDays(new Date(), 30);
2521

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

29-
const aggregateOrdersByDay = (orders: Order[]): { [key: number]: number } =>
25+
const aggregateOrdersByDay = (orders: Order[]): { [key: string]: number } =>
3026
orders
3127
.filter((order: Order) => order.status !== 'cancelled')
3228
.reduce((acc, curr) => {
33-
const day = new Date(new Date(curr.date).toDateString()).getTime();
29+
const day = format(curr.date, 'YYYY-MM-DD');
3430
if (!acc[day]) {
3531
acc[day] = 0;
3632
}
@@ -41,8 +37,8 @@ const aggregateOrdersByDay = (orders: Order[]): { [key: number]: number } =>
4137
const getRevenuePerDay = (orders: Order[]): TotalByDay[] => {
4238
const daysWithRevenue = aggregateOrdersByDay(orders);
4339
return lastMonthDays.map(date => ({
44-
date,
45-
total: daysWithRevenue[date] || 0,
40+
date: date.getTime(),
41+
total: daysWithRevenue[format(date, 'YYYY-MM-DD')] || 0,
4642
}));
4743
};
4844

@@ -83,11 +79,10 @@ const OrderChart: FC<{ orders?: Order[] }> = ({ orders }) => {
8379
type="number"
8480
scale="time"
8581
domain={[
86-
aMonthAgo.getTime(),
82+
addDays(aMonthAgo, 1).getTime(),
8783
new Date().getTime(),
8884
]}
8985
tickFormatter={dateFormatter}
90-
reversed
9186
/>
9287
<YAxis dataKey="total" name="Revenue" unit="€" />
9388
<CartesianGrid strokeDasharray="3 3" />

0 commit comments

Comments
 (0)