Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Home.ts 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import {stringToDate} from './Planning';
  20. import type {PlanningEventType} from './Planning';
  21. /**
  22. * Gets the time limit depending on the current day:
  23. * 17:30 for every day of the week except for thursday 11:30
  24. * 00:00 on weekends
  25. */
  26. export function getTodayEventTimeLimit(): Date {
  27. const now = new Date();
  28. if (now.getDay() === 4) {
  29. // Thursday
  30. now.setHours(11, 30, 0);
  31. } else if (now.getDay() === 6 || now.getDay() === 0) {
  32. // Weekend
  33. now.setHours(0, 0, 0);
  34. } else {
  35. now.setHours(17, 30, 0);
  36. }
  37. return now;
  38. }
  39. /**
  40. * Gets events starting after the limit
  41. *
  42. * @param events
  43. * @param limit
  44. * @return {Array<Object>}
  45. */
  46. export function getEventsAfterLimit(
  47. events: Array<PlanningEventType>,
  48. limit: Date,
  49. ): Array<PlanningEventType> {
  50. const validEvents: Array<PlanningEventType> = [];
  51. events.forEach((event: PlanningEventType) => {
  52. const startDate = stringToDate(event.date_begin);
  53. if (startDate != null && startDate >= limit) {
  54. validEvents.push(event);
  55. }
  56. });
  57. return validEvents;
  58. }
  59. /**
  60. * Gets events that have not yet ended/started
  61. *
  62. * @param events
  63. */
  64. export function getFutureEvents(
  65. events: Array<PlanningEventType>,
  66. ): Array<PlanningEventType> {
  67. const validEvents: Array<PlanningEventType> = [];
  68. const now = new Date();
  69. events.forEach((event: PlanningEventType) => {
  70. const startDate = stringToDate(event.date_begin);
  71. if (startDate != null && startDate > now) {
  72. validEvents.push(event);
  73. }
  74. });
  75. return validEvents;
  76. }
  77. /**
  78. * Gets the event to display in the preview
  79. *
  80. * @param events
  81. * @return {PlanningEventType | null}
  82. */
  83. export function getDisplayEvent(
  84. events: Array<PlanningEventType>,
  85. ): PlanningEventType | null {
  86. let displayEvent = null;
  87. if (events.length > 1) {
  88. const eventsAfterLimit = getEventsAfterLimit(
  89. events,
  90. getTodayEventTimeLimit(),
  91. );
  92. if (eventsAfterLimit.length > 0) {
  93. [displayEvent] = eventsAfterLimit;
  94. } else {
  95. [displayEvent] = events;
  96. }
  97. } else if (events.length === 1) {
  98. [displayEvent] = events;
  99. }
  100. return displayEvent;
  101. }