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.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // @flow
  2. import {stringToDate} from './Planning';
  3. import type {EventType} from '../screens/Home/HomeScreen';
  4. /**
  5. * Gets the time limit depending on the current day:
  6. * 17:30 for every day of the week except for thursday 11:30
  7. * 00:00 on weekends
  8. */
  9. export function getTodayEventTimeLimit(): Date {
  10. const now = new Date();
  11. if (now.getDay() === 4)
  12. // Thursday
  13. now.setHours(11, 30, 0);
  14. else if (now.getDay() === 6 || now.getDay() === 0)
  15. // Weekend
  16. now.setHours(0, 0, 0);
  17. else now.setHours(17, 30, 0);
  18. return now;
  19. }
  20. /**
  21. * Gets the duration (in milliseconds) of an event
  22. *
  23. * @param event {EventType}
  24. * @return {number} The number of milliseconds
  25. */
  26. export function getEventDuration(event: EventType): number {
  27. const start = stringToDate(event.date_begin);
  28. const end = stringToDate(event.date_end);
  29. let duration = 0;
  30. if (start != null && end != null) duration = end - start;
  31. return duration;
  32. }
  33. /**
  34. * Gets events starting after the limit
  35. *
  36. * @param events
  37. * @param limit
  38. * @return {Array<Object>}
  39. */
  40. export function getEventsAfterLimit(
  41. events: Array<EventType>,
  42. limit: Date,
  43. ): Array<EventType> {
  44. const validEvents = [];
  45. events.forEach((event: EventType) => {
  46. const startDate = stringToDate(event.date_begin);
  47. if (startDate != null && startDate >= limit) {
  48. validEvents.push(event);
  49. }
  50. });
  51. return validEvents;
  52. }
  53. /**
  54. * Gets the event with the longest duration in the given array.
  55. * If all events have the same duration, return the first in the array.
  56. *
  57. * @param events
  58. */
  59. export function getLongestEvent(events: Array<EventType>): EventType {
  60. let longestEvent = events[0];
  61. let longestTime = 0;
  62. events.forEach((event: EventType) => {
  63. const time = getEventDuration(event);
  64. if (time > longestTime) {
  65. longestTime = time;
  66. longestEvent = event;
  67. }
  68. });
  69. return longestEvent;
  70. }
  71. /**
  72. * Gets events that have not yet ended/started
  73. *
  74. * @param events
  75. */
  76. export function getFutureEvents(events: Array<EventType>): Array<EventType> {
  77. const validEvents = [];
  78. const now = new Date();
  79. events.forEach((event: EventType) => {
  80. const startDate = stringToDate(event.date_begin);
  81. const endDate = stringToDate(event.date_end);
  82. if (startDate != null) {
  83. if (startDate > now) validEvents.push(event);
  84. else if (endDate != null) {
  85. if (endDate > now || endDate < startDate)
  86. // Display event if it ends the following day
  87. validEvents.push(event);
  88. }
  89. }
  90. });
  91. return validEvents;
  92. }
  93. /**
  94. * Gets the event to display in the preview
  95. *
  96. * @param events
  97. * @return {EventType | null}
  98. */
  99. export function getDisplayEvent(events: Array<EventType>): EventType | null {
  100. let displayEvent = null;
  101. if (events.length > 1) {
  102. const eventsAfterLimit = getEventsAfterLimit(
  103. events,
  104. getTodayEventTimeLimit(),
  105. );
  106. if (eventsAfterLimit.length > 0) {
  107. if (eventsAfterLimit.length === 1) [displayEvent] = eventsAfterLimit;
  108. else displayEvent = getLongestEvent(events);
  109. } else {
  110. displayEvent = getLongestEvent(events);
  111. }
  112. } else if (events.length === 1) {
  113. [displayEvent] = events;
  114. }
  115. return displayEvent;
  116. }