react-weekly-planning 1.0.31 → 1.0.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/utils.js CHANGED
@@ -1,460 +1,465 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
- import moment from "moment";
13
- import dayjs from "dayjs";
14
- import { v4 as uuidv4 } from "uuid";
15
- export const DAY_IN_MILLISECONDS = 86400000;
16
- export const WEEK_IN_MILLISECONDS = DAY_IN_MILLISECONDS * 7;
17
- // Get the current date
18
- const currentDate = new Date();
19
- // Get the day of the week (Sunday = 0, Monday = 1, ..., Saturday = 6)
20
- const currentDayOfWeek = currentDate.getDay();
21
- // Calculate the start date of the week in milliseconds
22
- const startDate = new Date(currentDate);
23
- startDate.setDate(startDate.getDate() - currentDayOfWeek);
24
- startDate.setHours(0, 0, 0, 0);
25
- export const startDateMilliseconds = startDate.getTime();
26
- // Calculate the end date of the week in milliseconds
27
- const endDate = new Date(currentDate);
28
- endDate.setDate(endDate.getDate() + (6 - currentDayOfWeek));
29
- endDate.setHours(23, 59, 59, 999);
30
- export const endDateMilliseconds = endDate.getTime();
31
- export function getDayHourly(weekOffset) {
32
- const dailyHours = [];
33
- let dayOffset = weekOffset;
34
- // Adjust the offset if the current day is Sunday
35
- if (currentDate.getDay() === 0) {
36
- dayOffset = dayOffset - 7;
37
- }
38
- // Loop to calculate the start and end hours for each day of the week
39
- for (let i = 0; i < 7; i++) {
40
- const dayDate = new Date(startDate);
41
- dayDate.setDate(startDate.getDate() + i);
42
- const dayStart = new Date(dayDate);
43
- dayStart.setHours(1, 0, 0, 0);
44
- const dayEnd = new Date(dayDate);
45
- dayEnd.setHours(23, 59, 59, 59);
46
- dailyHours.push({
47
- positionDay: i,
48
- day: new Date(dayStart.getTime() + dayOffset * DAY_IN_MILLISECONDS),
49
- start: dayStart.getTime() + dayOffset * DAY_IN_MILLISECONDS,
50
- end: dayEnd.getTime() + dayOffset * DAY_IN_MILLISECONDS,
51
- });
52
- }
53
- return dailyHours;
54
- }
55
- // Convert milliseconds to a readable date format
56
- export function millisecondsToDate(milliseconds) {
57
- const date = new Date(milliseconds);
58
- const daysOfWeek = ["Mon", "Tues", "Wed", "Thur", "Frid", "Sat", "Sun"];
59
- const dayOfWeek = daysOfWeek[date.getDay()];
60
- let hours = date.getHours();
61
- const minutes = date.getMinutes();
62
- const formattedDate = `${hours.toString().padStart(2, "0")}h:${minutes
63
- .toString()
64
- .padStart(2, "0")}`;
65
- return { formattedDate, dayOfWeek };
66
- }
67
- // Convert milliseconds to integer representation
68
- export function millisecondsToInt(milliseconds) {
69
- const date = new Date(milliseconds);
70
- const daysOfWeek = [
71
- "Sunday",
72
- "Monday",
73
- "Tuesday",
74
- "Wednesday",
75
- "Thursday",
76
- "Friday",
77
- "Saturday",
78
- ];
79
- const months = [
80
- "January",
81
- "February",
82
- "March",
83
- "April",
84
- "May",
85
- "June",
86
- "July",
87
- "August",
88
- "September",
89
- "October",
90
- "November",
91
- "December",
92
- ];
93
- const dayOfWeek = daysOfWeek[date.getDay()];
94
- const dayOfMonth = date.getDate();
95
- const month = months[date.getMonth()];
96
- let hours = date.getHours();
97
- const amOrPm = hours >= 12 ? " pm" : " am";
98
- const minutes = date.getMinutes();
99
- const formattedDate = hours.toString().padStart(2, "0");
100
- return { formattedDate, dayOfWeek };
101
- }
102
- // Get days of the week with a jump offset
103
- export function getWeekDays(jump) {
104
- const days = ["Sun", "Mon", "Tues", "Wed", "Thur", "Frid", "Sat"];
105
- const month = [
106
- "Jan",
107
- "Feb",
108
- "Mar",
109
- "Apr",
110
- "May",
111
- "Jun",
112
- "Jul",
113
- "Aug",
114
- "Sept",
115
- "Oct",
116
- "Nov",
117
- "Dec",
118
- ];
119
- const currentDate = new Date();
120
- const currentDayOfWeek = currentDate.getDay();
121
- let weekDays = [];
122
- for (let i = 0; i < 7; i++) {
123
- const day = new Date();
124
- const diff = i - currentDayOfWeek;
125
- if (currentDayOfWeek === 0) {
126
- day.setDate(currentDate.getDate() + diff + jump - 7);
127
- }
128
- else {
129
- day.setDate(currentDate.getDate() + diff + jump);
130
- }
131
- const formattedDay = `${days[day.getDay()]}. ${day.getDate()}, ${month[day.getMonth()]} ${day.getFullYear()}`;
132
- weekDays.push({
133
- day: days[day.getDay()],
134
- dayMonth: month[day.getMonth()],
135
- dayYear: day.getFullYear(),
136
- dayOfTheMonth: day.getDate(),
137
- });
138
- }
139
- return weekDays;
140
- }
141
- // The remaining functions follow the same structure. Ensure all comments are in English and make the functions exportable as required.
142
- /**
143
- * Get the ISO week number for a given date.
144
- * @param date - The date to get the week number for.
145
- * @returns The ISO week number.
146
- */
147
- function getWeekNumber(date) {
148
- const tempDate = new Date(date.getTime());
149
- tempDate.setHours(0, 0, 0, 0);
150
- // Thursday in current week decides the year
151
- tempDate.setDate(tempDate.getDate() + 4 - (tempDate.getDay() || 7));
152
- const yearStart = new Date(tempDate.getFullYear(), 0, 1);
153
- const weekNumber = Math.ceil(((tempDate.getTime() - yearStart.getTime()) / DAY_IN_MILLISECONDS + 1) / 7);
154
- return weekNumber;
155
- }
156
- /**
157
- * Update the selected date to avoid issues with weeks starting on Sunday.
158
- * @param dateSelectionnee - The selected date.
159
- * @returns The updated date.
160
- */
161
- function updateSelectedDateForEcartSemaine(dateSelectionnee) {
162
- const updatedDate = new Date(dateSelectionnee.getTime());
163
- if (updatedDate.getDay() === 0) {
164
- updatedDate.setDate(updatedDate.getDate() + 1);
165
- }
166
- return updatedDate;
167
- }
168
- /**
169
- * Calculate the week difference between the selected date and the current date.
170
- * @param dateSelectionnee - The selected date.
171
- * @returns The week difference in days.
172
- */
173
- export function calculerEcartSemaine(dateSelectionnee) {
174
- if (!dateSelectionnee) {
175
- return 0;
176
- }
177
- const selectedDateUpdated = updateSelectedDateForEcartSemaine(dateSelectionnee);
178
- const dateActuelle = new Date();
179
- const anneeActuelle = dateActuelle.getFullYear();
180
- const numeroSemaineActuelle = getWeekNumber(dateActuelle);
181
- const anneeSelectionnee = selectedDateUpdated.getFullYear();
182
- const numeroSemaineSelectionnee = getWeekNumber(selectedDateUpdated);
183
- const ecartSemaine = semainesDepuisOrigine(anneeSelectionnee, numeroSemaineSelectionnee) -
184
- semainesDepuisOrigine(anneeActuelle, numeroSemaineActuelle);
185
- return ecartSemaine * 7;
186
- }
187
- /**
188
- * Calculate the number of weeks since an arbitrary origin date (January 1, 2022).
189
- * @param annee - The year.
190
- * @param numeroSemaine - The week number.
191
- * @returns The number of weeks since the origin date.
192
- */
193
- function semainesDepuisOrigine(annee, numeroSemaine) {
194
- const dateActuelle = new Date();
195
- const dateOrigine = new Date(dateActuelle.getFullYear() - 2, 0, 1);
196
- const anneeOrigine = dateOrigine.getFullYear();
197
- const numeroSemaineOrigine = getWeekNumber(dateOrigine);
198
- let nombreSemaines = 0;
199
- for (let i = anneeOrigine; i < annee; i++) {
200
- nombreSemaines += moment().year(i).isoWeeksInYear();
201
- }
202
- nombreSemaines += numeroSemaine - numeroSemaineOrigine;
203
- return nombreSemaines;
204
- }
205
- export function getSessionStorageRecordForDragAndDrop(tasks, positionDay, dropGroupId) {
206
- const dragtaskId = window.sessionStorage.getItem("calendardragtaskId");
207
- const dragtaskStart = window.sessionStorage.getItem("calendardragtaskStart");
208
- const dragtaskEnd = window.sessionStorage.getItem("calendardragtaskEnd");
209
- const dragdayIndex = window.sessionStorage.getItem("calendardragdayIndex");
210
- let newTask;
211
- let newTasks = [];
212
- window.sessionStorage.clear();
213
- if (!dragdayIndex || !dragtaskStart || !dragtaskEnd || !dragtaskId || !tasks)
214
- return;
215
- const dragTask = tasks.find((task) => task.taskId === dragtaskId);
216
- const dayIndex = parseInt(dragdayIndex);
217
- let ecartDaysIndex = positionDay - dayIndex;
218
- const convertTaskDropStart = new Date(parseInt(dragtaskStart));
219
- convertTaskDropStart.setDate(convertTaskDropStart.getDate() + ecartDaysIndex);
220
- const taskDropStart = convertTaskDropStart.getTime();
221
- let convertTaskDropEnd = new Date(parseInt(dragtaskEnd));
222
- convertTaskDropEnd.setDate(convertTaskDropEnd.getDate() + ecartDaysIndex);
223
- const taskDropEnd = convertTaskDropEnd.getTime();
224
- const taskDropDate = new Date(taskDropStart);
225
- if (dragTask) {
226
- const { taskStart, taskEnd, taskDate, groupId, dayIndex } = dragTask, rest = __rest(dragTask, ["taskStart", "taskEnd", "taskDate", "groupId", "dayIndex"]);
227
- newTask = Object.assign({ taskStart: taskDropStart, taskEnd: taskDropEnd, taskDate: taskDropDate, groupId: dropGroupId, dayIndex: positionDay }, rest);
228
- const dragTaskIndex = tasks.findIndex((task) => task.taskId === dragtaskId);
229
- newTasks = [...tasks];
230
- newTasks.splice(dragTaskIndex, 1, newTask);
231
- }
232
- return { taskDropStart, taskDropEnd, taskDropDate, newTask, newTasks };
233
- }
234
- export function compareWeekOffset(calendarDate, weekOffset, taskDate) {
235
- // if (taskDate.getDay() === 0 && calculerEcartSemaine(taskDate) === -7) {
236
- // return true;
237
- // }
238
- if (calendarDate)
239
- return (calculerEcartSemaine(calendarDate) === calculerEcartSemaine(taskDate));
240
- return weekOffset === calculerEcartSemaine(taskDate);
241
- }
242
- export const sumHoursByGroups = (groupId, tasks, weekOffset, calendarDate) => {
243
- let sum = 0;
244
- if (tasks)
245
- tasks.forEach((task) => {
246
- if (task.groupId === groupId &&
247
- compareWeekOffset(calendarDate, weekOffset, task.taskDate) === true) {
248
- sum += task.taskEnd - task.taskStart;
249
- }
250
- });
251
- return sum;
252
- };
253
- export function saveTasksToLocalStorage(tasks) {
254
- if (typeof window !== "undefined") {
255
- window.localStorage.setItem("Calendar", "je marche");
256
- const tasksSavedString = window.localStorage.getItem("CalendarTaskSaved");
257
- const tasksString = JSON.stringify(tasks);
258
- if (tasksSavedString === tasksString)
259
- return;
260
- if (tasksString === "[]")
261
- return;
262
- const backup = [
263
- ...tasks.filter((task) => {
264
- {
265
- if (task === null || task === void 0 ? void 0 : task.taskExpiryDate) {
266
- const taskDate = new Date(task === null || task === void 0 ? void 0 : task.taskExpiryDate);
267
- return taskDate.getTime() >= Date.now();
268
- }
269
- }
270
- }),
271
- ];
272
- window.localStorage.setItem("CalendarTaskSaved", JSON.stringify(backup));
273
- }
274
- }
275
- export const updateCalendarDateWithOffset = (offset, calendarDate) => {
276
- const newDate = new Date(calendarDate);
277
- newDate.setDate(newDate.getDate() + offset);
278
- return newDate;
279
- };
280
- export const updateOffsetWithDateCalendar = (calendarDate) => {
281
- return calculerEcartSemaine(calendarDate);
282
- };
283
- export const millisecondsToHours = (milliseconds) => {
284
- return millisecondsToDate(milliseconds).formattedDate;
285
- };
286
- export const checkDuplicates = (tasks, taskStart, taskEnd, groupId) => {
287
- const findDuplicates = tasks === null || tasks === void 0 ? void 0 : tasks.filter((task) => (taskStart >= task.taskStart && taskStart < task.taskEnd) ||
288
- (taskEnd > task.taskStart && taskEnd < task.taskEnd) ||
289
- (taskStart <= task.taskStart &&
290
- taskEnd > task.taskStart &&
291
- taskEnd >= task.taskEnd &&
292
- taskStart <= task.taskEnd)).filter((task) => task.groupId === groupId);
293
- return findDuplicates.length > 0;
294
- };
295
- export const getSavedTasks = () => {
296
- const taskSavedString = window.localStorage.getItem("CalendarTaskSaved");
297
- if (!taskSavedString) {
298
- return [];
299
- }
300
- const tasksTable = JSON.parse(taskSavedString);
301
- const savedTasks = tasksTable.map((task) => {
302
- const { taskDate, taskExpiryDate } = task, rest = __rest(task, ["taskDate", "taskExpiryDate"]);
303
- if (taskExpiryDate) {
304
- return Object.assign({ taskDate: new Date(taskDate), taskExpiryDate: new Date(taskExpiryDate) }, rest);
305
- }
306
- });
307
- return savedTasks;
308
- };
309
- export const deleteTaskSaved = (taskId) => {
310
- const tasksSavedString = window.localStorage.getItem("CalendarTaskSaved");
311
- if (!tasksSavedString)
312
- return;
313
- const tasksSavedTable = JSON.parse(tasksSavedString);
314
- const taskIndex = tasksSavedTable.findIndex((task) => task.taskId === taskId);
315
- if (taskIndex) {
316
- tasksSavedTable.splice(taskIndex, 1);
317
- window.localStorage.setItem("CalendarTaskSaved", JSON.stringify(tasksSavedTable));
318
- }
319
- };
320
- export const deleteTasksSaved = () => {
321
- window.localStorage.removeItem("CalendarTaskSaved");
322
- };
323
- export function getWeeksInMonth(year, month) {
324
- // Create a new date object for the first day of the given month
325
- const firstDay = new Date(year, month, 1);
326
- // Create a new date object for the last day of the given month
327
- const lastDay = new Date(year, month + 1, 0);
328
- // Get the day of the week of the first day of the month
329
- const startDay = firstDay.getDay();
330
- // Get the number of days in the month
331
- const daysInMonth = lastDay.getDate();
332
- // Calculate the number of weeks
333
- const weeks = Math.ceil((daysInMonth + startDay) / 7);
334
- return weeks;
335
- }
336
- export function getMonthNumber(monthName) {
337
- // List of months in order
338
- const months = [
339
- "January",
340
- "February",
341
- "March",
342
- "April",
343
- "May",
344
- "June",
345
- "July",
346
- "August",
347
- "September",
348
- "October",
349
- "November",
350
- "December",
351
- ];
352
- // Convert the month name to the corresponding month index
353
- const monthIndex = months.indexOf(monthName);
354
- // If the month name is valid, return its index (0-based)
355
- if (monthIndex !== -1) {
356
- return monthIndex;
357
- }
358
- else {
359
- // Return -1 if the month name is invalid
360
- console.error("Invalid month name");
361
- return -1;
362
- }
363
- }
364
- export function getDateObjectInTimeZone(timeZone) {
365
- try {
366
- let date = new Date(new Date().toLocaleString("en-US", { timeZone: timeZone }));
367
- return date;
368
- }
369
- catch (error) {
370
- return new Date();
371
- }
372
- }
373
- function recurring(ecartDay, task) {
374
- const newTask = Object.assign({}, task);
375
- newTask.taskStart = newTask.taskStart + ecartDay;
376
- newTask.taskEnd = newTask.taskEnd + ecartDay;
377
- if (newTask.taskExpiryDate)
378
- newTask.taskExpiryDate = new Date(newTask.taskExpiryDate.getTime() + ecartDay);
379
- newTask.taskDate = new Date(newTask.taskStart);
380
- newTask.dayIndex = newTask.taskDate.getDay();
381
- newTask.taskId = getUnqueId();
382
- return newTask;
383
- }
384
- export function recurringTasks(allTasks, task, recurrenceType, occurrences) {
385
- const tasks = [];
386
- function daily() {
387
- for (let i = 0; i < occurrences; i++) {
388
- // Create a copy of the task with updated taskDate for each day
389
- const newTask = recurring(i * DAY_IN_MILLISECONDS, task);
390
- if (!checkDuplicates(allTasks, newTask.taskStart, newTask.taskEnd, task.groupId)) {
391
- tasks.push(newTask);
392
- }
393
- }
394
- }
395
- function weekly() {
396
- for (let i = 0; i < occurrences; i++) {
397
- // Create a copy of the task with updated taskDate for each week
398
- const newTask = recurring(i * WEEK_IN_MILLISECONDS, task);
399
- if (!checkDuplicates(allTasks, newTask.taskStart, newTask.taskEnd, task.groupId)) {
400
- tasks.push(newTask);
401
- }
402
- }
403
- }
404
- function monthly() {
405
- for (let i = 0; i < occurrences; i++) {
406
- // Create a copy of the task with updated taskDate for each week
407
- const newTask = recurring(dayjs(task.taskDate).daysInMonth() * i * DAY_IN_MILLISECONDS, task);
408
- if (!checkDuplicates(allTasks, newTask.taskStart, newTask.taskEnd, task.groupId)) {
409
- tasks.push(newTask);
410
- }
411
- }
412
- }
413
- // Execute the correct recurrence function based on the type
414
- if (recurrenceType === "daily") {
415
- daily();
416
- }
417
- else if (recurrenceType === "weekly") {
418
- weekly();
419
- }
420
- else if (recurrenceType === "monthly") {
421
- monthly();
422
- }
423
- console.log(tasks);
424
- // Return the list of generated tasks
425
- return tasks;
426
- }
427
- export function getHoursByday(tasks, dayIndex, weekOffset) {
428
- const sum = tasks.reduce((currentSum, task) => {
429
- if (task.dayIndex === dayIndex &&
430
- weekOffset === updateOffsetWithDateCalendar(task.taskDate))
431
- return (currentSum +
432
- Math.ceil((task.taskEnd - task.taskStart) / DAY_IN_MILLISECONDS));
433
- return currentSum;
434
- }, 0);
435
- return sum;
436
- }
437
- export function getHoursByGroup(tasks, groupId, weekOffset) {
438
- const sum = tasks.reduce((currentSum, task) => {
439
- if (task.groupId === groupId &&
440
- weekOffset === updateOffsetWithDateCalendar(task.taskDate))
441
- return (currentSum +
442
- Math.ceil((task.taskEnd - task.taskStart) / DAY_IN_MILLISECONDS));
443
- return currentSum;
444
- }, 0);
445
- return sum;
446
- }
447
- export function getTaskProgression(task, timeZone) {
448
- const now = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
449
- if (task.taskStart >= now.getTime())
450
- return 0;
451
- if (now.getTime() >= task.taskEnd)
452
- return 100;
453
- const progression = ((now.getTime() - task.taskStart) / (task.taskEnd - task.taskStart)) * 100;
454
- return progression.toFixed(0);
455
- }
456
- export function convertTasksToIcsFormat(tasks) {
457
- const ics = tasks.reduce((previousIcs, task) => {
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import moment from "moment";
13
+ import dayjs from "dayjs";
14
+ import { v4 as uuidv4 } from "uuid";
15
+ export const DAY_IN_MILLISECONDS = 86400000;
16
+ export const WEEK_IN_MILLISECONDS = DAY_IN_MILLISECONDS * 7;
17
+ // Get the current date
18
+ const currentDate = new Date();
19
+ // Get the day of the week (Sunday = 0, Monday = 1, ..., Saturday = 6)
20
+ const currentDayOfWeek = currentDate.getDay();
21
+ // Calculate the start date of the week in milliseconds
22
+ const startDate = new Date(currentDate);
23
+ startDate.setDate(startDate.getDate() - currentDayOfWeek);
24
+ startDate.setHours(0, 0, 0, 0);
25
+ export const startDateMilliseconds = startDate.getTime();
26
+ // Calculate the end date of the week in milliseconds
27
+ const endDate = new Date(currentDate);
28
+ endDate.setDate(endDate.getDate() + (6 - currentDayOfWeek));
29
+ endDate.setHours(23, 59, 59, 999);
30
+ export const endDateMilliseconds = endDate.getTime();
31
+ export function getDayHourly(weekOffset, timeZone) {
32
+ const dailyHours = [];
33
+ let dayOffset = weekOffset;
34
+ const resolvedCurrentDate = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
35
+ const currentDayOfWeek = resolvedCurrentDate.getDay();
36
+ const resolvedStartDate = new Date(resolvedCurrentDate);
37
+ resolvedStartDate.setDate(resolvedStartDate.getDate() - currentDayOfWeek);
38
+ resolvedStartDate.setHours(0, 0, 0, 0);
39
+ // Loop to calculate the start and end hours for each day of the week
40
+ for (let i = 0; i < 7; i++) {
41
+ const dayDate = new Date(resolvedStartDate);
42
+ dayDate.setDate(resolvedStartDate.getDate() + i);
43
+ const dayStart = new Date(dayDate);
44
+ dayStart.setHours(1, 0, 0, 0);
45
+ const dayEnd = new Date(dayDate);
46
+ dayEnd.setHours(23, 59, 59, 59);
47
+ dailyHours.push({
48
+ positionDay: i,
49
+ day: new Date(dayStart.getTime() + (dayOffset * DAY_IN_MILLISECONDS)),
50
+ start: dayStart.getTime() + (dayOffset * DAY_IN_MILLISECONDS),
51
+ end: dayEnd.getTime() + (dayOffset * DAY_IN_MILLISECONDS),
52
+ });
53
+ }
54
+ return dailyHours;
55
+ }
56
+ // Convert milliseconds to a readable date format
57
+ export function millisecondsToDate(milliseconds) {
58
+ const date = new Date(milliseconds);
59
+ const daysOfWeek = ["Mon", "Tues", "Wed", "Thur", "Frid", "Sat", "Sun"];
60
+ const dayOfWeek = daysOfWeek[date.getDay()];
61
+ let hours = date.getHours();
62
+ const minutes = date.getMinutes();
63
+ const formattedDate = `${hours.toString().padStart(2, "0")}h:${minutes
64
+ .toString()
65
+ .padStart(2, "0")}`;
66
+ return { formattedDate, dayOfWeek };
67
+ }
68
+ // Convert milliseconds to integer representation
69
+ export function millisecondsToInt(milliseconds) {
70
+ const date = new Date(milliseconds);
71
+ const daysOfWeek = [
72
+ "Sunday",
73
+ "Monday",
74
+ "Tuesday",
75
+ "Wednesday",
76
+ "Thursday",
77
+ "Friday",
78
+ "Saturday",
79
+ ];
80
+ const months = [
81
+ "January",
82
+ "February",
83
+ "March",
84
+ "April",
85
+ "May",
86
+ "June",
87
+ "July",
88
+ "August",
89
+ "September",
90
+ "October",
91
+ "November",
92
+ "December",
93
+ ];
94
+ const dayOfWeek = daysOfWeek[date.getDay()];
95
+ const dayOfMonth = date.getDate();
96
+ const month = months[date.getMonth()];
97
+ let hours = date.getHours();
98
+ const amOrPm = hours >= 12 ? " pm" : " am";
99
+ const minutes = date.getMinutes();
100
+ const formattedDate = hours.toString().padStart(2, "0");
101
+ return { formattedDate, dayOfWeek };
102
+ }
103
+ // Get days of the week with a jump offset
104
+ export function getWeekDays(jump, timeZone) {
105
+ const days = ["Sun", "Mon", "Tues", "Wed", "Thur", "Frid", "Sat"];
106
+ const month = [
107
+ "Jan",
108
+ "Feb",
109
+ "Mar",
110
+ "Apr",
111
+ "May",
112
+ "Jun",
113
+ "Jul",
114
+ "Aug",
115
+ "Sept",
116
+ "Oct",
117
+ "Nov",
118
+ "Dec",
119
+ ];
120
+ const currentDate = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
121
+ const currentDayOfWeek = currentDate.getDay();
122
+ let weekDays = [];
123
+ for (let i = 0; i < 7; i++) {
124
+ const day = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
125
+ const diff = i - currentDayOfWeek;
126
+ if (currentDayOfWeek === 0) {
127
+ day.setDate(currentDate.getDate() + diff + jump - 7);
128
+ }
129
+ else {
130
+ day.setDate(currentDate.getDate() + diff + jump);
131
+ }
132
+ const formattedDay = `${days[day.getDay()]}. ${day.getDate()}, ${month[day.getMonth()]} ${day.getFullYear()}`;
133
+ weekDays.push({
134
+ day: days[day.getDay()],
135
+ dayMonth: month[day.getMonth()],
136
+ dayYear: day.getFullYear(),
137
+ dayOfTheMonth: day.getDate(),
138
+ });
139
+ }
140
+ return weekDays;
141
+ }
142
+ // The remaining functions follow the same structure. Ensure all comments are in English and make the functions exportable as required.
143
+ /**
144
+ * Get the ISO week number for a given date.
145
+ * @param date - The date to get the week number for.
146
+ * @returns The ISO week number.
147
+ */
148
+ function getWeekNumber(date) {
149
+ const tempDate = new Date(date.getTime());
150
+ tempDate.setHours(0, 0, 0, 0);
151
+ // Thursday in current week decides the year
152
+ tempDate.setDate(tempDate.getDate() + 4 - (tempDate.getDay() || 7));
153
+ const yearStart = new Date(tempDate.getFullYear(), 0, 1);
154
+ const weekNumber = Math.ceil(((tempDate.getTime() - yearStart.getTime()) / DAY_IN_MILLISECONDS + 1) / 7);
155
+ return weekNumber;
156
+ }
157
+ /**
158
+ * Update the selected date to avoid issues with weeks starting on Sunday.
159
+ * @param dateSelectionnee - The selected date.
160
+ * @returns The updated date.
161
+ */
162
+ function updateSelectedDateForEcartSemaine(dateSelectionnee) {
163
+ const updatedDate = new Date(dateSelectionnee.getTime());
164
+ if (updatedDate.getDay() === 0) {
165
+ updatedDate.setDate(updatedDate.getDate() + 1);
166
+ }
167
+ return updatedDate;
168
+ }
169
+ /**
170
+ * Calculate the week difference between the selected date and the current date.
171
+ * @param dateSelectionnee - The selected date.
172
+ * @returns The week difference in days.
173
+ */
174
+ export function calculerEcartSemaine(dateSelectionnee, timeZone) {
175
+ const dateActuelle = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
176
+ const recupDate = new Date(dateSelectionnee);
177
+ recupDate.setUTCDate(recupDate.getUTCDate() + 4 - (recupDate.getUTCDay() || 7));
178
+ dateActuelle.setUTCDate(dateActuelle.getUTCDate() + 4 - (dateActuelle.getUTCDay() || 7));
179
+ return Math.ceil((recupDate.getTime() - dateActuelle.getTime()) / 86400000);
180
+ }
181
+ /**
182
+ * Calculate the number of weeks since an arbitrary origin date (January 1, 2022).
183
+ * @param annee - The year.
184
+ * @param numeroSemaine - The week number.
185
+ * @returns The number of weeks since the origin date.
186
+ */
187
+ function semainesDepuisOrigine(annee, numeroSemaine) {
188
+ const dateActuelle = new Date();
189
+ const dateOrigine = new Date(dateActuelle.getFullYear() - 2, 0, 1);
190
+ const anneeOrigine = dateOrigine.getFullYear();
191
+ const numeroSemaineOrigine = getWeekNumber(dateOrigine);
192
+ let nombreSemaines = 0;
193
+ for (let i = anneeOrigine; i < annee; i++) {
194
+ nombreSemaines += moment().year(i).isoWeeksInYear();
195
+ }
196
+ nombreSemaines += numeroSemaine - numeroSemaineOrigine;
197
+ return nombreSemaines;
198
+ }
199
+ export function getSessionStorageRecordForDragAndDrop(tasks, positionDay, dropGroupId) {
200
+ const dragtaskId = window.sessionStorage.getItem("calendardragtaskId");
201
+ const dragtaskStart = window.sessionStorage.getItem("calendardragtaskStart");
202
+ const dragtaskEnd = window.sessionStorage.getItem("calendardragtaskEnd");
203
+ const dragdayIndex = window.sessionStorage.getItem("calendardragdayIndex");
204
+ let newTask;
205
+ let newTasks = [];
206
+ window.sessionStorage.clear();
207
+ if (!dragdayIndex || !dragtaskStart || !dragtaskEnd || !dragtaskId || !tasks)
208
+ return;
209
+ const dragTask = tasks.find((task) => task.taskId === dragtaskId);
210
+ const dayIndex = parseInt(dragdayIndex);
211
+ let ecartDaysIndex = positionDay - dayIndex;
212
+ const convertTaskDropStart = new Date(parseInt(dragtaskStart));
213
+ convertTaskDropStart.setDate(convertTaskDropStart.getDate() + ecartDaysIndex);
214
+ const taskDropStart = convertTaskDropStart.getTime();
215
+ let convertTaskDropEnd = new Date(parseInt(dragtaskEnd));
216
+ convertTaskDropEnd.setDate(convertTaskDropEnd.getDate() + ecartDaysIndex);
217
+ const taskDropEnd = convertTaskDropEnd.getTime();
218
+ const taskDropDate = new Date(taskDropStart);
219
+ if (dragTask) {
220
+ const { taskStart, taskEnd, taskDate, groupId, dayIndex } = dragTask, rest = __rest(dragTask, ["taskStart", "taskEnd", "taskDate", "groupId", "dayIndex"]);
221
+ newTask = Object.assign({ taskStart: taskDropStart, taskEnd: taskDropEnd, taskDate: taskDropDate, groupId: dropGroupId, dayIndex: positionDay }, rest);
222
+ const dragTaskIndex = tasks.findIndex((task) => task.taskId === dragtaskId);
223
+ newTasks = [...tasks];
224
+ newTasks.splice(dragTaskIndex, 1, newTask);
225
+ }
226
+ return { taskDropStart, taskDropEnd, taskDropDate, newTask, newTasks };
227
+ }
228
+ export function compareWeekOffset(calendarDate, weekOffset, taskDate, timeZone) {
229
+ // if (taskDate.getDay() === 0 && calculerEcartSemaine(taskDate) === -7) {
230
+ // return true;
231
+ // }
232
+ const localTaskDate = getArbitraryDateInTimeZone(taskDate, timeZone);
233
+ // if (calendarDate)
234
+ // return (calculerEcartSemaine(calendarDate) === calculerEcartSemaine(taskDate));
235
+ const ecartTask = calculerEcartSemaine(taskDate, timeZone) + (localTaskDate.getDay() === 0 ? 7 : 0);
236
+ return weekOffset === ecartTask;
237
+ }
238
+ export const sumHoursByGroups = (groupId, tasks, weekOffset, calendarDate, timeZone) => {
239
+ let sum = 0;
240
+ if (tasks)
241
+ tasks.forEach((task) => {
242
+ if (task.groupId === groupId &&
243
+ compareWeekOffset(calendarDate, weekOffset, task.taskDate, timeZone) === true) {
244
+ sum += task.taskEnd - task.taskStart;
245
+ }
246
+ });
247
+ return sum;
248
+ };
249
+ export function saveTasksToLocalStorage(tasks) {
250
+ if (typeof window !== "undefined") {
251
+ window.localStorage.setItem("Calendar", "je marche");
252
+ const tasksSavedString = window.localStorage.getItem("CalendarTaskSaved");
253
+ const tasksString = JSON.stringify(tasks);
254
+ if (tasksSavedString === tasksString)
255
+ return;
256
+ if (tasksString === "[]")
257
+ return;
258
+ const backup = [
259
+ ...tasks.filter((task) => {
260
+ {
261
+ if (task === null || task === void 0 ? void 0 : task.taskExpiryDate) {
262
+ const taskDate = new Date(task === null || task === void 0 ? void 0 : task.taskExpiryDate);
263
+ return taskDate.getTime() >= Date.now();
264
+ }
265
+ }
266
+ }),
267
+ ];
268
+ window.localStorage.setItem("CalendarTaskSaved", JSON.stringify(backup));
269
+ }
270
+ }
271
+ export const updateCalendarDateWithOffset = (offset, calendarDate) => {
272
+ const newDate = new Date(calendarDate);
273
+ newDate.setDate(newDate.getDate() + offset);
274
+ return newDate;
275
+ };
276
+ export const updateOffsetWithDateCalendar = (calendarDate, timeZone) => {
277
+ return calculerEcartSemaine(calendarDate, timeZone);
278
+ };
279
+ export const millisecondsToHours = (milliseconds) => {
280
+ return millisecondsToDate(milliseconds).formattedDate;
281
+ };
282
+ export const checkDuplicates = (tasks, taskStart, taskEnd, groupId) => {
283
+ const findDuplicates = tasks === null || tasks === void 0 ? void 0 : tasks.filter((task) => (taskStart >= task.taskStart && taskStart < task.taskEnd) ||
284
+ (taskEnd > task.taskStart && taskEnd < task.taskEnd) ||
285
+ (taskStart <= task.taskStart &&
286
+ taskEnd > task.taskStart &&
287
+ taskEnd >= task.taskEnd &&
288
+ taskStart <= task.taskEnd)).filter((task) => task.groupId === groupId);
289
+ return findDuplicates.length > 0;
290
+ };
291
+ export const getSavedTasks = () => {
292
+ const taskSavedString = window.localStorage.getItem("CalendarTaskSaved");
293
+ if (!taskSavedString) {
294
+ return [];
295
+ }
296
+ const tasksTable = JSON.parse(taskSavedString);
297
+ const savedTasks = tasksTable.map((task) => {
298
+ const { taskDate, taskExpiryDate } = task, rest = __rest(task, ["taskDate", "taskExpiryDate"]);
299
+ if (taskExpiryDate) {
300
+ return Object.assign({ taskDate: new Date(taskDate), taskExpiryDate: new Date(taskExpiryDate) }, rest);
301
+ }
302
+ });
303
+ return savedTasks;
304
+ };
305
+ export const deleteTaskSaved = (taskId) => {
306
+ const tasksSavedString = window.localStorage.getItem("CalendarTaskSaved");
307
+ if (!tasksSavedString)
308
+ return;
309
+ const tasksSavedTable = JSON.parse(tasksSavedString);
310
+ const taskIndex = tasksSavedTable.findIndex((task) => task.taskId === taskId);
311
+ if (taskIndex) {
312
+ tasksSavedTable.splice(taskIndex, 1);
313
+ window.localStorage.setItem("CalendarTaskSaved", JSON.stringify(tasksSavedTable));
314
+ }
315
+ };
316
+ export const deleteTasksSaved = () => {
317
+ window.localStorage.removeItem("CalendarTaskSaved");
318
+ };
319
+ export function getWeeksInMonth(year, month) {
320
+ // Create a new date object for the first day of the given month
321
+ const firstDay = new Date(year, month, 1);
322
+ // Create a new date object for the last day of the given month
323
+ const lastDay = new Date(year, month + 1, 0);
324
+ // Get the day of the week of the first day of the month
325
+ const startDay = firstDay.getDay();
326
+ // Get the number of days in the month
327
+ const daysInMonth = lastDay.getDate();
328
+ // Calculate the number of weeks
329
+ const weeks = Math.ceil((daysInMonth + startDay) / 7);
330
+ return weeks;
331
+ }
332
+ export function getMonthNumber(monthName) {
333
+ // List of months in order
334
+ const months = [
335
+ "January",
336
+ "February",
337
+ "March",
338
+ "April",
339
+ "May",
340
+ "June",
341
+ "July",
342
+ "August",
343
+ "September",
344
+ "October",
345
+ "November",
346
+ "December",
347
+ ];
348
+ // Convert the month name to the corresponding month index
349
+ const monthIndex = months.indexOf(monthName);
350
+ // If the month name is valid, return its index (0-based)
351
+ if (monthIndex !== -1) {
352
+ return monthIndex;
353
+ }
354
+ else {
355
+ // Return -1 if the month name is invalid
356
+ console.error("Invalid month name");
357
+ return -1;
358
+ }
359
+ }
360
+ export function getDateObjectInTimeZone(timeZone) {
361
+ try {
362
+ let date = new Date(new Date().toLocaleString("en-US", { timeZone: timeZone }));
363
+ return date;
364
+ }
365
+ catch (error) {
366
+ return new Date();
367
+ }
368
+ }
369
+ export function getArbitraryDateInTimeZone(date, timeZone) {
370
+ if (!timeZone)
371
+ return date;
372
+ try {
373
+ return new Date(date.toLocaleString("en-US", { timeZone: timeZone }));
374
+ }
375
+ catch (error) {
376
+ return date;
377
+ }
378
+ }
379
+ function recurring(ecartDay, task, timeZone) {
380
+ const newTask = Object.assign({}, task);
381
+ newTask.taskStart = newTask.taskStart + ecartDay;
382
+ newTask.taskEnd = newTask.taskEnd + ecartDay;
383
+ if (newTask.taskExpiryDate)
384
+ newTask.taskExpiryDate = new Date(newTask.taskExpiryDate.getTime() + ecartDay);
385
+ newTask.taskDate = new Date(newTask.taskStart);
386
+ newTask.dayIndex = getArbitraryDateInTimeZone(newTask.taskDate, timeZone).getDay();
387
+ newTask.taskId = getUnqueId();
388
+ return newTask;
389
+ }
390
+ export function recurringTasks(allTasks, task, recurrenceType, occurrences, timeZone) {
391
+ const tasks = [];
392
+ function daily() {
393
+ for (let i = 0; i < occurrences; i++) {
394
+ // Create a copy of the task with updated taskDate for each day
395
+ const newTask = recurring(i * DAY_IN_MILLISECONDS, task, timeZone);
396
+ if (!checkDuplicates(allTasks, newTask.taskStart, newTask.taskEnd, task.groupId)) {
397
+ tasks.push(newTask);
398
+ }
399
+ }
400
+ }
401
+ function weekly() {
402
+ for (let i = 0; i < occurrences; i++) {
403
+ // Create a copy of the task with updated taskDate for each week
404
+ const newTask = recurring(i * WEEK_IN_MILLISECONDS, task, timeZone);
405
+ if (!checkDuplicates(allTasks, newTask.taskStart, newTask.taskEnd, task.groupId)) {
406
+ tasks.push(newTask);
407
+ }
408
+ }
409
+ }
410
+ function monthly() {
411
+ for (let i = 0; i < occurrences; i++) {
412
+ // Create a copy of the task with updated taskDate for each week
413
+ const newTask = recurring(dayjs(task.taskDate).daysInMonth() * i * DAY_IN_MILLISECONDS, task, timeZone);
414
+ if (!checkDuplicates(allTasks, newTask.taskStart, newTask.taskEnd, task.groupId)) {
415
+ tasks.push(newTask);
416
+ }
417
+ }
418
+ }
419
+ // Execute the correct recurrence function based on the type
420
+ if (recurrenceType === "daily") {
421
+ daily();
422
+ }
423
+ else if (recurrenceType === "weekly") {
424
+ weekly();
425
+ }
426
+ else if (recurrenceType === "monthly") {
427
+ monthly();
428
+ }
429
+ // Return the list of generated tasks
430
+ return tasks;
431
+ }
432
+ export function getHoursByday(tasks, dayIndex, weekOffset, timeZone) {
433
+ const sum = tasks.reduce((currentSum, task) => {
434
+ if (task.dayIndex === dayIndex &&
435
+ weekOffset === updateOffsetWithDateCalendar(task.taskDate, timeZone))
436
+ return (currentSum +
437
+ Math.ceil((task.taskEnd - task.taskStart) / DAY_IN_MILLISECONDS));
438
+ return currentSum;
439
+ }, 0);
440
+ return sum;
441
+ }
442
+ export function getHoursByGroup(tasks, groupId, weekOffset, timeZone) {
443
+ const sum = tasks.reduce((currentSum, task) => {
444
+ if (task.groupId === groupId &&
445
+ weekOffset === updateOffsetWithDateCalendar(task.taskDate, timeZone))
446
+ return (currentSum +
447
+ Math.ceil((task.taskEnd - task.taskStart) / DAY_IN_MILLISECONDS));
448
+ return currentSum;
449
+ }, 0);
450
+ return sum;
451
+ }
452
+ export function getTaskProgression(task, timeZone) {
453
+ const now = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
454
+ if (task.taskStart >= now.getTime())
455
+ return 0;
456
+ if (now.getTime() >= task.taskEnd)
457
+ return 100;
458
+ const progression = ((now.getTime() - task.taskStart) / (task.taskEnd - task.taskStart)) * 100;
459
+ return progression.toFixed(0);
460
+ }
461
+ export function convertTasksToIcsFormat(tasks) {
462
+ const ics = tasks.reduce((previousIcs, task) => {
458
463
  previousIcs += `
459
464
 
460
465
  BEGIN:VCALENDAR
@@ -469,172 +474,172 @@ export function convertTasksToIcsFormat(tasks) {
469
474
  END:VEVENT
470
475
  END:VCALENDAR
471
476
 
472
- `;
473
- return previousIcs;
474
- }, "");
475
- return ics;
476
- }
477
- export function addTask(tasks, task) {
478
- return [...tasks, task];
479
- }
480
- export function deleteTask(tasks, taskId) {
481
- const taskPos = tasks.findIndex((task) => task.taskId === taskId);
482
- const alltasks = [...tasks];
483
- alltasks.splice(taskPos, 1);
484
- return alltasks;
485
- }
486
- export function getTaskInfoById(tasks, groups, taskId) {
487
- const task = tasks.find((task) => task.taskId === taskId);
488
- if (!task)
489
- throw new Error("no such to task");
490
- const group = groups.find((group) => group.id === task.groupId);
491
- return {
492
- group,
493
- task,
494
- };
495
- }
496
- export function selectTask(task) {
497
- if (typeof window !== "undefined") {
498
- const copiedTasks = JSON.parse(window.sessionStorage.getItem("copiedTasks") || "[]");
499
- if (copiedTasks.length > 0) {
500
- window.sessionStorage.setItem("copiedTasks", JSON.stringify([...copiedTasks, task]));
501
- }
502
- else
503
- window.sessionStorage.setItem("copiedTasks", JSON.stringify([task]));
504
- }
505
- }
506
- export function deSelectTask(task) {
507
- if (typeof window !== "undefined") {
508
- const copiedTasks = JSON.parse(window.sessionStorage.getItem("copiedTasks") || "[]");
509
- if (copiedTasks.length > 0) {
510
- const newTasks = deleteTask(copiedTasks, task.taskId);
511
- window.sessionStorage.setItem("copiedTasks", JSON.stringify([...newTasks]));
512
- }
513
- else
514
- throw new Error("Sorry there are no tasks to select");
515
- }
516
- }
517
- export function copyTasks(task) {
518
- selectTask(task);
519
- }
520
- export function cutTasks(task, tasks) {
521
- copyTasks(task);
522
- const newTasks = deleteTask(tasks, task.taskId);
523
- return newTasks;
524
- }
525
- function updateTaskStartTimeAnEndTime(start, end, calendarOffset, dayIndex, taskPosition) {
526
- const diffDay = dayIndex +
527
- calendarOffset -
528
- (taskPosition + updateOffsetWithDateCalendar(new Date(start)));
529
- const startTime = start + diffDay * DAY_IN_MILLISECONDS;
530
- const endTime = end + diffDay * DAY_IN_MILLISECONDS;
531
- return { startTime, endTime };
532
- }
533
- export function getUnqueId() {
534
- const uid = uuidv4();
535
- return uid;
536
- }
537
- export function pastTasks(dayInfo, groupId, tasks, taskExpiryDate) {
538
- if (typeof window !== "undefined") {
539
- const copiedTasks = JSON.parse(window.sessionStorage.getItem("copiedTasks") || "[]");
540
- if (copiedTasks.length > 0) {
541
- const newTasks = copiedTasks.reduce((previousTasks, task) => {
542
- const { dayIndex: copiedTaskDayIndex, taskStart: copiedTasktaskStart, taskEnd: copiedTasktaskEnd, taskDate: copiedTasktaskDate, groupId: copiedTaskGroupId, taskId: copiedTaskId, taskExpiryDate: copiedTaskExpiryDate } = task, rest = __rest(task, ["dayIndex", "taskStart", "taskEnd", "taskDate", "groupId", "taskId", "taskExpiryDate"]);
543
- const newTaskStartAndEnd = updateTaskStartTimeAnEndTime(copiedTasktaskStart, copiedTasktaskEnd, updateOffsetWithDateCalendar(dayInfo.day), dayInfo.positionDay, copiedTaskDayIndex);
544
- if (!checkDuplicates(previousTasks, newTaskStartAndEnd.startTime, newTaskStartAndEnd.endTime, groupId)) {
545
- const newTaskDate = new Date(newTaskStartAndEnd.startTime);
546
- const newTaskId = `${getUnqueId()}`;
547
- const newTask = Object.assign({ taskStart: newTaskStartAndEnd.startTime, taskEnd: newTaskStartAndEnd.endTime, dayIndex: dayInfo.positionDay, taskId: newTaskId, taskDate: newTaskDate, groupId: groupId, taskExpiryDate: taskExpiryDate }, rest);
548
- return [...previousTasks, newTask];
549
- }
550
- else
551
- return previousTasks;
552
- }, []);
553
- window.sessionStorage.removeItem("copiedTasks");
554
- return [...tasks, ...newTasks];
555
- }
556
- else
557
- throw new Error("Sorry there are no tasks to select");
558
- }
559
- }
560
- export function updateTask(tasks, taskId, newtask) {
561
- return tasks.map((task) => {
562
- if (task.taskId === taskId) {
563
- return newtask;
564
- }
565
- return task;
566
- });
567
- }
568
- export function duplicateTasksForPeriod(periodStart, periodEnd, calendarOffset, allTasks) {
569
- if (periodStart > periodEnd)
570
- throw new Error("period Start is after period End");
571
- let tasks = [...allTasks];
572
- const currentWeekallTasks = allTasks.filter((planning) => planning.offset === calendarOffset);
573
- for (let dayInMilliseconds = periodStart.getTime(); dayInMilliseconds <= periodEnd.getTime(); dayInMilliseconds += DAY_IN_MILLISECONDS) {
574
- const dayIndex = new Date(dayInMilliseconds).getDay();
575
- const findPlanning = currentWeekallTasks.filter((planning) => planning.dayIndex === dayIndex);
576
- if (findPlanning.length > 0) {
577
- findPlanning.forEach((plan) => {
578
- const { taskStart, taskEnd, taskDate, offset, taskId } = plan, rest = __rest(plan, ["taskStart", "taskEnd", "taskDate", "offset", "taskId"]);
579
- const newOffset = updateOffsetWithDateCalendar(periodEnd);
580
- const ecartDay = Math.round((periodEnd.getTime() - periodStart.getTime()) / DAY_IN_MILLISECONDS);
581
- const newTaskStart = taskStart + ecartDay * DAY_IN_MILLISECONDS;
582
- const newTaskEnd = taskEnd + ecartDay * DAY_IN_MILLISECONDS;
583
- const newTaskDate = new Date(taskDate.getTime() + ecartDay * DAY_IN_MILLISECONDS);
584
- if (!checkDuplicates(allTasks, newTaskStart, newTaskEnd, rest.groupId)) {
585
- const newTask = Object.assign(Object.assign({}, rest), { taskDate: newTaskDate, taskStart: newTaskStart, taskEnd: newTaskEnd, taskId: getUnqueId() });
586
- tasks.push(Object.assign(Object.assign({}, newTask), { offset: newOffset }));
587
- }
588
- });
589
- }
590
- }
591
- return tasks;
592
- }
593
- export function duplicateTaskForPeriod(periodStart, periodEnd, task, ecartDay, groupId, taskExpiryDate) {
594
- if (periodStart >= periodEnd)
595
- throw new Error("period Start is after period End");
596
- let allTasks = [];
597
- for (let dayInMilliseconds = periodStart.getTime(); dayInMilliseconds <= periodEnd.getTime(); dayInMilliseconds += ecartDay
598
- ? DAY_IN_MILLISECONDS * ecartDay
599
- : DAY_IN_MILLISECONDS) {
600
- const { taskStart, taskEnd, taskDate, offset, taskId, groupId: currentTaskGroupId, taskExpiryDate: currentTaskExpiryDate } = task, rest = __rest(task, ["taskStart", "taskEnd", "taskDate", "offset", "taskId", "groupId", "taskExpiryDate"]);
601
- const newOffset = updateOffsetWithDateCalendar(periodEnd);
602
- const ecartDay = Math.round((dayInMilliseconds - taskDate.getTime()) / DAY_IN_MILLISECONDS);
603
- const newTaskStart = taskStart + ecartDay * DAY_IN_MILLISECONDS;
604
- const newTaskEnd = taskEnd + ecartDay * DAY_IN_MILLISECONDS;
605
- const newTaskDate = new Date(taskDate.getTime() + ecartDay * DAY_IN_MILLISECONDS);
606
- if (!checkDuplicates(allTasks, newTaskStart, newTaskEnd, rest.groupId)) {
607
- const newTask = Object.assign(Object.assign({}, rest), { taskDate: newTaskDate, taskStart: newTaskStart, taskEnd: newTaskEnd, taskId: getUnqueId(), groupId: groupId ? groupId : currentTaskGroupId, taskExpiryDate: taskExpiryDate });
608
- allTasks.push(Object.assign(Object.assign({}, newTask), { offset: newOffset }));
609
- }
610
- }
611
- return allTasks;
612
- }
613
- export const GetTimeRangeByDay = (start, end) => {
614
- const hourToMillisecond = 3600000;
615
- const range = [];
616
- for (let i = start; i < end; i += hourToMillisecond) {
617
- range.push(i);
618
- }
619
- return range;
620
- };
621
- export function totalLabel(milliseconds) {
622
- let label = "";
623
- const hourConv = milliseconds / 3600000;
624
- const truncHour = Math.trunc(hourConv);
625
- if (hourConv !== truncHour) {
626
- const deciHour = hourConv - truncHour;
627
- const minConv = deciHour * 60;
628
- const truncMin = Math.trunc(minConv);
629
- if (truncMin !== minConv) {
630
- const deciMin = minConv - truncMin;
631
- const secConv = deciMin * 60;
632
- label = `${truncHour}:${truncMin}:${Math.trunc(secConv)}`;
633
- }
634
- else
635
- label = `${truncHour}:${minConv}:0`;
636
- }
637
- else
638
- label = `${hourConv}:0:0`;
639
- return label;
640
- }
477
+ `;
478
+ return previousIcs;
479
+ }, "");
480
+ return ics;
481
+ }
482
+ export function addTask(tasks, task) {
483
+ return [...tasks, task];
484
+ }
485
+ export function deleteTask(tasks, taskId) {
486
+ const taskPos = tasks.findIndex((task) => task.taskId === taskId);
487
+ const alltasks = [...tasks];
488
+ alltasks.splice(taskPos, 1);
489
+ return alltasks;
490
+ }
491
+ export function getTaskInfoById(tasks, groups, taskId) {
492
+ const task = tasks.find((task) => task.taskId === taskId);
493
+ if (!task)
494
+ throw new Error("no such to task");
495
+ const group = groups.find((group) => group.id === task.groupId);
496
+ return {
497
+ group,
498
+ task,
499
+ };
500
+ }
501
+ export function selectTask(task) {
502
+ if (typeof window !== "undefined") {
503
+ const copiedTasks = JSON.parse(window.sessionStorage.getItem("copiedTasks") || "[]");
504
+ if (copiedTasks.length > 0) {
505
+ window.sessionStorage.setItem("copiedTasks", JSON.stringify([...copiedTasks, task]));
506
+ }
507
+ else
508
+ window.sessionStorage.setItem("copiedTasks", JSON.stringify([task]));
509
+ }
510
+ }
511
+ export function deSelectTask(task) {
512
+ if (typeof window !== "undefined") {
513
+ const copiedTasks = JSON.parse(window.sessionStorage.getItem("copiedTasks") || "[]");
514
+ if (copiedTasks.length > 0) {
515
+ const newTasks = deleteTask(copiedTasks, task.taskId);
516
+ window.sessionStorage.setItem("copiedTasks", JSON.stringify([...newTasks]));
517
+ }
518
+ else
519
+ throw new Error("Sorry there are no tasks to select");
520
+ }
521
+ }
522
+ export function copyTasks(task) {
523
+ selectTask(task);
524
+ }
525
+ export function cutTasks(task, tasks) {
526
+ copyTasks(task);
527
+ const newTasks = deleteTask(tasks, task.taskId);
528
+ return newTasks;
529
+ }
530
+ function updateTaskStartTimeAnEndTime(start, end, calendarOffset, dayIndex, taskPosition, timeZone) {
531
+ const diffDay = dayIndex +
532
+ calendarOffset -
533
+ (taskPosition + updateOffsetWithDateCalendar(new Date(start), timeZone));
534
+ const startTime = start + diffDay * DAY_IN_MILLISECONDS;
535
+ const endTime = end + diffDay * DAY_IN_MILLISECONDS;
536
+ return { startTime, endTime };
537
+ }
538
+ export function getUnqueId() {
539
+ const uid = uuidv4();
540
+ return uid;
541
+ }
542
+ export function pastTasks(dayInfo, groupId, tasks, taskExpiryDate, timeZone) {
543
+ if (typeof window !== "undefined") {
544
+ const copiedTasks = JSON.parse(window.sessionStorage.getItem("copiedTasks") || "[]");
545
+ if (copiedTasks.length > 0) {
546
+ const newTasks = copiedTasks.reduce((previousTasks, task) => {
547
+ const { dayIndex: copiedTaskDayIndex, taskStart: copiedTasktaskStart, taskEnd: copiedTasktaskEnd, taskDate: copiedTasktaskDate, groupId: copiedTaskGroupId, taskId: copiedTaskId, taskExpiryDate: copiedTaskExpiryDate } = task, rest = __rest(task, ["dayIndex", "taskStart", "taskEnd", "taskDate", "groupId", "taskId", "taskExpiryDate"]);
548
+ const newTaskStartAndEnd = updateTaskStartTimeAnEndTime(copiedTasktaskStart, copiedTasktaskEnd, updateOffsetWithDateCalendar(dayInfo.day, timeZone), dayInfo.positionDay, copiedTaskDayIndex, timeZone);
549
+ if (!checkDuplicates(previousTasks, newTaskStartAndEnd.startTime, newTaskStartAndEnd.endTime, groupId)) {
550
+ const newTaskDate = new Date(newTaskStartAndEnd.startTime);
551
+ const newTaskId = `${getUnqueId()}`;
552
+ const newTask = Object.assign({ taskStart: newTaskStartAndEnd.startTime, taskEnd: newTaskStartAndEnd.endTime, dayIndex: dayInfo.positionDay, taskId: newTaskId, taskDate: newTaskDate, groupId: groupId, taskExpiryDate: taskExpiryDate }, rest);
553
+ return [...previousTasks, newTask];
554
+ }
555
+ else
556
+ return previousTasks;
557
+ }, []);
558
+ window.sessionStorage.removeItem("copiedTasks");
559
+ return [...tasks, ...newTasks];
560
+ }
561
+ else
562
+ throw new Error("no past task(s)");
563
+ }
564
+ }
565
+ export function updateTask(tasks, taskId, newtask) {
566
+ return tasks.map((task) => {
567
+ if (task.taskId === taskId) {
568
+ return newtask;
569
+ }
570
+ return task;
571
+ });
572
+ }
573
+ export function duplicateTasksForPeriod(periodStart, periodEnd, calendarOffset, allTasks) {
574
+ if (periodStart > periodEnd)
575
+ throw new Error("period Start is after period End");
576
+ let tasks = [...allTasks];
577
+ const currentWeekallTasks = allTasks.filter((planning) => planning.offset === calendarOffset);
578
+ for (let dayInMilliseconds = periodStart.getTime(); dayInMilliseconds <= periodEnd.getTime(); dayInMilliseconds += DAY_IN_MILLISECONDS) {
579
+ const dayIndex = new Date(dayInMilliseconds).getDay();
580
+ const findPlanning = currentWeekallTasks.filter((planning) => planning.dayIndex === dayIndex);
581
+ if (findPlanning.length > 0) {
582
+ findPlanning.forEach((plan) => {
583
+ const { taskStart, taskEnd, taskDate, offset, taskId } = plan, rest = __rest(plan, ["taskStart", "taskEnd", "taskDate", "offset", "taskId"]);
584
+ const newOffset = updateOffsetWithDateCalendar(periodEnd);
585
+ const ecartDay = Math.round((periodEnd.getTime() - periodStart.getTime()) / DAY_IN_MILLISECONDS);
586
+ const newTaskStart = taskStart + ecartDay * DAY_IN_MILLISECONDS;
587
+ const newTaskEnd = taskEnd + ecartDay * DAY_IN_MILLISECONDS;
588
+ const newTaskDate = new Date(taskDate.getTime() + ecartDay * DAY_IN_MILLISECONDS);
589
+ if (!checkDuplicates(allTasks, newTaskStart, newTaskEnd, rest.groupId)) {
590
+ const newTask = Object.assign(Object.assign({}, rest), { taskDate: newTaskDate, taskStart: newTaskStart, taskEnd: newTaskEnd, taskId: getUnqueId() });
591
+ tasks.push(Object.assign(Object.assign({}, newTask), { offset: newOffset }));
592
+ }
593
+ });
594
+ }
595
+ }
596
+ return tasks;
597
+ }
598
+ export function duplicateTaskForPeriod(periodStart, periodEnd, task, ecartDay, groupId, taskExpiryDate) {
599
+ if (periodStart >= periodEnd)
600
+ throw new Error("period Start is after period End");
601
+ let allTasks = [];
602
+ for (let dayInMilliseconds = periodStart.getTime(); dayInMilliseconds <= periodEnd.getTime(); dayInMilliseconds += ecartDay
603
+ ? DAY_IN_MILLISECONDS * ecartDay
604
+ : DAY_IN_MILLISECONDS) {
605
+ const { taskStart, taskEnd, taskDate, offset, taskId, groupId: currentTaskGroupId, taskExpiryDate: currentTaskExpiryDate } = task, rest = __rest(task, ["taskStart", "taskEnd", "taskDate", "offset", "taskId", "groupId", "taskExpiryDate"]);
606
+ const newOffset = updateOffsetWithDateCalendar(periodEnd);
607
+ const ecartDay = Math.round((dayInMilliseconds - taskDate.getTime()) / DAY_IN_MILLISECONDS);
608
+ const newTaskStart = taskStart + ecartDay * DAY_IN_MILLISECONDS;
609
+ const newTaskEnd = taskEnd + ecartDay * DAY_IN_MILLISECONDS;
610
+ const newTaskDate = new Date(taskDate.getTime() + ecartDay * DAY_IN_MILLISECONDS);
611
+ if (!checkDuplicates(allTasks, newTaskStart, newTaskEnd, rest.groupId)) {
612
+ const newTask = Object.assign(Object.assign({}, rest), { taskDate: newTaskDate, taskStart: newTaskStart, taskEnd: newTaskEnd, taskId: getUnqueId(), groupId: groupId ? groupId : currentTaskGroupId, taskExpiryDate: taskExpiryDate });
613
+ allTasks.push(Object.assign(Object.assign({}, newTask), { offset: newOffset }));
614
+ }
615
+ }
616
+ return allTasks;
617
+ }
618
+ export const GetTimeRangeByDay = (start, end) => {
619
+ const hourToMillisecond = 3600000;
620
+ const range = [];
621
+ for (let i = start; i < end; i += hourToMillisecond) {
622
+ range.push(i);
623
+ }
624
+ return range;
625
+ };
626
+ export function totalLabel(milliseconds) {
627
+ let label = "";
628
+ const hourConv = milliseconds / 3600000;
629
+ const truncHour = Math.trunc(hourConv);
630
+ if (hourConv !== truncHour) {
631
+ const deciHour = hourConv - truncHour;
632
+ const minConv = deciHour * 60;
633
+ const truncMin = Math.trunc(minConv);
634
+ if (truncMin !== minConv) {
635
+ const deciMin = minConv - truncMin;
636
+ const secConv = deciMin * 60;
637
+ label = `${truncHour}:${truncMin}:${Math.trunc(secConv)}`;
638
+ }
639
+ else
640
+ label = `${truncHour}:${minConv}:0`;
641
+ }
642
+ else
643
+ label = `${hourConv}:0:0`;
644
+ return label;
645
+ }