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.4KB

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