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.

Planning.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // @flow
  2. export type PlanningEventType = {
  3. id: number,
  4. title: string,
  5. logo: string,
  6. date_begin: string,
  7. date_end: string,
  8. description: string,
  9. club: string,
  10. category_id: number,
  11. url: string,
  12. };
  13. // Regex used to check date string validity
  14. const dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
  15. /**
  16. * Checks if the given date string is in the format
  17. * YYYY-MM-DD HH:MM
  18. *
  19. * @param dateString The string to check
  20. * @return {boolean}
  21. */
  22. export function isEventDateStringFormatValid(dateString: ?string): boolean {
  23. return (
  24. dateString !== undefined &&
  25. dateString !== null &&
  26. dateRegExp.test(dateString)
  27. );
  28. }
  29. /**
  30. * Converts the given date string to a date object.<br>
  31. * Accepted format: YYYY-MM-DD HH:MM
  32. *
  33. * @param dateString The string to convert
  34. * @return {Date|null} The date object or null if the given string is invalid
  35. */
  36. export function stringToDate(dateString: string): Date | null {
  37. let date = new Date();
  38. if (isEventDateStringFormatValid(dateString)) {
  39. const stringArray = dateString.split(' ');
  40. const dateArray = stringArray[0].split('-');
  41. const timeArray = stringArray[1].split(':');
  42. date.setFullYear(
  43. parseInt(dateArray[0], 10),
  44. parseInt(dateArray[1], 10) - 1, // Month range from 0 to 11
  45. parseInt(dateArray[2], 10),
  46. );
  47. date.setHours(parseInt(timeArray[0], 10), parseInt(timeArray[1], 10), 0, 0);
  48. } else date = null;
  49. return date;
  50. }
  51. /**
  52. * Converts a date object to a string in the format
  53. * YYYY-MM-DD HH-MM
  54. *
  55. * @param date The date object to convert
  56. * @param isUTC Whether to treat the date as UTC
  57. * @return {string} The converted string
  58. */
  59. export function dateToString(date: Date, isUTC: boolean): string {
  60. const day = String(date.getDate()).padStart(2, '0');
  61. const month = String(date.getMonth() + 1).padStart(2, '0'); // January is 0!
  62. const year = date.getFullYear();
  63. const h = isUTC ? date.getUTCHours() : date.getHours();
  64. const hours = String(h).padStart(2, '0');
  65. const minutes = String(date.getMinutes()).padStart(2, '0');
  66. return `${year}-${month}-${day} ${hours}:${minutes}`;
  67. }
  68. /**
  69. * Gets the current day string representation in the format
  70. * YYYY-MM-DD
  71. *
  72. * @return {string} The string representation
  73. */
  74. export function getCurrentDateString(): string {
  75. return dateToString(new Date(Date.now()), false);
  76. }
  77. /**
  78. * Checks if the given date is before the other.
  79. *
  80. * @param event1Date Event 1 date in format YYYY-MM-DD HH:MM
  81. * @param event2Date Event 2 date in format YYYY-MM-DD HH:MM
  82. * @return {boolean}
  83. */
  84. export function isEventBefore(event1Date: string, event2Date: string): boolean {
  85. const date1 = stringToDate(event1Date);
  86. const date2 = stringToDate(event2Date);
  87. if (date1 !== null && date2 !== null) return date1 < date2;
  88. return false;
  89. }
  90. /**
  91. * Gets only the date part of the given event date string in the format
  92. * YYYY-MM-DD HH:MM
  93. *
  94. * @param dateString The string to get the date from
  95. * @return {string|null} Date in format YYYY:MM:DD or null if given string is invalid
  96. */
  97. export function getDateOnlyString(dateString: string): string | null {
  98. if (isEventDateStringFormatValid(dateString)) return dateString.split(' ')[0];
  99. return null;
  100. }
  101. /**
  102. * Gets only the time part of the given event date string in the format
  103. * YYYY-MM-DD HH:MM
  104. *
  105. * @param dateString The string to get the date from
  106. * @return {string|null} Time in format HH:MM or null if given string is invalid
  107. */
  108. export function getTimeOnlyString(dateString: string): string | null {
  109. if (isEventDateStringFormatValid(dateString)) return dateString.split(' ')[1];
  110. return null;
  111. }
  112. /**
  113. * Returns a string corresponding to the event start and end times in the following format:
  114. *
  115. * HH:MM - HH:MM
  116. *
  117. * If the end date is not specified or is equal to start time, only start time will be shown.
  118. *
  119. * If the end date is not on the same day, 23:59 will be shown as end time
  120. *
  121. * @param start Start time in YYYY-MM-DD HH:MM:SS format
  122. * @param end End time in YYYY-MM-DD HH:MM:SS format
  123. * @return {string} Formatted string or "/ - /" on error
  124. */
  125. export function getFormattedEventTime(start: string, end: string): string {
  126. let formattedStr = '/ - /';
  127. const startDate = stringToDate(start);
  128. const endDate = stringToDate(end);
  129. if (
  130. startDate !== null &&
  131. endDate !== null &&
  132. startDate.getTime() !== endDate.getTime()
  133. ) {
  134. formattedStr = `${String(startDate.getHours()).padStart(2, '0')}:${String(
  135. startDate.getMinutes(),
  136. ).padStart(2, '0')} - `;
  137. if (
  138. endDate.getFullYear() > startDate.getFullYear() ||
  139. endDate.getMonth() > startDate.getMonth() ||
  140. endDate.getDate() > startDate.getDate()
  141. )
  142. formattedStr += '23:59';
  143. else
  144. formattedStr += `${String(endDate.getHours()).padStart(2, '0')}:${String(
  145. endDate.getMinutes(),
  146. ).padStart(2, '0')}`;
  147. } else if (startDate !== null)
  148. formattedStr = `${String(startDate.getHours()).padStart(2, '0')}:${String(
  149. startDate.getMinutes(),
  150. ).padStart(2, '0')}`;
  151. return formattedStr;
  152. }
  153. /**
  154. * Checks if the given description can be considered empty.
  155. * <br>
  156. * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
  157. *
  158. *
  159. * @param description The text to check
  160. * @return {boolean}
  161. */
  162. export function isDescriptionEmpty(description: ?string): boolean {
  163. if (description !== undefined && description !== null) {
  164. return (
  165. description
  166. .split('<p>')
  167. .join('') // Equivalent to a replace all
  168. .split('</p>')
  169. .join('')
  170. .split('<br>')
  171. .join('')
  172. .trim() === ''
  173. );
  174. }
  175. return true;
  176. }
  177. /**
  178. * Generates an object with an empty array for each key.
  179. * Each key is a date string in the format
  180. * YYYY-MM-DD
  181. *
  182. * @param numberOfMonths The number of months to create, starting from the current date
  183. * @return {Object}
  184. */
  185. export function generateEmptyCalendar(
  186. numberOfMonths: number,
  187. ): {[key: string]: Array<PlanningEventType>} {
  188. const end = new Date(Date.now());
  189. end.setMonth(end.getMonth() + numberOfMonths);
  190. const daysOfYear = {};
  191. for (let d = new Date(Date.now()); d <= end; d.setDate(d.getDate() + 1)) {
  192. const dateString = getDateOnlyString(dateToString(new Date(d), false));
  193. if (dateString !== null) daysOfYear[dateString] = [];
  194. }
  195. return daysOfYear;
  196. }
  197. /**
  198. * Adds events to the given array depending on their starting date.
  199. *
  200. * Events starting before are added at the front.
  201. *
  202. * @param eventArray The array to hold sorted events
  203. * @param event The event to add to the array
  204. */
  205. export function pushEventInOrder(
  206. eventArray: Array<PlanningEventType>,
  207. event: PlanningEventType,
  208. ) {
  209. if (eventArray.length === 0) eventArray.push(event);
  210. else {
  211. for (let i = 0; i < eventArray.length; i += 1) {
  212. if (isEventBefore(event.date_begin, eventArray[i].date_begin)) {
  213. eventArray.splice(i, 0, event);
  214. break;
  215. } else if (i === eventArray.length - 1) {
  216. eventArray.push(event);
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. /**
  223. * Generates an object with an array of eventObject at each key.
  224. * Each key is a date string in the format
  225. * YYYY-MM-DD.
  226. *
  227. * If no event is available at the given key, the array will be empty
  228. *
  229. * @param eventList The list of events to map to the agenda
  230. * @param numberOfMonths The number of months to create the agenda for
  231. * @return {Object}
  232. */
  233. export function generateEventAgenda(
  234. eventList: Array<PlanningEventType>,
  235. numberOfMonths: number,
  236. ): {[key: string]: Array<PlanningEventType>} {
  237. const agendaItems = generateEmptyCalendar(numberOfMonths);
  238. for (let i = 0; i < eventList.length; i += 1) {
  239. const dateString = getDateOnlyString(eventList[i].date_begin);
  240. if (dateString != null) {
  241. const eventArray = agendaItems[dateString];
  242. if (eventArray != null) pushEventInOrder(eventArray, eventList[i]);
  243. }
  244. }
  245. return agendaItems;
  246. }