-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathScheduler.js
More file actions
205 lines (177 loc) · 5.56 KB
/
Scheduler.js
File metadata and controls
205 lines (177 loc) · 5.56 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import moment from 'moment';
import defaults from './defaults';
function formatDayToString(m) {
return m.format('YYYY-MM-DD');
}
function formatHourTitle(h) {
return moment(String(h), ['HH']).format('ha');
}
moment.updateLocale(moment.locale(), {
calendar: {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
lastDay: '[Yesterday]',
lastWeek: '[last] dddd',
sameElse: 'L'
}
});
class Scheduler {
static getDaysFrame(frameStartDate) {
const mbase = moment.utc(frameStartDate);
const result = [];
for (let diff = 0; diff < defaults.daysInList; ++diff) {
const momentDay = moment(mbase).add(diff, 'days');
result.push({
val: formatDayToString(momentDay),
title: momentDay.calendar()
});
}
return result;
}
static getHoursFrame(dayPicked, hoursFrameStart) {
const mOrg = moment.utc(dayPicked).add(hoursFrameStart, 'hours');
const result = [];
for (let diff = 0; diff < defaults.hoursInList; ++diff) {
const hourItemMoment = mOrg.clone().add(diff, 'hours');
const day = formatDayToString(hourItemMoment);
const hour = hourItemMoment.hours();
result.push({
title: formatHourTitle(hour),
day,
hour
});
}
return result;
}
static getDayDataFromSlots(slots) {
const result = {};
slots.forEach((slot) => {
const slotMoment = moment.utc(slot);
const slotDate = formatDayToString(slotMoment);
if (!result[slotDate]) {
const newSlot = {
presentSlots: {},
presentHours: []
};
result[slotDate] = newSlot;
}
const { presentSlots, presentHours } = result[slotDate];
const h = slotMoment.hours();
if (!presentSlots[h]) {
presentSlots[h] = [];
presentHours.push(h);
}
const presentMinutes = presentSlots[h];
const minutes = slotMoment.minutes();
if (!~presentMinutes.indexOf(minutes))
presentMinutes.push(minutes);
});
return result;
}
static getNowStr() {
return moment().startOf('minute').format();
}
static getNextDayString(date) {
return formatDayToString(moment.utc(date).add(1, 'day'));
}
static getNextQMinString(date) {
return moment(date).add(15, 'minutes').format();
}
static getStructuredIncrement(date, hour, unit, increment) {
const m = moment.utc(date).add(hour, 'hours').add(increment, unit);
return { day: formatDayToString(m), hour: m.hours() };
}
static getRangeEndValue(day, hour, qMinutesIdx) {
if (undefined === day || undefined === hour || undefined === qMinutesIdx)
return undefined;
return moment(day).add(hour, 'hours').add(15 * qMinutesIdx, 'minutes').format();
}
static getRangeEndFormatted(dateStr) {
let result = '';
if (dateStr) {
const mVal = moment(dateStr);
const mToday = moment();
if (formatDayToString(mToday) === formatDayToString(mVal))
result = mVal.format('ha:mm');
else if (mToday.year() === mVal.year())
result = mVal.format('MM-DD ha:mm');
else
result = mVal.format('YY-MM-DD ha:mm');
}
return result;
}
static isAfterHour(date, hours, base) {
const momentToCompare = moment(date).add(hours, 'hours');
return moment(base).isAfter(momentToCompare, 'hour');
}
static isAfterMin(date, hours, minutes, base) {
const momentToCompare = moment(date).add(hours, 'hours').add(minutes, 'minutes');
return moment(base).isAfter(momentToCompare, 'minute');
}
static previousIsAfter(date, hours, dayNotHour, base) {
const unit = dayNotHour ? 'day' : 'hour';
const momentToCompare = moment(date).add(hours, 'hours').subtract(1, unit);
return moment(base).isAfter(momentToCompare, unit);
}
static parseEnteredDate(strValue) {
// also, format is valid: 2017-01-11T09:26:46Z
const mVal = moment(
strValue,
['ha:mm', 'MM-DD ha:mm', 'YYYY-MM-DD ha:mm', 'YYYY-MM-DD', 'YYYY-MM-DD HH:mm Z']
);
let result = false;
if (mVal.isValid()) {
const day = formatDayToString(mVal);
const hour = mVal.hour();
const qMinIdx = Math.trunc(mVal.minutes() / 15);
result = { day, hour, qMinIdx };
}
return result;
}
static parseEnteredDateAsArray(strValue) {
const { day, hour, qMinIdx } = this.parseEnteredDate(strValue);
return [day, hour, qMinIdx];
}
static parseEnteredDatePicked(strValue) {
const r = this.parseEnteredDate(strValue);
return r ? {
dayPicked: r.day,
hourPicked: r.hour,
minutesIdxPicked: r.qMinIdx
} : false;
}
static parseEnteredDateOrUndefined(value) {
return Scheduler.parseEnteredDatePicked(value) || {
dayPicked: undefined,
hourPicked: undefined,
minutesIdxPicked: undefined
};
}
static getDayStr(dateTimeStr) {
return formatDayToString(moment(dateTimeStr));
}
static getHour(dateTimeStr) {
return moment(dateTimeStr).hour();
}
static composeBookingData(startVal, endVal, settingDelay) {
const mStart = moment(startVal);
const mEnd = moment(endVal);
// '2017-01-13'
const date = formatDayToString(mStart);
// '13:15'
const time = mStart.format('HH:mm');
// length in minutes
const duration = settingDelay ?
moment.duration(mEnd.diff(mStart)).asMinutes()
:
undefined;
const daysToFetch = [formatDayToString(mStart)];
if (endVal) {
while (mStart.add(1, 'day').isSameOrBefore(mEnd, 'day'))
daysToFetch.push(formatDayToString(mStart));
}
return [{ date, time, duration }, daysToFetch];
}
}
export default Scheduler;