-
Notifications
You must be signed in to change notification settings - Fork 678
/
defaultRanges.js
120 lines (114 loc) · 2.9 KB
/
defaultRanges.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
addDays,
endOfDay,
startOfDay,
startOfMonth,
endOfMonth,
addMonths,
startOfWeek,
endOfWeek,
isSameDay,
differenceInCalendarDays,
} from 'date-fns';
const defineds = {
startOfWeek: startOfWeek(new Date()),
endOfWeek: endOfWeek(new Date()),
startOfLastWeek: startOfWeek(addDays(new Date(), -7)),
endOfLastWeek: endOfWeek(addDays(new Date(), -7)),
startOfToday: startOfDay(new Date()),
endOfToday: endOfDay(new Date()),
startOfYesterday: startOfDay(addDays(new Date(), -1)),
endOfYesterday: endOfDay(addDays(new Date(), -1)),
startOfMonth: startOfMonth(new Date()),
endOfMonth: endOfMonth(new Date()),
startOfLastMonth: startOfMonth(addMonths(new Date(), -1)),
endOfLastMonth: endOfMonth(addMonths(new Date(), -1)),
};
const staticRangeHandler = {
range: {},
isSelected(range) {
const definedRange = this.range();
return (
isSameDay(range.startDate, definedRange.startDate) &&
isSameDay(range.endDate, definedRange.endDate)
);
},
};
export function createStaticRanges(ranges) {
return ranges.map(range => ({ ...staticRangeHandler, ...range }));
}
export const defaultStaticRanges = createStaticRanges([
{
label: 'Today',
range: () => ({
startDate: defineds.startOfToday,
endDate: defineds.endOfToday,
}),
},
{
label: 'Yesterday',
range: () => ({
startDate: defineds.startOfYesterday,
endDate: defineds.endOfYesterday,
}),
},
{
label: 'This Week',
range: () => ({
startDate: defineds.startOfWeek,
endDate: defineds.endOfWeek,
}),
},
{
label: 'Last Week',
range: () => ({
startDate: defineds.startOfLastWeek,
endDate: defineds.endOfLastWeek,
}),
},
{
label: 'This Month',
range: () => ({
startDate: defineds.startOfMonth,
endDate: defineds.endOfMonth,
}),
},
{
label: 'Last Month',
range: () => ({
startDate: defineds.startOfLastMonth,
endDate: defineds.endOfLastMonth,
}),
},
]);
export const defaultInputRanges = [
{
label: 'days up to today',
range(value) {
return {
startDate: addDays(defineds.startOfToday, (Math.max(Number(value), 1) - 1) * -1),
endDate: defineds.endOfToday,
};
},
getCurrentValue(range) {
if (!isSameDay(range.endDate, defineds.endOfToday)) return '-';
if (!range.startDate) return '∞';
return differenceInCalendarDays(defineds.endOfToday, range.startDate) + 1;
},
},
{
label: 'days starting today',
range(value) {
const today = new Date();
return {
startDate: today,
endDate: addDays(today, Math.max(Number(value), 1) - 1),
};
},
getCurrentValue(range) {
if (!isSameDay(range.startDate, defineds.startOfToday)) return '-';
if (!range.endDate) return '∞';
return differenceInCalendarDays(range.endDate, defineds.startOfToday) + 1;
},
},
];