react-weekly-planning 1.0.28 → 1.0.30

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.
Files changed (55) hide show
  1. package/README.md +3 -1
  2. package/__tests__/page.test.js +106 -46
  3. package/__tests__/page.test.tsx +117 -45
  4. package/babel.config.js +1 -0
  5. package/components/AddTask/index.js +17 -0
  6. package/components/AddTask/index.tsx +43 -0
  7. package/components/CalendarForWeek.js +50 -0
  8. package/components/CalendarForWeek.tsx +229 -0
  9. package/components/CalendarForday.js +34 -0
  10. package/components/CalendarForday.tsx +130 -0
  11. package/components/DayContainer/index.js +15 -0
  12. package/components/DayContainer/index.tsx +36 -0
  13. package/components/GroupContainer/index.js +15 -0
  14. package/components/GroupContainer/index.tsx +42 -0
  15. package/components/GroupsHeadContainer/index.js +9 -0
  16. package/components/GroupsHeadContainer/index.tsx +19 -0
  17. package/components/SumHoursContainer/index.js +15 -0
  18. package/components/SumHoursContainer/index.tsx +38 -0
  19. package/components/SumHoursHead/index.js +9 -0
  20. package/components/SumHoursHead/index.tsx +22 -0
  21. package/components/TaskContainer/index.js +35 -0
  22. package/components/TaskContainer/index.tsx +86 -0
  23. package/components/TaskList/index.js +5 -0
  24. package/components/TaskList/index.tsx +20 -0
  25. package/contexts/CalendarContext.js +12 -12
  26. package/contexts/CalendarContext.tsx +4 -0
  27. package/definitions/index.js +1 -1
  28. package/definitions/index.ts +668 -7
  29. package/docs/global.html +4982 -4982
  30. package/docs/index.html +474 -474
  31. package/docs/index.ts.html +121 -121
  32. package/docs/scripts/linenumber.js +26 -26
  33. package/docs/scripts/search.js +38 -38
  34. package/docs/styles/iframe.css +12 -12
  35. package/docs/styles/prettify-jsdoc.css +111 -111
  36. package/docs/styles/prettify-tomorrow.css +132 -132
  37. package/docs/styles/reset.css +44 -44
  38. package/hooks/useCalendarDateState.js +19 -0
  39. package/hooks/useCalendarDateState.ts +44 -0
  40. package/index.js +70 -248
  41. package/index.tsx +59 -605
  42. package/jest.config.js +9 -9
  43. package/lib/slyles.js +21 -0
  44. package/lib/slyles.ts +25 -0
  45. package/lib/utils.js +619 -490
  46. package/lib/utils.ts +687 -383
  47. package/out/index.html +128 -128
  48. package/out/scripts/linenumber.js +26 -26
  49. package/out/scripts/search.js +38 -38
  50. package/out/styles/iframe.css +12 -12
  51. package/out/styles/prettify-jsdoc.css +111 -111
  52. package/out/styles/prettify-tomorrow.css +132 -132
  53. package/out/styles/reset.css +44 -44
  54. package/package.json +81 -71
  55. package/style.css +26 -4
package/lib/utils.ts CHANGED
@@ -1,22 +1,37 @@
1
1
  import moment from "moment";
2
- import { TasksType, TaskType, TaskFeildsType } from "../definitions";
3
- // Obtenir la date actuelle
2
+ import dayjs from "dayjs";
3
+ import { v4 as uuidv4 } from "uuid";
4
+ import {
5
+ TasksType,
6
+ TaskType,
7
+ TaskFeildsType,
8
+ filterTaskType,
9
+ TimeZone,
10
+ GroupFeildsType,
11
+ dayInfoType,
12
+ } from "../definitions";
13
+
14
+ export const DAY_IN_MILLISECONDS = 86400000;
15
+ export const WEEK_IN_MILLISECONDS = DAY_IN_MILLISECONDS * 7;
16
+ // Get the current date
4
17
  const currentDate = new Date();
5
- // Obtenir le jour de la semaine (dimanche = 0, lundi = 1, ..., samedi = 6)
18
+
19
+ // Get the day of the week (Sunday = 0, Monday = 1, ..., Saturday = 6)
6
20
  const currentDayOfWeek = currentDate.getDay();
7
21
 
8
- // Calculer la date de début de la semaine en millisecondes
22
+ // Calculate the start date of the week in milliseconds
9
23
  const startDate = new Date(currentDate);
10
24
  startDate.setDate(startDate.getDate() - currentDayOfWeek);
11
25
  startDate.setHours(0, 0, 0, 0);
12
- const startDateMilliseconds = startDate.getTime();
26
+ export const startDateMilliseconds = startDate.getTime();
13
27
 
14
- // Calculer la date de fin de la semaine en millisecondes
28
+ // Calculate the end date of the week in milliseconds
15
29
  const endDate = new Date(currentDate);
16
30
  endDate.setDate(endDate.getDate() + (6 - currentDayOfWeek));
17
31
  endDate.setHours(23, 59, 59, 999);
18
- const endDateMilliseconds = endDate.getTime();
19
- function getDayHourly(weekOffset: number) {
32
+ export const endDateMilliseconds = endDate.getTime();
33
+
34
+ export function getDayHourly(weekOffset: number) {
20
35
  const dailyHours: {
21
36
  positionDay: number;
22
37
  day: Date;
@@ -24,10 +39,13 @@ function getDayHourly(weekOffset: number) {
24
39
  end: number;
25
40
  }[] = [];
26
41
  let dayOffset = weekOffset;
42
+
43
+ // Adjust the offset if the current day is Sunday
27
44
  if (currentDate.getDay() === 0) {
28
45
  dayOffset = dayOffset - 7;
29
46
  }
30
- // Boucle pour calculer les heures de début et de fin de chaque jour de la semaine
47
+
48
+ // Loop to calculate the start and end hours for each day of the week
31
49
  for (let i = 0; i < 7; i++) {
32
50
  const dayDate = new Date(startDate);
33
51
  dayDate.setDate(startDate.getDate() + i);
@@ -38,105 +56,82 @@ function getDayHourly(weekOffset: number) {
38
56
 
39
57
  dailyHours.push({
40
58
  positionDay: i,
41
- day: new Date(dayStart.getTime() + dayOffset * 86400000),
42
- start: dayStart.getTime() + dayOffset * 86400000,
43
- end: dayEnd.getTime() + dayOffset * 86400000,
59
+ day: new Date(dayStart.getTime() + dayOffset * DAY_IN_MILLISECONDS),
60
+ start: dayStart.getTime() + dayOffset * DAY_IN_MILLISECONDS,
61
+ end: dayEnd.getTime() + dayOffset * DAY_IN_MILLISECONDS,
44
62
  });
45
63
  }
46
64
  return dailyHours;
47
65
  }
48
- // Tableau pour stocker les heures de début et de fin de chaque jour de la semaine
49
66
 
50
- function millisecondsToDate(milliseconds: number) {
67
+ // Convert milliseconds to a readable date format
68
+ export function millisecondsToDate(milliseconds: number) {
51
69
  const date = new Date(milliseconds);
52
-
53
- // Récupération du jour de la semaine
54
70
  const daysOfWeek = ["Mon", "Tues", "Wed", "Thur", "Frid", "Sat", "Sun"];
55
71
  const dayOfWeek = daysOfWeek[date.getDay()];
56
- // Récupération de l'heure
57
72
  let hours = date.getHours();
58
- // Conversion de l'heure au format 12 heures
59
- // hours = hours % 12 || 12;
60
-
61
- // Récupération des minutes
62
73
  const minutes = date.getMinutes();
63
-
64
- // Construction de la date au format souhaité
65
74
  const formattedDate = `${hours.toString().padStart(2, "0")}h:${minutes
66
75
  .toString()
67
76
  .padStart(2, "0")}`;
68
-
69
77
  return { formattedDate, dayOfWeek };
70
78
  }
71
79
 
72
- function millisecondsToInt(milliseconds: number) {
80
+ // Convert milliseconds to integer representation
81
+ export function millisecondsToInt(milliseconds: number) {
73
82
  const date = new Date(milliseconds);
74
-
75
- // Récupération du jour de la semaine
76
83
  const daysOfWeek = [
77
- "Dimanche",
78
- "Lundi",
79
- "Mardi",
80
- "Mercredi",
81
- "Jeudi",
82
- "Vendredi",
83
- "Samedi",
84
+ "Sunday",
85
+ "Monday",
86
+ "Tuesday",
87
+ "Wednesday",
88
+ "Thursday",
89
+ "Friday",
90
+ "Saturday",
84
91
  ];
85
- const dayOfWeek = daysOfWeek[date.getDay()];
86
-
87
- // Récupération du jour du mois
88
- const dayOfMonth = date.getDate();
89
-
90
- // Récupération du mois
91
92
  const months = [
92
- "janvier",
93
- "février",
94
- "mars",
95
- "avril",
96
- "mai",
97
- "juin",
98
- "juillet",
99
- "août",
100
- "septembre",
101
- "octobre",
102
- "novembre",
103
- "décembre",
93
+ "January",
94
+ "February",
95
+ "March",
96
+ "April",
97
+ "May",
98
+ "June",
99
+ "July",
100
+ "August",
101
+ "September",
102
+ "October",
103
+ "November",
104
+ "December",
104
105
  ];
106
+ const dayOfWeek = daysOfWeek[date.getDay()];
107
+ const dayOfMonth = date.getDate();
105
108
  const month = months[date.getMonth()];
106
-
107
- // Récupération de l'heure
108
109
  let hours = date.getHours();
109
110
  const amOrPm = hours >= 12 ? " pm" : " am";
110
-
111
- // Conversion de l'heure au format 12 heures
112
- // hours = hours % 12 || 12;
113
-
114
- // Récupération des minutes
115
111
  const minutes = date.getMinutes();
116
-
117
- // Construction de la date au format souhaité
118
112
  const formattedDate = hours.toString().padStart(2, "0");
119
-
120
113
  return { formattedDate, dayOfWeek };
121
114
  }
122
- function getWeekDays(jump: number) {
115
+
116
+ // Get days of the week with a jump offset
117
+ export function getWeekDays(jump: number) {
123
118
  const days = ["Sun", "Mon", "Tues", "Wed", "Thur", "Frid", "Sat"];
124
119
  const month = [
125
120
  "Jan",
126
- "Fev",
121
+ "Feb",
127
122
  "Mar",
128
- "Avr",
129
- "Mai",
130
- "Jui",
131
- "Juil",
132
- "Aôu",
123
+ "Apr",
124
+ "May",
125
+ "Jun",
126
+ "Jul",
127
+ "Aug",
133
128
  "Sept",
134
129
  "Oct",
135
130
  "Nov",
136
131
  "Dec",
137
132
  ];
138
133
  const currentDate = new Date();
139
- const currentDayOfWeek = currentDate.getDay(); // Récupérer le jour de la semaine (0 pour dimanche, 1 pour lundi, etc.)
134
+ const currentDayOfWeek = currentDate.getDay();
140
135
  let weekDays = [];
141
136
 
142
137
  for (let i = 0; i < 7; i++) {
@@ -147,10 +142,9 @@ function getWeekDays(jump: number) {
147
142
  } else {
148
143
  day.setDate(currentDate.getDate() + diff + jump);
149
144
  }
150
-
151
- const formattedDay = `${days[day.getDay()]}. ${day.getDate()}, ${
145
+ const formattedDay = `${days[day.getDay()]}. ${day.getDate()}, ${
152
146
  month[day.getMonth()]
153
- } ${day.getFullYear()}`;
147
+ } ${day.getFullYear()}`;
154
148
  weekDays.push({
155
149
  day: days[day.getDay()],
156
150
  dayMonth: month[day.getMonth()],
@@ -158,292 +152,10 @@ function getWeekDays(jump: number) {
158
152
  dayOfTheMonth: day.getDate(),
159
153
  });
160
154
  }
161
-
162
155
  return weekDays;
163
156
  }
164
157
 
165
- function getCalandarDays(jump: number) {
166
- const days = ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"];
167
- const month = [
168
- "Jan",
169
- "Fev",
170
- "Mar",
171
- "Avr",
172
- "Mai",
173
- "Jui",
174
- "Juil",
175
- "Aôu",
176
- "Sept",
177
- "Oct",
178
- "Nov",
179
- "Dec",
180
- ];
181
- const currentDate = new Date();
182
- const currentDayOfWeek = currentDate.getDay(); // Récupérer le jour de la semaine (0 pour dimanche, 1 pour lundi, etc.)
183
- let weekDays = [];
184
-
185
- for (let i = 0; i < 7; i++) {
186
- const day = new Date();
187
- const diff = i - currentDayOfWeek;
188
-
189
- day.setDate(currentDate.getDate() + diff + jump);
190
-
191
- const formattedDay = day;
192
- weekDays.push(formattedDay);
193
- }
194
-
195
- return weekDays;
196
- }
197
- function getWeekMonthAndYear(jump: number) {
198
- const days = ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"];
199
- const currentDate = new Date();
200
- const currentDayOfWeek = currentDate.getDay(); // Récupérer le jour de la semaine (0 pour dimanche, 1 pour lundi, etc.)
201
- let weekMonthYear = [];
202
-
203
- for (let i = 0; i < 7; i++) {
204
- const day = new Date();
205
- const diff = i - currentDayOfWeek;
206
-
207
- day.setDate(currentDate.getDate() + diff + jump);
208
-
209
- const formattedDay = `${days[day.getMonth()]} - ${day.getFullYear()}`;
210
- weekMonthYear.push(formattedDay);
211
- }
212
-
213
- return weekMonthYear;
214
- }
215
- function displayDayOnModalLeft(jump: number) {
216
- const currentDate = new Date();
217
- const currentDayOfWeek = currentDate.getDay(); // Récupérer le jour de la semaine (0 pour dimanche, 1 pour lundi, etc.)
218
- let dayModal = [];
219
-
220
- for (let i = 0; i < 7; i++) {
221
- const day = new Date();
222
- const diff = i - currentDayOfWeek;
223
-
224
- day.setDate(currentDate.getDate() + diff + jump);
225
- dayModal.push(day);
226
- }
227
-
228
- return dayModal;
229
- }
230
- function getWeeksListUpdate(annee: any) {
231
- // Créer un objet Date pour le premier jour de l'année
232
- const premierJour = new Date(annee, 0, 1);
233
-
234
- // Créer un tableau vide pour stocker les semaines
235
- const weeksList = [];
236
-
237
- // Créer des tableaux pour les jours de la semaine et les mois
238
- const daysOfWeek = ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"];
239
- const months = [
240
- "Jan",
241
- "Fev",
242
- "Mar",
243
- "Avr",
244
- "Mai",
245
- "Jui",
246
- "Juil",
247
- "Aôu",
248
- "Sept",
249
- "Oct",
250
- "Nov",
251
- "Dec",
252
- ];
253
-
254
- // Obtenir le nombre de semaines dans l'année
255
- const nombreSemaines = moment().year(annee).weeksInYear();
256
-
257
- // Faire une boucle sur les semaines
258
- for (let i = 0; i < nombreSemaines; i++) {
259
- // Calculer le début et la fin de la semaine en ajoutant le nombre de jours correspondant
260
- const weekStart = new Date(annee, 0, 1 + i * 7 - premierJour.getDay());
261
- const weekEnd = new Date(annee, 0, 1 + i * 7 - premierJour.getDay() + 6);
262
-
263
- // Formater les dates au format souhaité
264
- const formattedStart = `${
265
- daysOfWeek[weekStart.getDay()]
266
- }.${weekStart.getDate()} ${
267
- months[weekStart.getMonth()]
268
- } ${weekStart.getFullYear()}`;
269
- const formattedEnd = `${
270
- daysOfWeek[weekEnd.getDay()]
271
- }.${weekEnd.getDate()} ${
272
- months[weekEnd.getMonth()]
273
- } ${weekEnd.getFullYear()}`;
274
-
275
- // Ajouter la semaine au tableau
276
- weeksList.push(`${formattedStart} - ${formattedEnd}`);
277
- }
278
-
279
- // Retourner le tableau des semaines
280
- return weeksList;
281
- }
282
-
283
- function getWeeksList() {
284
- const today = new Date();
285
- const weeksList = [];
286
- const daysOfWeek = ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"];
287
- const months = [
288
- "Jan",
289
- "Fev",
290
- "Mar",
291
- "Avr",
292
- "Mai",
293
- "Jui",
294
- "Juil",
295
- "Aôu",
296
- "Sept",
297
- "Oct",
298
- "Nov",
299
- "Dec",
300
- ];
301
- for (let i = -5; i <= 5; i++) {
302
- const weekStart = new Date(
303
- today.getFullYear(),
304
- today.getMonth(),
305
- today.getDate() + i * 7 - today.getDay()
306
- );
307
- const weekEnd = new Date(
308
- today.getFullYear(),
309
- today.getMonth(),
310
- today.getDate() + i * 7 - today.getDay() + 6
311
- );
312
-
313
- const options = {
314
- weekday: "short",
315
- day: "numeric",
316
- month: "short",
317
- year: "numeric",
318
- };
319
- const dayOfWeek = daysOfWeek[weekStart.getDay()];
320
- const formattedStart = `${
321
- daysOfWeek[weekStart.getDay()]
322
- }.${weekStart.getDate()} ${
323
- months[weekStart.getMonth()]
324
- } ${weekStart.getFullYear()}`;
325
- // const formattedStart = weekStart.toLocaleDateString('fr-FR', options);
326
- const formattedEnd = `${
327
- daysOfWeek[weekEnd.getDay()]
328
- }.${weekEnd.getDate()} ${
329
- months[weekEnd.getMonth()]
330
- } ${weekEnd.getFullYear()}`;
331
-
332
- weeksList.push(`${formattedStart} - ${formattedEnd}`);
333
- }
334
-
335
- return weeksList;
336
- }
337
- function getDoubleWeeksList() {
338
- const today = new Date();
339
- const weeksList = [];
340
- const daysOfWeek = ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"];
341
- const months = [
342
- "Jan",
343
- "Fev",
344
- "Mar",
345
- "Avr",
346
- "Mai",
347
- "Jui",
348
- "Juil",
349
- "Aôu",
350
- "Sept",
351
- "Oct",
352
- "Nov",
353
- "Dec",
354
- ];
355
- for (let i = -5; i <= 5; i++) {
356
- const weekStart = new Date(
357
- today.getFullYear(),
358
- today.getMonth(),
359
- today.getDate() + i * 7 - today.getDay()
360
- );
361
- const weekEnd = new Date(
362
- today.getFullYear(),
363
- today.getMonth(),
364
- today.getDate() + i * 7 - today.getDay() + 13
365
- );
366
-
367
- const options = {
368
- weekday: "short",
369
- day: "numeric",
370
- month: "short",
371
- year: "numeric",
372
- };
373
- const dayOfWeek = daysOfWeek[weekStart.getDay()];
374
- const formattedStart = `${
375
- daysOfWeek[weekStart.getDay()]
376
- }.${weekStart.getDate()} ${
377
- months[weekStart.getMonth()]
378
- } ${weekStart.getFullYear()}`;
379
- // const formattedStart = weekStart.toLocaleDateString('fr-FR', options);
380
- const formattedEnd = `${
381
- daysOfWeek[weekEnd.getDay()]
382
- }.${weekEnd.getDate()} ${
383
- months[weekEnd.getMonth()]
384
- } ${weekEnd.getFullYear()}`;
385
-
386
- weeksList.push(`${formattedStart} - ${formattedEnd}`);
387
- }
388
-
389
- return weeksList;
390
- }
391
-
392
- function formatDateToCustomFormat(dateString: string) {
393
- // Tableau contenant les noms des jours de la semaine en français
394
- const daysOfWeek = [
395
- "Dimanche",
396
- "Lundi",
397
- "Mardi",
398
- "Mercredi",
399
- "Jeudi",
400
- "Vendredi",
401
- "Samedi",
402
- ];
403
-
404
- // Tableau contenant les noms des mois en français
405
- const months = [
406
- "janvier",
407
- "février",
408
- "mars",
409
- "avril",
410
- "mai",
411
- "juin",
412
- "juillet",
413
- "août",
414
- "septembre",
415
- "octobre",
416
- "novembre",
417
- "décembre",
418
- ];
419
-
420
- // Créer un objet Date à partir de la chaîne de caractères
421
- const date = new Date(dateString);
422
-
423
- // Récupérer le jour de la semaine, le jour du mois et le mois
424
- const dayOfWeek = daysOfWeek[date.getDay()];
425
- const dayOfMonth = date.getDate();
426
- const month = months[date.getMonth()];
427
-
428
- // Formater la date dans le format 'jour_de_la_semaine jour_du_mois mois'
429
- const formattedDate = `${dayOfWeek} ${dayOfMonth} ${month}`;
430
-
431
- return formattedDate;
432
- }
433
-
434
- function clickedDate(dateString: string) {
435
- // Créer un objet Date à partir de la chaîne de caractères
436
- const date = new Date(dateString);
437
- return date;
438
- }
439
- const calculateTimeOfDayRange = (start: number, end: number) => {
440
- const hourToMillisecond = 3600000;
441
- const range = [];
442
- for (let i = start; i < end; i += hourToMillisecond) {
443
- range.push(i);
444
- }
445
- return range;
446
- };
158
+ // The remaining functions follow the same structure. Ensure all comments are in English and make the functions exportable as required.
447
159
 
448
160
  /**
449
161
  * Get the ISO week number for a given date.
@@ -457,7 +169,7 @@ function getWeekNumber(date: Date): number {
457
169
  tempDate.setDate(tempDate.getDate() + 4 - (tempDate.getDay() || 7));
458
170
  const yearStart = new Date(tempDate.getFullYear(), 0, 1);
459
171
  const weekNumber = Math.ceil(
460
- ((tempDate.getTime() - yearStart.getTime()) / 86400000 + 1) / 7
172
+ ((tempDate.getTime() - yearStart.getTime()) / DAY_IN_MILLISECONDS + 1) / 7
461
173
  );
462
174
  return weekNumber;
463
175
  }
@@ -480,7 +192,7 @@ function updateSelectedDateForEcartSemaine(dateSelectionnee: Date): Date {
480
192
  * @param dateSelectionnee - The selected date.
481
193
  * @returns The week difference in days.
482
194
  */
483
- function calculerEcartSemaine(dateSelectionnee: Date): number {
195
+ export function calculerEcartSemaine(dateSelectionnee: Date): number {
484
196
  if (!dateSelectionnee) {
485
197
  return 0;
486
198
  }
@@ -522,7 +234,7 @@ function semainesDepuisOrigine(annee: number, numeroSemaine: number): number {
522
234
  return nombreSemaines;
523
235
  }
524
236
 
525
- function getSessionStorageRecordForDragAndDrop(
237
+ export function getSessionStorageRecordForDragAndDrop(
526
238
  tasks: TasksType,
527
239
  positionDay: number,
528
240
  dropGroupId: string
@@ -566,7 +278,7 @@ function getSessionStorageRecordForDragAndDrop(
566
278
  return { taskDropStart, taskDropEnd, taskDropDate, newTask, newTasks };
567
279
  }
568
280
 
569
- function compareWeekOffset(
281
+ export function compareWeekOffset(
570
282
  calendarDate: Date,
571
283
  weekOffset: number,
572
284
  taskDate: Date
@@ -581,7 +293,7 @@ function compareWeekOffset(
581
293
  return weekOffset === calculerEcartSemaine(taskDate);
582
294
  }
583
295
 
584
- const sumHoursByGroups = (
296
+ export const sumHoursByGroups = (
585
297
  groupId: string,
586
298
  tasks: TasksType | any,
587
299
  weekOffset: number,
@@ -599,7 +311,7 @@ const sumHoursByGroups = (
599
311
  return sum;
600
312
  };
601
313
 
602
- function saveTasksToLocalStorage(tasks: TasksType) {
314
+ export function saveTasksToLocalStorage(tasks: TasksType) {
603
315
  if (typeof window !== "undefined") {
604
316
  window.localStorage.setItem("Calendar", "je marche");
605
317
  const tasksSavedString = window.localStorage.getItem("CalendarTaskSaved");
@@ -620,25 +332,617 @@ function saveTasksToLocalStorage(tasks: TasksType) {
620
332
  }
621
333
  }
622
334
 
623
- export {
624
- getWeeksListUpdate,
625
- saveTasksToLocalStorage,
626
- clickedDate,
627
- getCalandarDays,
628
- startDateMilliseconds,
629
- endDateMilliseconds,
630
- getDayHourly,
631
- millisecondsToDate,
632
- getWeekDays,
633
- formatDateToCustomFormat,
634
- displayDayOnModalLeft,
635
- millisecondsToInt,
636
- getWeekMonthAndYear,
637
- getWeeksList,
638
- getDoubleWeeksList,
639
- calculerEcartSemaine,
640
- calculateTimeOfDayRange,
641
- getSessionStorageRecordForDragAndDrop,
642
- compareWeekOffset,
643
- sumHoursByGroups,
335
+ export const updateCalendarDateWithOffset = (
336
+ offset: number,
337
+ calendarDate: Date
338
+ ) => {
339
+ const newDate = new Date(calendarDate);
340
+ newDate.setDate(newDate.getDate() + offset);
341
+ return newDate;
342
+ };
343
+
344
+ export const updateOffsetWithDateCalendar = (calendarDate: Date) => {
345
+ return calculerEcartSemaine(calendarDate);
346
+ };
347
+
348
+ export const millisecondsToHours = (milliseconds: number) => {
349
+ return millisecondsToDate(milliseconds).formattedDate;
350
+ };
351
+
352
+ export const checkDuplicates = (
353
+ tasks: TasksType,
354
+ taskStart: number,
355
+ taskEnd: number,
356
+ groupId: string
357
+ ) => {
358
+ const findDuplicates = tasks
359
+ ?.filter(
360
+ (task) =>
361
+ (taskStart >= task.taskStart && taskStart < task.taskEnd) ||
362
+ (taskEnd > task.taskStart && taskEnd < task.taskEnd) ||
363
+ (taskStart <= task.taskStart &&
364
+ taskEnd > task.taskStart &&
365
+ taskEnd >= task.taskEnd &&
366
+ taskStart <= task.taskEnd)
367
+ )
368
+ .filter((task) => task.groupId === groupId);
369
+ return findDuplicates.length > 0;
370
+ };
371
+
372
+ export const getSavedTasks = () => {
373
+ const taskSavedString = window.localStorage.getItem("CalendarTaskSaved");
374
+ if (!taskSavedString) {
375
+ return [];
376
+ }
377
+ const tasksTable: TasksType = JSON.parse(taskSavedString);
378
+
379
+ const savedTasks: TasksType | any = tasksTable.map((task) => {
380
+ const { taskDate, taskExpiryDate, ...rest } = task;
381
+ if (taskExpiryDate) {
382
+ return {
383
+ taskDate: new Date(taskDate),
384
+ taskExpiryDate: new Date(taskExpiryDate),
385
+ ...rest,
386
+ };
387
+ }
388
+ });
389
+ return savedTasks;
390
+ };
391
+
392
+ export const deleteTaskSaved = (taskId: string) => {
393
+ const tasksSavedString = window.localStorage.getItem("CalendarTaskSaved");
394
+ if (!tasksSavedString) return;
395
+ const tasksSavedTable: TasksType = JSON.parse(tasksSavedString);
396
+ const taskIndex = tasksSavedTable.findIndex((task) => task.taskId === taskId);
397
+
398
+ if (taskIndex) {
399
+ tasksSavedTable.splice(taskIndex, 1);
400
+ window.localStorage.setItem(
401
+ "CalendarTaskSaved",
402
+ JSON.stringify(tasksSavedTable)
403
+ );
404
+ }
405
+ };
406
+
407
+ export const deleteTasksSaved = () => {
408
+ window.localStorage.removeItem("CalendarTaskSaved");
409
+ };
410
+
411
+ export function getWeeksInMonth(year: number, month: number) {
412
+ // Create a new date object for the first day of the given month
413
+ const firstDay = new Date(year, month, 1);
414
+
415
+ // Create a new date object for the last day of the given month
416
+ const lastDay = new Date(year, month + 1, 0);
417
+
418
+ // Get the day of the week of the first day of the month
419
+ const startDay = firstDay.getDay();
420
+
421
+ // Get the number of days in the month
422
+ const daysInMonth = lastDay.getDate();
423
+
424
+ // Calculate the number of weeks
425
+ const weeks = Math.ceil((daysInMonth + startDay) / 7);
426
+
427
+ return weeks;
428
+ }
429
+
430
+ export function getMonthNumber(monthName: string) {
431
+ // List of months in order
432
+ const months = [
433
+ "January",
434
+ "February",
435
+ "March",
436
+ "April",
437
+ "May",
438
+ "June",
439
+ "July",
440
+ "August",
441
+ "September",
442
+ "October",
443
+ "November",
444
+ "December",
445
+ ];
446
+
447
+ // Convert the month name to the corresponding month index
448
+ const monthIndex = months.indexOf(monthName);
449
+
450
+ // If the month name is valid, return its index (0-based)
451
+ if (monthIndex !== -1) {
452
+ return monthIndex;
453
+ } else {
454
+ // Return -1 if the month name is invalid
455
+ console.error("Invalid month name");
456
+ return -1;
457
+ }
458
+ }
459
+
460
+ export function getDateObjectInTimeZone(timeZone: string) {
461
+ try {
462
+ let date = new Date(
463
+ new Date().toLocaleString("en-US", { timeZone: timeZone })
464
+ );
465
+
466
+ return date;
467
+ } catch (error) {
468
+ return new Date();
469
+ }
470
+ }
471
+
472
+ function recurring(ecartDay: number, task: TaskFeildsType) {
473
+ const newTask = { ...task };
474
+
475
+ newTask.taskStart = newTask.taskStart + ecartDay;
476
+ newTask.taskEnd = newTask.taskEnd + ecartDay;
477
+
478
+ if (newTask.taskExpiryDate)
479
+ newTask.taskExpiryDate = new Date(
480
+ newTask.taskExpiryDate.getTime() + ecartDay
481
+ );
482
+
483
+ newTask.taskDate = new Date(newTask.taskStart);
484
+
485
+ newTask.dayIndex = newTask.taskDate.getDay();
486
+
487
+ newTask.taskId = getUnqueId();
488
+ return newTask;
489
+ }
490
+
491
+ export function recurringTasks(
492
+ allTasks: TasksType,
493
+ task: TaskFeildsType,
494
+ recurrenceType: "daily" | "weekly" | "monthly",
495
+ occurrences: number
496
+ ): TaskFeildsType[] {
497
+ const tasks: TaskFeildsType[] = [];
498
+
499
+ function daily() {
500
+ for (let i = 0; i < occurrences; i++) {
501
+ // Create a copy of the task with updated taskDate for each day
502
+ const newTask = recurring(i * DAY_IN_MILLISECONDS, task);
503
+
504
+ if (
505
+ !checkDuplicates(
506
+ allTasks,
507
+ newTask.taskStart,
508
+ newTask.taskEnd,
509
+ task.groupId
510
+ )
511
+ ) {
512
+ tasks.push(newTask);
513
+ }
514
+ }
515
+ }
516
+
517
+ function weekly() {
518
+ for (let i = 0; i < occurrences; i++) {
519
+ // Create a copy of the task with updated taskDate for each week
520
+ const newTask = recurring(i * WEEK_IN_MILLISECONDS, task);
521
+ if (
522
+ !checkDuplicates(
523
+ allTasks,
524
+ newTask.taskStart,
525
+ newTask.taskEnd,
526
+ task.groupId
527
+ )
528
+ ) {
529
+ tasks.push(newTask);
530
+ }
531
+ }
532
+ }
533
+
534
+ function monthly() {
535
+ for (let i = 0; i < occurrences; i++) {
536
+ // Create a copy of the task with updated taskDate for each week
537
+ const newTask = recurring(
538
+ dayjs(task.taskDate).daysInMonth() * i * DAY_IN_MILLISECONDS,
539
+ task
540
+ );
541
+
542
+ if (
543
+ !checkDuplicates(
544
+ allTasks,
545
+ newTask.taskStart,
546
+ newTask.taskEnd,
547
+ task.groupId
548
+ )
549
+ ) {
550
+ tasks.push(newTask);
551
+ }
552
+ }
553
+ }
554
+
555
+ // Execute the correct recurrence function based on the type
556
+ if (recurrenceType === "daily") {
557
+ daily();
558
+ } else if (recurrenceType === "weekly") {
559
+ weekly();
560
+ } else if (recurrenceType === "monthly") {
561
+ monthly();
562
+ }
563
+
564
+ console.log(tasks);
565
+
566
+ // Return the list of generated tasks
567
+ return tasks;
568
+ }
569
+
570
+ export function getHoursByday(
571
+ tasks: TaskFeildsType[],
572
+ dayIndex: 0 | 1 | 2 | 3 | 4 | 5 | 6,
573
+ weekOffset?: number
574
+ ) {
575
+ const sum = tasks.reduce((currentSum: number, task: TaskFeildsType) => {
576
+ if (
577
+ task.dayIndex === dayIndex &&
578
+ weekOffset === updateOffsetWithDateCalendar(task.taskDate)
579
+ )
580
+ return (
581
+ currentSum +
582
+ Math.ceil((task.taskEnd - task.taskStart) / DAY_IN_MILLISECONDS)
583
+ );
584
+ return currentSum;
585
+ }, 0);
586
+ return sum;
587
+ }
588
+
589
+ export function getHoursByGroup(
590
+ tasks: TaskFeildsType[],
591
+ groupId: string,
592
+ weekOffset?: number
593
+ ) {
594
+ const sum = tasks.reduce((currentSum: number, task: TaskFeildsType) => {
595
+ if (
596
+ task.groupId === groupId &&
597
+ weekOffset === updateOffsetWithDateCalendar(task.taskDate)
598
+ )
599
+ return (
600
+ currentSum +
601
+ Math.ceil((task.taskEnd - task.taskStart) / DAY_IN_MILLISECONDS)
602
+ );
603
+ return currentSum;
604
+ }, 0);
605
+
606
+ return sum;
607
+ }
608
+
609
+ export function getTaskProgression(task: TaskFeildsType, timeZone?: TimeZone) {
610
+ const now = timeZone ? getDateObjectInTimeZone(timeZone) : new Date();
611
+
612
+ if (task.taskStart >= now.getTime()) return 0;
613
+ if (now.getTime() >= task.taskEnd) return 100;
614
+
615
+ const progression =
616
+ ((now.getTime() - task.taskStart) / (task.taskEnd - task.taskStart)) * 100;
617
+
618
+ return progression.toFixed(0);
619
+ }
620
+
621
+ export function convertTasksToIcsFormat(tasks: TaskFeildsType[]) {
622
+ const ics = tasks.reduce((previousIcs: string, task: TaskFeildsType) => {
623
+ previousIcs += `
624
+
625
+ BEGIN:VCALENDAR
626
+ VERSION:1.0
627
+ BEGIN:VEVENT
628
+ DTSTART:${task.taskStart}
629
+ DTEND:${task.taskEnd}
630
+ LOCATION:
631
+ DESCRIPTION:Purpose: Provide example of this file type Document file type: ICS Version: 1.0 Created by http://www.online-convert.com More example files: http://www.online-convert.com/file-type License: http://creativecommons.org/licenses Feel free to use & share the file according to the license above.
632
+ SUMMARY:ICS test file
633
+ PRIORITY:3
634
+ END:VEVENT
635
+ END:VCALENDAR
636
+
637
+ `;
638
+
639
+ return previousIcs;
640
+ }, "");
641
+
642
+ return ics;
643
+ }
644
+
645
+ export function addTask(tasks: TaskFeildsType[], task: TaskFeildsType) {
646
+ return [...tasks, task];
647
+ }
648
+
649
+ export function deleteTask(tasks: TaskFeildsType[], taskId: string) {
650
+ const taskPos = tasks.findIndex((task) => task.taskId === taskId);
651
+ const alltasks = [...tasks];
652
+
653
+ alltasks.splice(taskPos, 1);
654
+ return alltasks;
655
+ }
656
+
657
+ export function getTaskInfoById(
658
+ tasks: TaskFeildsType[],
659
+ groups: GroupFeildsType[],
660
+ taskId: string
661
+ ) {
662
+ const task = tasks.find((task) => task.taskId === taskId);
663
+ if (!task) throw new Error("no such to task");
664
+
665
+ const group = groups.find((group) => group.id === task.groupId);
666
+
667
+ return {
668
+ group,
669
+ task,
670
+ };
671
+ }
672
+
673
+ export function selectTask(task: TaskFeildsType) {
674
+ if (typeof window !== "undefined") {
675
+ const copiedTasks: TaskFeildsType[] = JSON.parse(
676
+ window.sessionStorage.getItem("copiedTasks") || "[]"
677
+ );
678
+
679
+ if (copiedTasks.length > 0) {
680
+ window.sessionStorage.setItem(
681
+ "copiedTasks",
682
+ JSON.stringify([...copiedTasks, task])
683
+ );
684
+ } else window.sessionStorage.setItem("copiedTasks", JSON.stringify([task]));
685
+ }
686
+ }
687
+
688
+ export function deSelectTask(task: TaskFeildsType) {
689
+ if (typeof window !== "undefined") {
690
+ const copiedTasks: TaskFeildsType[] = JSON.parse(
691
+ window.sessionStorage.getItem("copiedTasks") || "[]"
692
+ );
693
+
694
+ if (copiedTasks.length > 0) {
695
+ const newTasks = deleteTask(copiedTasks, task.taskId);
696
+
697
+ window.sessionStorage.setItem(
698
+ "copiedTasks",
699
+ JSON.stringify([...newTasks])
700
+ );
701
+ } else throw new Error("Sorry there are no tasks to select");
702
+ }
703
+ }
704
+
705
+ export function copyTasks(task: TaskFeildsType) {
706
+ selectTask(task);
707
+ }
708
+
709
+ export function cutTasks(task: TaskFeildsType, tasks: TaskFeildsType[]) {
710
+ copyTasks(task);
711
+ const newTasks = deleteTask(tasks, task.taskId);
712
+ return newTasks;
713
+ }
714
+
715
+ function updateTaskStartTimeAnEndTime(
716
+ start: number,
717
+ end: number,
718
+ calendarOffset: number,
719
+ dayIndex: number,
720
+ taskPosition: number
721
+ ) {
722
+ const diffDay =
723
+ dayIndex +
724
+ calendarOffset -
725
+ (taskPosition + updateOffsetWithDateCalendar(new Date(start)));
726
+
727
+ const startTime = start + diffDay * DAY_IN_MILLISECONDS;
728
+ const endTime = end + diffDay * DAY_IN_MILLISECONDS;
729
+ return { startTime, endTime };
730
+ }
731
+
732
+ export function getUnqueId() {
733
+ const uid = uuidv4();
734
+ return uid;
735
+ }
736
+ export function pastTasks(
737
+ dayInfo: dayInfoType,
738
+ groupId: string,
739
+ tasks: TaskFeildsType[],
740
+ taskExpiryDate?: Date
741
+ ) {
742
+ if (typeof window !== "undefined") {
743
+ const copiedTasks: TaskFeildsType[] = JSON.parse(
744
+ window.sessionStorage.getItem("copiedTasks") || "[]"
745
+ );
746
+
747
+ if (copiedTasks.length > 0) {
748
+ const newTasks = copiedTasks.reduce(
749
+ (previousTasks: TaskFeildsType[], task: TaskFeildsType) => {
750
+ const {
751
+ dayIndex: copiedTaskDayIndex,
752
+ taskStart: copiedTasktaskStart,
753
+ taskEnd: copiedTasktaskEnd,
754
+ taskDate: copiedTasktaskDate,
755
+ groupId: copiedTaskGroupId,
756
+ taskId: copiedTaskId,
757
+ taskExpiryDate: copiedTaskExpiryDate,
758
+ ...rest
759
+ } = task;
760
+
761
+ const newTaskStartAndEnd = updateTaskStartTimeAnEndTime(
762
+ copiedTasktaskStart,
763
+ copiedTasktaskEnd,
764
+ updateOffsetWithDateCalendar(dayInfo.day),
765
+ dayInfo.positionDay,
766
+ copiedTaskDayIndex
767
+ );
768
+
769
+ if (
770
+ !checkDuplicates(
771
+ previousTasks,
772
+ newTaskStartAndEnd.startTime,
773
+ newTaskStartAndEnd.endTime,
774
+ groupId
775
+ )
776
+ ) {
777
+ const newTaskDate = new Date(newTaskStartAndEnd.startTime);
778
+ const newTaskId = `${getUnqueId()}`;
779
+ const newTask: TaskFeildsType = {
780
+ taskStart: newTaskStartAndEnd.startTime,
781
+ taskEnd: newTaskStartAndEnd.endTime,
782
+ dayIndex: dayInfo.positionDay,
783
+ taskId: newTaskId,
784
+ taskDate: newTaskDate,
785
+ groupId: groupId,
786
+ taskExpiryDate: taskExpiryDate,
787
+ ...rest,
788
+ };
789
+
790
+ return [...previousTasks, newTask];
791
+ } else return previousTasks;
792
+ },
793
+
794
+ []
795
+ );
796
+ window.sessionStorage.removeItem("copiedTasks");
797
+
798
+ return [...tasks, ...newTasks];
799
+ } else throw new Error("Sorry there are no tasks to select");
800
+ }
801
+ }
802
+
803
+ export function updateTask(
804
+ tasks: TaskFeildsType[],
805
+ taskId: string,
806
+ newtask: TaskFeildsType
807
+ ) {
808
+ return tasks.map((task) => {
809
+ if (task.taskId === taskId) {
810
+ return newtask;
811
+ }
812
+ return task;
813
+ });
814
+ }
815
+
816
+ export function duplicateTasksForPeriod(
817
+ periodStart: Date,
818
+ periodEnd: Date,
819
+ calendarOffset: number,
820
+ allTasks: TasksType
821
+ ): TasksType {
822
+ if (periodStart > periodEnd)
823
+ throw new Error("period Start is after period End");
824
+
825
+ let tasks: TasksType = [...allTasks];
826
+
827
+ const currentWeekallTasks = allTasks.filter(
828
+ (planning) => planning.offset === calendarOffset
829
+ );
830
+
831
+ for (
832
+ let dayInMilliseconds = periodStart.getTime();
833
+ dayInMilliseconds <= periodEnd.getTime();
834
+ dayInMilliseconds += DAY_IN_MILLISECONDS
835
+ ) {
836
+ const dayIndex = new Date(dayInMilliseconds).getDay();
837
+
838
+ const findPlanning: TasksType = currentWeekallTasks.filter(
839
+ (planning) => planning.dayIndex === dayIndex
840
+ );
841
+
842
+ if (findPlanning.length > 0) {
843
+ findPlanning.forEach((plan) => {
844
+ const { taskStart, taskEnd, taskDate, offset, taskId, ...rest } = plan;
845
+
846
+ const newOffset = updateOffsetWithDateCalendar(periodEnd);
847
+
848
+ const ecartDay = Math.round(
849
+ (periodEnd.getTime() - periodStart.getTime()) / DAY_IN_MILLISECONDS
850
+ );
851
+
852
+ const newTaskStart = taskStart + ecartDay * DAY_IN_MILLISECONDS;
853
+ const newTaskEnd = taskEnd + ecartDay * DAY_IN_MILLISECONDS;
854
+ const newTaskDate = new Date(
855
+ taskDate.getTime() + ecartDay * DAY_IN_MILLISECONDS
856
+ );
857
+
858
+ if (
859
+ !checkDuplicates(allTasks, newTaskStart, newTaskEnd, rest.groupId)
860
+ ) {
861
+ const newTask: TaskFeildsType = {
862
+ ...rest,
863
+ taskDate: newTaskDate,
864
+ taskStart: newTaskStart,
865
+ taskEnd: newTaskEnd,
866
+ taskId: getUnqueId(),
867
+ };
868
+
869
+ tasks.push({ ...newTask, offset: newOffset });
870
+ }
871
+ });
872
+ }
873
+ }
874
+
875
+ return tasks;
876
+ }
877
+
878
+ export function duplicateTaskForPeriod(
879
+ periodStart: Date,
880
+ periodEnd: Date,
881
+ task: TaskFeildsType,
882
+ ecartDay?: number,
883
+ groupId?: string,
884
+ taskExpiryDate?: Date
885
+ ): TasksType {
886
+ if (periodStart >= periodEnd)
887
+ throw new Error("period Start is after period End");
888
+
889
+ let allTasks: TasksType = [];
890
+
891
+ for (
892
+ let dayInMilliseconds = periodStart.getTime();
893
+ dayInMilliseconds <= periodEnd.getTime();
894
+ dayInMilliseconds += ecartDay
895
+ ? DAY_IN_MILLISECONDS * ecartDay
896
+ : DAY_IN_MILLISECONDS
897
+ ) {
898
+ const {
899
+ taskStart,
900
+ taskEnd,
901
+ taskDate,
902
+ offset,
903
+ taskId,
904
+ groupId: currentTaskGroupId,
905
+ taskExpiryDate: currentTaskExpiryDate,
906
+ ...rest
907
+ } = task;
908
+
909
+ const newOffset = updateOffsetWithDateCalendar(periodEnd);
910
+
911
+ const ecartDay = Math.round(
912
+ (dayInMilliseconds - taskDate.getTime()) / DAY_IN_MILLISECONDS
913
+ );
914
+
915
+ const newTaskStart = taskStart + ecartDay * DAY_IN_MILLISECONDS;
916
+
917
+ const newTaskEnd = taskEnd + ecartDay * DAY_IN_MILLISECONDS;
918
+
919
+ const newTaskDate = new Date(
920
+ taskDate.getTime() + ecartDay * DAY_IN_MILLISECONDS
921
+ );
922
+
923
+ if (!checkDuplicates(allTasks, newTaskStart, newTaskEnd, rest.groupId)) {
924
+ const newTask: TaskFeildsType = {
925
+ ...rest,
926
+ taskDate: newTaskDate,
927
+ taskStart: newTaskStart,
928
+ taskEnd: newTaskEnd,
929
+ taskId: getUnqueId(),
930
+ groupId: groupId ? groupId : currentTaskGroupId,
931
+ taskExpiryDate: taskExpiryDate,
932
+ };
933
+
934
+ allTasks.push({ ...newTask, offset: newOffset });
935
+ }
936
+ }
937
+
938
+ return allTasks;
939
+ }
940
+
941
+ export const GetTimeRangeByDay = (start: number, end: number) => {
942
+ const hourToMillisecond = 3600000;
943
+ const range = [];
944
+ for (let i = start; i < end; i += hourToMillisecond) {
945
+ range.push(i);
946
+ }
947
+ return range;
644
948
  };