-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathweb.date.util.ts
More file actions
347 lines (285 loc) · 9.12 KB
/
web.date.util.ts
File metadata and controls
347 lines (285 loc) · 9.12 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import dayjs, { Dayjs } from "dayjs";
import { RRULE } from "@core/constants/core.constants";
import {
HOURS_AM_FORMAT,
HOURS_AM_SHORT_FORMAT,
YEAR_MONTH_DAY_FORMAT,
YMDHAM_FORMAT,
YMDHM_FORMAT,
} from "@core/constants/date.constants";
import { Categories_Event, Direction_Migrate } from "@core/types/event.types";
import { Option_Time } from "@web/common/types/util.types";
import { GRID_TIME_STEP } from "@web/views/Calendar/layout.constants";
import { roundToNext } from ".";
import { ACCEPTED_TIMES, OPTIONS_RECURRENCE } from "../constants/web.constants";
import { theme } from "../styles/theme";
import {
Recurrence_Selection,
Schema_SelectedDates,
} from "../types/web.event.types";
export const dateIsValid = (date: string) => {
const notNaN = !Number.isNaN(new Date(date).getTime());
const isValid = notNaN;
return isValid;
};
export const getColorsByHour = (currentHour: number) => {
const colors: string[] = [];
[...(new Array(24) as number[])].map((_, index) => {
const isCurrentHour = currentHour - 1 === index;
const color = isCurrentHour
? theme.color.text.accent
: theme.color.text.light;
colors.push(color);
return dayjs()
.startOf("day")
.add(index + 1, "hour")
.format(HOURS_AM_SHORT_FORMAT);
});
return colors;
};
export const getDatesByCategory = (
category: Categories_Event,
weekStart: Dayjs,
weekEnd: Dayjs,
) => {
if (category === Categories_Event.SOMEDAY_WEEK) {
return {
startDate: weekStart.format(YEAR_MONTH_DAY_FORMAT),
endDate: weekEnd.format(YEAR_MONTH_DAY_FORMAT),
};
}
if (category === Categories_Event.SOMEDAY_MONTH) {
return {
startDate: weekStart.startOf("month").format(YEAR_MONTH_DAY_FORMAT),
endDate: weekStart.endOf("month").format(YEAR_MONTH_DAY_FORMAT),
};
}
const { startDate, endDate } = _getNextWeekInSameMonth(weekStart);
return {
startDate: startDate.format(YEAR_MONTH_DAY_FORMAT),
endDate: endDate.format(YEAR_MONTH_DAY_FORMAT),
};
};
export const getDayjsByTimeValue = (timeValue: string) => {
return dayjs(`2000-01-01 ${timeValue}`, YMDHAM_FORMAT);
};
export const getDurationLabel = (start: string, end: string) => {
const _start = dayjs(`2000-00-00 ${start}`, YMDHM_FORMAT);
const _end = dayjs(`2000-00-00 ${end}`, YMDHM_FORMAT);
return _end.diff(_start, "minutes");
};
export const getEndTimeOptions = (): Option_Time[] => {
const options = ACCEPTED_TIMES.map((value) => {
const day = dayjs(`2000-00-00 ${value}`, YMDHM_FORMAT);
const label = day.format(HOURS_AM_FORMAT).replace(":00", "");
return {
value,
label: `${label} 18h 10m`,
};
});
return options;
};
export const getHourLabels = () => {
const day = dayjs();
return [...(new Array(23) as number[])].map((_, index) => {
return day
.startOf("day")
.add(index + 1, "hour")
.format(HOURS_AM_SHORT_FORMAT);
});
};
export const getMigrationDates = (
origDates: { startDate: string; endDate: string },
category: Categories_Event,
direction: Direction_Migrate,
) => {
const dates =
category === Categories_Event.SOMEDAY_WEEK
? _getWeeklyMigrationDates(origDates, direction)
: _getMonthlyMigrationDates(origDates, direction);
return {
startDate: dates.startDate.format(YEAR_MONTH_DAY_FORMAT),
endDate: dates.endDate.format(YEAR_MONTH_DAY_FORMAT),
};
};
export const getNextIntervalTimes = () => {
const currentMinute = dayjs().minute();
const nextInterval = roundToNext(currentMinute, GRID_TIME_STEP);
const start = dayjs().minute(nextInterval).second(0);
const end = start.add(1, "hour");
const startDate = start.format();
const endDate = end.format();
return { startDate, endDate };
};
export const getRecurrenceRule = (selection: Recurrence_Selection) => {
switch (selection) {
case Recurrence_Selection.WEEK:
return [RRULE.WEEK];
break;
case Recurrence_Selection.MONTH:
return [RRULE.MONTH];
break;
default:
throw Error("Invalid selection");
}
};
export const getRecurrenceOption = (rrule: string) => {
switch (rrule) {
case RRULE.WEEK:
return OPTIONS_RECURRENCE.WEEK;
break;
case RRULE.WEEKS_2:
return OPTIONS_RECURRENCE.WEEKS_2;
break;
case RRULE.WEEKS_3:
return OPTIONS_RECURRENCE.WEEKS_3;
break;
case RRULE.MONTH:
return OPTIONS_RECURRENCE.MONTH;
break;
default:
throw Error(`Invalid RRule: ${rrule.toString()}`);
}
};
export const getTimeLabel = (value: string) => value.replace(":00", "");
export const getTimeOptionByValue = (date: Dayjs): Option_Time => {
const value = dayjs(date).format(HOURS_AM_FORMAT);
const label = getTimeLabel(value);
return {
label,
value,
};
};
export const getTimeOptions = (): Option_Time[] => {
const options = ACCEPTED_TIMES.map((value) => {
const label = getTimeLabel(value);
return {
label,
value,
};
});
return options;
};
export const getTimesLabel = (startDate: string, endDate: string) => {
const start = _getTimeLabel(startDate);
const end = _getTimeLabel(endDate);
const startMinimal = _cleanStartMeridiem(start, end);
const label = `${startMinimal} - ${end}`;
return label;
};
export const getWeekRangeLabel = (weekStart: Dayjs, weekEnd: Dayjs) => {
const isSameMonth = weekStart.month() === weekEnd.month();
const start = weekStart.format("M.D");
const end = weekEnd.format(isSameMonth ? "D" : "M.D");
const label = start + " - " + end;
return label;
};
export const getCalendarHeadingLabel = (
start: Dayjs,
end: Dayjs,
now: Dayjs,
) => {
const startsThisYear = now.year() === start.year();
const endsThisYear = now.year() === end.year();
if (startsThisYear && endsThisYear) {
return start.format("MMMM");
} else if (startsThisYear || endsThisYear) {
const startLabel = start.format("MMM YY");
const endLabel = end.format("MMM YY");
return `${startLabel} - ${endLabel}`;
} else {
return start.format("MMMM YYYY");
}
};
export const mapToBackend = (s: Schema_SelectedDates) => {
if (s.isAllDay) {
return {
startDate: dayjs(s.startDate).format(YEAR_MONTH_DAY_FORMAT),
endDate: dayjs(s.endDate).format(YEAR_MONTH_DAY_FORMAT),
};
}
const { startDate, endDate } = _addTimesToDates(s);
return { startDate, endDate };
};
// uses inferred timezone and shortened string to
// convert to a string format that the backend/gcal/mongo accepts:
// '2022-02-04 12:15' -> '2022-02-04T12:15:00-06:00'
export const toUTCOffset = (date: string | Dayjs | Date) => {
if (typeof date === "string" || date instanceof Date) {
return dayjs(date).format();
} else return date.format(); // then already a DayJs object
};
const _addTimesToDates = (dt: Schema_SelectedDates) => {
const start = getDayjsByTimeValue(dt.startTime.value);
const startDate = dayjs(dt.startDate)
.hour(start.hour())
.minute(start.minute())
.format();
const end = getDayjsByTimeValue(dt.endTime.value);
const endDate = dayjs(dt.startDate)
.hour(end.hour())
.minute(end.minute())
.format();
return { startDate, endDate };
};
const _cleanStartMeridiem = (start: string, end: string) => {
const meridiems = [start.slice(-2), end.slice(-2)];
const verboseMeridiems = meridiems[0] === meridiems[1];
if (verboseMeridiems) {
return start.slice(0, -2);
}
return start;
};
const _getNextWeekInSameMonth = (weekStart: Dayjs) => {
let startDate: Dayjs;
const startOfMonth = weekStart.startOf("month");
const nextWeek = weekStart.add(1, "week");
const nextWeekStartOfMonth = nextWeek.startOf("month");
if (nextWeekStartOfMonth.isSame(startOfMonth, "month")) {
startDate = nextWeek;
} else {
startDate = weekStart.subtract(1, "week");
}
const endDate = startDate.add(6, "days");
return { startDate, endDate };
};
const _getTimeLabel = (date: string) => {
const orig = dayjs(date).format(HOURS_AM_FORMAT);
return orig.replace(":00", "");
};
const _getMonthlyMigrationDates = (
origDates: { startDate: string; endDate: string },
direction: Direction_Migrate,
) => {
const WEEK_START = 0;
let startDate: Dayjs;
if (direction === "forward") {
const nextMonth = dayjs(origDates.startDate)
.startOf("month")
.add(1, "month");
const buffer = 7; //ensures it's in the next month
const firstXOfNextMonth = nextMonth.day(WEEK_START + buffer);
startDate = firstXOfNextMonth;
} else {
const prevMonth = dayjs(origDates.startDate)
.startOf("month")
.subtract(1, "month");
const lastDayOfMonth = prevMonth.endOf("month");
const lastXOfMonth = lastDayOfMonth.day(WEEK_START);
if (lastDayOfMonth.date() < lastXOfMonth.date()) {
lastXOfMonth.subtract(7, "days");
}
startDate = lastXOfMonth;
}
const endDate = startDate.add(6, "days");
return { startDate, endDate };
};
const _getWeeklyMigrationDates = (
origDates: { startDate: string; endDate: string },
direction: Direction_Migrate,
) => {
const diff = direction === "forward" ? 7 : -7;
const startDate = dayjs(origDates.startDate).add(diff, "days");
const endDate = dayjs(origDates.endDate).add(diff, "days");
return { startDate, endDate };
};