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.

EquipmentBooking.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // @flow
  2. import type {Device} from "../screens/Amicale/Equipment/EquipmentListScreen";
  3. import i18n from "i18n-js";
  4. import DateManager from "../managers/DateManager";
  5. import type {CustomTheme} from "../managers/ThemeManager";
  6. /**
  7. * Gets the current day at midnight
  8. *
  9. * @returns {Date}
  10. */
  11. export function getCurrentDay() {
  12. let today = new Date(Date.now());
  13. today.setUTCHours(0, 0, 0, 0);
  14. return today;
  15. }
  16. /**
  17. * Returns the ISO date format (without the time)
  18. *
  19. * @param date The date to recover the ISO format from
  20. * @returns {*}
  21. */
  22. export function getISODate(date: Date) {
  23. return date.toISOString().split("T")[0];
  24. }
  25. /**
  26. * Finds if the given equipment is available today
  27. *
  28. * @param item
  29. * @returns {boolean}
  30. */
  31. export function isEquipmentAvailable(item: Device) {
  32. let isAvailable = true;
  33. const today = getCurrentDay();
  34. const dates = item.booked_at;
  35. for (let i = 0; i < dates.length; i++) {
  36. const start = new Date(dates[i].begin);
  37. const end = new Date(dates[i].end);
  38. isAvailable = today < start || today > end;
  39. if (!isAvailable)
  40. break;
  41. }
  42. return isAvailable;
  43. }
  44. /**
  45. * Finds the first date free for booking.
  46. *
  47. * @param item
  48. * @returns {Date}
  49. */
  50. export function getFirstEquipmentAvailability(item: Device) {
  51. let firstAvailability = getCurrentDay();
  52. const dates = item.booked_at;
  53. for (let i = 0; i < dates.length; i++) {
  54. const start = new Date(dates[i].begin);
  55. let end = new Date(dates[i].end);
  56. end.setDate(end.getDate() + 1);
  57. if (firstAvailability >= start)
  58. firstAvailability = end;
  59. }
  60. return firstAvailability;
  61. }
  62. /**
  63. * Gets a translated string representing the given date, relative to the current date
  64. *
  65. * @param date The date to translate
  66. */
  67. export function getRelativeDateString(date: Date) {
  68. const today = getCurrentDay();
  69. const yearDelta = date.getUTCFullYear() - today.getUTCFullYear();
  70. const monthDelta = date.getUTCMonth() - today.getUTCMonth();
  71. const dayDelta = date.getUTCDate() - today.getUTCDate();
  72. let translatedString = i18n.t('equipmentScreen.today');
  73. if (yearDelta > 0)
  74. translatedString = i18n.t('equipmentScreen.otherYear', {
  75. date: date.getDate(),
  76. month: DateManager.getInstance().getMonthsOfYear()[date.getMonth()],
  77. year: date.getFullYear()
  78. });
  79. else if (monthDelta > 0)
  80. translatedString = i18n.t('equipmentScreen.otherMonth', {
  81. date: date.getDate(),
  82. month: DateManager.getInstance().getMonthsOfYear()[date.getMonth()],
  83. });
  84. else if (dayDelta > 1)
  85. translatedString = i18n.t('equipmentScreen.thisMonth', {
  86. date: date.getDate(),
  87. });
  88. else if (dayDelta === 1)
  89. translatedString = i18n.t('equipmentScreen.tomorrow');
  90. return translatedString;
  91. }
  92. /**
  93. * Gets a valid array of dates between the given start and end, for the corresponding item.
  94. * I stops at the first booked date encountered before the end.
  95. * It assumes the range start and end are valid.
  96. *
  97. * Start and End specify the range's direction.
  98. * If start < end, it will begin at Start and stop if it encounters any booked date before reaching End.
  99. * If start > end, it will begin at End and stop if it encounters any booked dates before reaching Start.
  100. *
  101. * @param start Range start
  102. * @param end Range end
  103. * @param item Item containing booked dates to look for
  104. * @returns {[string]}
  105. */
  106. export function getValidRange(start: Date, end: Date, item: Device | null) {
  107. let direction = start <= end ? 1 : -1;
  108. let limit = new Date(end);
  109. limit.setDate(limit.getDate() + direction); // Limit is excluded, but we want to include range end
  110. if (item != null) {
  111. if (direction === 1) {
  112. for (let i = 0; i < item.booked_at.length; i++) {
  113. const bookLimit = new Date(item.booked_at[i].begin);
  114. if (start < bookLimit && limit > bookLimit) {
  115. limit = bookLimit;
  116. break;
  117. }
  118. }
  119. } else {
  120. for (let i = item.booked_at.length - 1; i >= 0; i--) {
  121. const bookLimit = new Date(item.booked_at[i].end);
  122. if (start > bookLimit && limit < bookLimit) {
  123. limit = bookLimit;
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. let validRange = [];
  130. let date = new Date(start);
  131. while ((direction === 1 && date < limit) || (direction === -1 && date > limit)) {
  132. if (direction === 1)
  133. validRange.push(getISODate(date));
  134. else
  135. validRange.unshift(getISODate(date));
  136. date.setDate(date.getDate() + direction);
  137. }
  138. return validRange;
  139. }
  140. /**
  141. * Generates calendar compatible marked dates from the given array
  142. *
  143. *
  144. * @param isSelection True to use user selection color, false to use disabled color
  145. * @param theme The current App theme to get colors from
  146. * @param range The range to mark dates for
  147. * @returns {{}}
  148. */
  149. export function generateMarkedDates(isSelection: boolean, theme: CustomTheme, range: Array<string>) {
  150. let markedDates = {}
  151. for (let i = 0; i < range.length; i++) {
  152. const isStart = i === 0;
  153. const isEnd = i === range.length - 1;
  154. markedDates[range[i]] = {
  155. startingDay: isStart,
  156. endingDay: isEnd,
  157. color: isSelection
  158. ? isStart || isEnd
  159. ? theme.colors.primary
  160. : theme.colors.danger
  161. : theme.colors.textDisabled
  162. };
  163. }
  164. return markedDates;
  165. }