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.

AbstractCalendarList.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { appNavigate } from '../../app';
  5. import { getLocalizedDateFormatter, translate } from '../../base/i18n';
  6. import { NavigateSectionList } from '../../base/react';
  7. import { refreshCalendar } from '../actions';
  8. import { isCalendarEnabled } from '../functions';
  9. import AddMeetingUrlButton from './AddMeetingUrlButton';
  10. /**
  11. * The type of the React {@code Component} props of
  12. * {@link AbstractCalendarList}.
  13. */
  14. type Props = {
  15. /**
  16. * The calendar event list.
  17. */
  18. _eventList: Array<Object>,
  19. /**
  20. * Indicates if the list is disabled or not.
  21. */
  22. disabled: boolean,
  23. /**
  24. * The Redux dispatch function.
  25. */
  26. dispatch: Function,
  27. /**
  28. *
  29. */
  30. renderListEmptyComponent: Function,
  31. /**
  32. * The translate function.
  33. */
  34. t: Function
  35. };
  36. /**
  37. * Component to display a list of events from a connected calendar.
  38. */
  39. class AbstractCalendarList extends Component<Props> {
  40. /**
  41. * Default values for the component's props.
  42. */
  43. static defaultProps = {
  44. _eventList: []
  45. };
  46. /**
  47. * Public API method for {@code Component}s rendered in
  48. * {@link AbstractPagedList}. When invoked, refreshes the calendar entries
  49. * in the app.
  50. *
  51. * Note: It is a static method as the {@code Component} may not be
  52. * initialized yet when the UI invokes refresh (e.g. {@link TabBarIOS} tab
  53. * change).
  54. *
  55. * @param {Function} dispatch - The Redux dispatch function.
  56. * @param {boolean} isInteractive - If true this refresh was caused by
  57. * direct user interaction, false otherwise.
  58. * @public
  59. * @returns {void}
  60. */
  61. static refresh(dispatch, isInteractive) {
  62. dispatch(refreshCalendar(false, isInteractive));
  63. }
  64. /**
  65. * Initializes a new {@code CalendarList} instance.
  66. *
  67. * @inheritdoc
  68. */
  69. constructor(props) {
  70. super(props);
  71. // Bind event handlers so they are only bound once per instance.
  72. this._onPress = this._onPress.bind(this);
  73. this._onRefresh = this._onRefresh.bind(this);
  74. this._toDateString = this._toDateString.bind(this);
  75. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  76. this._toDisplayableList = this._toDisplayableList.bind(this);
  77. this._toTimeString = this._toTimeString.bind(this);
  78. }
  79. /**
  80. * Implements React's {@link Component#render}.
  81. *
  82. * @inheritdoc
  83. */
  84. render() {
  85. const { disabled, renderListEmptyComponent } = this.props;
  86. return (
  87. <NavigateSectionList
  88. disabled = { disabled }
  89. onPress = { this._onPress }
  90. onRefresh = { this._onRefresh }
  91. renderListEmptyComponent
  92. = { renderListEmptyComponent }
  93. sections = { this._toDisplayableList() } />
  94. );
  95. }
  96. _onPress: string => Function;
  97. /**
  98. * Handles the list's navigate action.
  99. *
  100. * @private
  101. * @param {string} url - The url string to navigate to.
  102. * @returns {void}
  103. */
  104. _onPress(url) {
  105. this.props.dispatch(appNavigate(url));
  106. }
  107. _onRefresh: () => void;
  108. /**
  109. * Callback to execute when the list is doing a pull-to-refresh.
  110. *
  111. * @private
  112. * @returns {void}
  113. */
  114. _onRefresh() {
  115. this.props.dispatch(refreshCalendar(true));
  116. }
  117. _toDateString: Object => string;
  118. /**
  119. * Generates a date string for a given event.
  120. *
  121. * @param {Object} event - The event.
  122. * @private
  123. * @returns {string}
  124. */
  125. _toDateString(event) {
  126. const startDateTime
  127. = getLocalizedDateFormatter(event.startDate).format('MMM Do, YYYY');
  128. return `${startDateTime}`;
  129. }
  130. _toDisplayableItem: Object => Object;
  131. /**
  132. * Creates a displayable object from an event.
  133. *
  134. * @param {Object} event - The calendar event.
  135. * @private
  136. * @returns {Object}
  137. */
  138. _toDisplayableItem(event) {
  139. return {
  140. elementAfter: event.url ? undefined : (
  141. <AddMeetingUrlButton
  142. calendarId = { event.calendarId }
  143. eventId = { event.id } />
  144. ),
  145. key: `${event.id}-${event.startDate}`,
  146. lines: [
  147. event.url,
  148. this._toTimeString(event)
  149. ],
  150. title: event.title,
  151. url: event.url
  152. };
  153. }
  154. _toDisplayableList: () => Array<Object>;
  155. /**
  156. * Transforms the event list to a displayable list with sections.
  157. *
  158. * @private
  159. * @returns {Array<Object>}
  160. */
  161. _toDisplayableList() {
  162. const { _eventList, t } = this.props;
  163. const now = new Date();
  164. const { createSection } = NavigateSectionList;
  165. const TODAY_SECTION = 'today';
  166. const sectionMap = new Map();
  167. for (const event of _eventList) {
  168. const displayableEvent = this._toDisplayableItem(event);
  169. const startDate = new Date(event.startDate).getDate();
  170. if (startDate === now.getDate()) {
  171. let todaySection = sectionMap.get(TODAY_SECTION);
  172. if (!todaySection) {
  173. todaySection
  174. = createSection(t('calendarSync.today'), TODAY_SECTION);
  175. sectionMap.set(TODAY_SECTION, todaySection);
  176. }
  177. todaySection.data.push(displayableEvent);
  178. } else if (sectionMap.has(startDate)) {
  179. const section = sectionMap.get(startDate);
  180. if (section) {
  181. section.data.push(displayableEvent);
  182. }
  183. } else {
  184. const newSection
  185. = createSection(this._toDateString(event), startDate);
  186. sectionMap.set(startDate, newSection);
  187. newSection.data.push(displayableEvent);
  188. }
  189. }
  190. return Array.from(sectionMap.values());
  191. }
  192. _toTimeString: Object => string;
  193. /**
  194. * Generates a time (interval) string for a given event.
  195. *
  196. * @param {Object} event - The event.
  197. * @private
  198. * @returns {string}
  199. */
  200. _toTimeString(event) {
  201. const startDateTime
  202. = getLocalizedDateFormatter(event.startDate).format('lll');
  203. const endTime
  204. = getLocalizedDateFormatter(event.endDate).format('LT');
  205. return `${startDateTime} - ${endTime}`;
  206. }
  207. }
  208. /**
  209. * Maps redux state to component props.
  210. *
  211. * @param {Object} state - The redux state.
  212. * @returns {{
  213. * _eventList: Array<Object>
  214. * }}
  215. */
  216. function _mapStateToProps(state: Object) {
  217. return {
  218. _eventList: state['features/calendar-sync'].events
  219. };
  220. }
  221. export default isCalendarEnabled()
  222. ? translate(connect(_mapStateToProps)(AbstractCalendarList))
  223. : undefined;