@@ -11,26 +11,22 @@ import {
11
11
Tooltip ,
12
12
} from 'recharts' ;
13
13
import { useTranslate } from 'react-admin' ;
14
+ import { format , subDays , addDays } from 'date-fns' ;
14
15
15
16
import { Order } from '../types' ;
16
17
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 ) ;
25
21
26
22
const dateFormatter = ( date : number ) : string =>
27
23
new Date ( date ) . toLocaleDateString ( ) ;
28
24
29
- const aggregateOrdersByDay = ( orders : Order [ ] ) : { [ key : number ] : number } =>
25
+ const aggregateOrdersByDay = ( orders : Order [ ] ) : { [ key : string ] : number } =>
30
26
orders
31
27
. filter ( ( order : Order ) => order . status !== 'cancelled' )
32
28
. reduce ( ( acc , curr ) => {
33
- const day = new Date ( new Date ( curr . date ) . toDateString ( ) ) . getTime ( ) ;
29
+ const day = format ( curr . date , 'YYYY-MM-DD' ) ;
34
30
if ( ! acc [ day ] ) {
35
31
acc [ day ] = 0 ;
36
32
}
@@ -41,8 +37,8 @@ const aggregateOrdersByDay = (orders: Order[]): { [key: number]: number } =>
41
37
const getRevenuePerDay = ( orders : Order [ ] ) : TotalByDay [ ] => {
42
38
const daysWithRevenue = aggregateOrdersByDay ( orders ) ;
43
39
return lastMonthDays . map ( date => ( {
44
- date,
45
- total : daysWithRevenue [ date ] || 0 ,
40
+ date : date . getTime ( ) ,
41
+ total : daysWithRevenue [ format ( date , 'YYYY-MM-DD' ) ] || 0 ,
46
42
} ) ) ;
47
43
} ;
48
44
@@ -83,11 +79,10 @@ const OrderChart: FC<{ orders?: Order[] }> = ({ orders }) => {
83
79
type = "number"
84
80
scale = "time"
85
81
domain = { [
86
- aMonthAgo . getTime ( ) ,
82
+ addDays ( aMonthAgo , 1 ) . getTime ( ) ,
87
83
new Date ( ) . getTime ( ) ,
88
84
] }
89
85
tickFormatter = { dateFormatter }
90
- reversed
91
86
/>
92
87
< YAxis dataKey = "total" name = "Revenue" unit = "€" />
93
88
< CartesianGrid strokeDasharray = "3 3" />
0 commit comments