| 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 |
1x
1x
1x
1x
1x
1x
1x
1x
1x
| import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ReactTooltip from 'react-tooltip';
import moment from 'moment';
import styled from 'styled-components';
import Day from './Day';
const Week = styled.div`
padding: 0;
margin: 1rem;
display: flex;
justify-content: center;
`;
const getFirstDayOfWeek = (timestamp) => {
const startOfMonth = moment.unix(timestamp).startOf('month');
const dayOfWeek = startOfMonth.day();
if (dayOfWeek !== 1) {
const daysFromMonday = dayOfWeek - 1;
startOfMonth.subtract(daysFromMonday, 'days');
}
return startOfMonth.unix();
};
const isInValidRange = (start, end, timestamp) => {
if (start === null) {
return true;
}
const date = moment.unix(timestamp);
// start date should always be inclusive
const maxStartDate = moment(start).subtract(1, 'days');
const maxEndDate = moment(end);
return date.isBetween(maxStartDate, maxEndDate);
};
const isInRange = (start, end, timestamp) => {
const startDate = moment.unix(start);
const endDate = moment.unix(end);
const date = moment.unix(timestamp);
if (startDate.isSame(date, 'day')) {
return -1;
} else if (date.isBetween(startDate, endDate) && start !== null && end !== null) {
return 0;
} else if (endDate.isSame(date, 'day')) {
return 1;
}
return false;
};
const isSelectedBetween = (start, end, timestamp) => isInRange(start, end, timestamp) === 0;
const isSelectedStart = (start, end, timestamp) => isInRange(start, end, timestamp) === -1;
const isSelectedEnd = (start, end, timestamp) => isInRange(start, end, timestamp) === 1;
class Month extends Component {
componentDidUpdate(nextProps) {
if (this.props.currentMonth !== nextProps.currentMonth) {
ReactTooltip.rebuild();
}
}
getDays(weekList) {
const handleDayClick = this.props.handleDayClick;
return weekList.map(day => (
<Day
{...day}
key={day.timestamp}
handleClick={handleDayClick}
/>
));
}
getMonth (unixTimestamp) {
const {
startDate,
endDate,
maxStartDate,
maxEndDate,
} = this.props;
const monthGrid = this.formatMonth(unixTimestamp, startDate, endDate, maxStartDate, maxEndDate);
return monthGrid.map((weekList) => {
const days = this.getDays(weekList);
return (
<Week key={weekList[0].timestamp}>
{days}
</Week>
);
});
}
formatMonth(unixTimestamp) {
const month = moment.unix(unixTimestamp).startOf('month').month();
const year = moment.unix(unixTimestamp).year();
const m = moment.unix(getFirstDayOfWeek(unixTimestamp));
const grid = [[]];
const {
startDate,
endDate,
maxStartDate,
maxEndDate,
} = this.props;
let endOfMonthGrid = false;
let endOfWeek = false;
let endOfMonth = false;
const mMaxStartDate = moment(maxStartDate);
while (!endOfMonthGrid) {
const weekIndex = grid.length - 1;
const showTooltip = m.isBefore(mMaxStartDate);
grid[weekIndex].push({
timestamp: m.unix(),
day: m.date(),
today: m.isSame(moment(), 'day'),
inMonth: m.month() === month,
selectedBetween: isSelectedBetween(startDate, endDate, m.unix()),
selectedStart: isSelectedStart(startDate, endDate, m.unix()),
selectedEnd: isSelectedEnd(startDate, endDate, m.unix()),
isDisabled: !isInValidRange(maxStartDate, maxEndDate, m.unix()),
disabledText: showTooltip ? `We don't have data prior to ${mMaxStartDate.format('MM/DD/YY')}` : '',
startDate,
endDate,
});
endOfWeek = m.day() === 0;
endOfMonth = m.month() > month || m.year() > year;
if (endOfWeek && endOfMonth) {
endOfMonthGrid = true;
} else if (endOfWeek) {
grid.push([]);
}
m.add(1, 'day');
}
return grid;
}
render () {
const {
currentMonth,
} = this.props;
return (
<div>
{this.getMonth(currentMonth)}
</div>
);
}
}
Month.defaultProps = {
startDate: null,
endDate: null,
};
Month.propTypes = {
currentMonth: PropTypes.number.isRequired,
startDate: PropTypes.number,
endDate: PropTypes.number,
maxStartDate: PropTypes.number.isRequired,
maxEndDate: PropTypes.number.isRequired,
// Actions
handleDayClick: PropTypes.func.isRequired,
};
export default Month;
|