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.

BaseCalendarList.js 8.5KB

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