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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // @flow
  2. export type PlanningEventType = {
  3. id: number,
  4. title: string,
  5. date_begin: string,
  6. club: string,
  7. category_id: number,
  8. description: string,
  9. place: string,
  10. url: string,
  11. logo: string | null,
  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. * Checks if the given description can be considered empty.
  114. * <br>
  115. * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
  116. *
  117. *
  118. * @param description The text to check
  119. * @return {boolean}
  120. */
  121. export function isDescriptionEmpty(description: ?string): boolean {
  122. if (description !== undefined && description !== null) {
  123. return (
  124. description
  125. .split('<p>')
  126. .join('') // Equivalent to a replace all
  127. .split('</p>')
  128. .join('')
  129. .split('<br>')
  130. .join('')
  131. .trim() === ''
  132. );
  133. }
  134. return true;
  135. }
  136. /**
  137. * Generates an object with an empty array for each key.
  138. * Each key is a date string in the format
  139. * YYYY-MM-DD
  140. *
  141. * @param numberOfMonths The number of months to create, starting from the current date
  142. * @return {Object}
  143. */
  144. export function generateEmptyCalendar(
  145. numberOfMonths: number,
  146. ): {[key: string]: Array<PlanningEventType>} {
  147. const end = new Date(Date.now());
  148. end.setMonth(end.getMonth() + numberOfMonths);
  149. const daysOfYear = {};
  150. for (let d = new Date(Date.now()); d <= end; d.setDate(d.getDate() + 1)) {
  151. const dateString = getDateOnlyString(dateToString(new Date(d), false));
  152. if (dateString !== null) daysOfYear[dateString] = [];
  153. }
  154. return daysOfYear;
  155. }
  156. /**
  157. * Adds events to the given array depending on their starting date.
  158. *
  159. * Events starting before are added at the front.
  160. *
  161. * @param eventArray The array to hold sorted events
  162. * @param event The event to add to the array
  163. */
  164. export function pushEventInOrder(
  165. eventArray: Array<PlanningEventType>,
  166. event: PlanningEventType,
  167. ) {
  168. if (eventArray.length === 0) eventArray.push(event);
  169. else {
  170. for (let i = 0; i < eventArray.length; i += 1) {
  171. if (isEventBefore(event.date_begin, eventArray[i].date_begin)) {
  172. eventArray.splice(i, 0, event);
  173. break;
  174. } else if (i === eventArray.length - 1) {
  175. eventArray.push(event);
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. /**
  182. * Generates an object with an array of eventObject at each key.
  183. * Each key is a date string in the format
  184. * YYYY-MM-DD.
  185. *
  186. * If no event is available at the given key, the array will be empty
  187. *
  188. * @param eventList The list of events to map to the agenda
  189. * @param numberOfMonths The number of months to create the agenda for
  190. * @return {Object}
  191. */
  192. export function generateEventAgenda(
  193. eventList: Array<PlanningEventType>,
  194. numberOfMonths: number,
  195. ): {[key: string]: Array<PlanningEventType>} {
  196. const agendaItems = generateEmptyCalendar(numberOfMonths);
  197. for (let i = 0; i < eventList.length; i += 1) {
  198. const dateString = getDateOnlyString(eventList[i].date_begin);
  199. if (dateString != null) {
  200. const eventArray = agendaItems[dateString];
  201. if (eventArray != null) pushEventInOrder(eventArray, eventList[i]);
  202. }
  203. }
  204. return agendaItems;
  205. }