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.

CalendarListContent.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 CalendarListContent}.
  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 CalendarListContent extends Component<Props> {
  46. /**
  47. * Default values for the component's props.
  48. */
  49. static defaultProps = {
  50. _eventList: []
  51. };
  52. /**
  53. * Initializes a new {@code CalendarListContent} instance.
  54. *
  55. * @inheritdoc
  56. */
  57. constructor(props) {
  58. super(props);
  59. // Bind event handlers so they are only bound once per instance.
  60. this._onJoinPress = this._onJoinPress.bind(this);
  61. this._onPress = this._onPress.bind(this);
  62. this._onRefresh = this._onRefresh.bind(this);
  63. this._onSecondaryAction = this._onSecondaryAction.bind(this);
  64. this._toDateString = this._toDateString.bind(this);
  65. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  66. this._toDisplayableList = this._toDisplayableList.bind(this);
  67. this._toTimeString = this._toTimeString.bind(this);
  68. }
  69. /**
  70. * Implements React's {@link Component#componentDidMount()}. Invoked
  71. * immediately after this component is mounted.
  72. *
  73. * @inheritdoc
  74. * @returns {void}
  75. */
  76. componentDidMount() {
  77. sendAnalytics(createCalendarSelectedEvent());
  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. onSecondaryAction = { this._onSecondaryAction }
  92. renderListEmptyComponent
  93. = { renderListEmptyComponent }
  94. sections = { this._toDisplayableList() } />
  95. );
  96. }
  97. _onJoinPress: (Object, string) => Function;
  98. /**
  99. * Handles the list's navigate action.
  100. *
  101. * @private
  102. * @param {Object} event - The click event.
  103. * @param {string} url - The url string to navigate to.
  104. * @returns {void}
  105. */
  106. _onJoinPress(event, url) {
  107. event.stopPropagation();
  108. this._onPress(url, 'calendar.meeting.join');
  109. }
  110. _onPress: (string, string) => Function;
  111. /**
  112. * Handles the list's navigate action.
  113. *
  114. * @private
  115. * @param {string} url - The url string to navigate to.
  116. * @param {string} analyticsEventName - Тhe name of the analytics event.
  117. * associated with this action.
  118. * @returns {void}
  119. */
  120. _onPress(url, analyticsEventName = 'calendar.meeting.tile') {
  121. sendAnalytics(createCalendarClickedEvent(analyticsEventName));
  122. this.props.dispatch(appNavigate(url));
  123. }
  124. _onRefresh: () => void;
  125. /**
  126. * Callback to execute when the list is doing a pull-to-refresh.
  127. *
  128. * @private
  129. * @returns {void}
  130. */
  131. _onRefresh() {
  132. this.props.dispatch(refreshCalendar(true));
  133. }
  134. _onSecondaryAction: string => void;
  135. /**
  136. * Handles the list's secondary action.
  137. *
  138. * @private
  139. * @param {string} id - The ID of the item on which the secondary action was
  140. * performed.
  141. * @returns {void}
  142. */
  143. _onSecondaryAction(id) {
  144. this.props.dispatch(openUpdateCalendarEventDialog(id, ''));
  145. }
  146. _toDateString: Object => string;
  147. /**
  148. * Generates a date string for a given event.
  149. *
  150. * @param {Object} event - The event.
  151. * @private
  152. * @returns {string}
  153. */
  154. _toDateString(event) {
  155. const startDateTime
  156. = getLocalizedDateFormatter(event.startDate).format('MMM Do, YYYY');
  157. return `${startDateTime}`;
  158. }
  159. _toDisplayableItem: Object => Object;
  160. /**
  161. * Creates a displayable object from an event.
  162. *
  163. * @param {Object} event - The calendar event.
  164. * @private
  165. * @returns {Object}
  166. */
  167. _toDisplayableItem(event) {
  168. return {
  169. elementAfter: event.url
  170. ? <JoinButton
  171. onPress = { this._onJoinPress }
  172. url = { event.url } />
  173. : (<AddMeetingUrlButton
  174. calendarId = { event.calendarId }
  175. eventId = { event.id } />),
  176. id: event.id,
  177. key: `${event.id}-${event.startDate}`,
  178. lines: [
  179. event.url,
  180. this._toTimeString(event)
  181. ],
  182. title: event.title,
  183. url: event.url
  184. };
  185. }
  186. _toDisplayableList: () => Array<Object>;
  187. /**
  188. * Transforms the event list to a displayable list with sections.
  189. *
  190. * @private
  191. * @returns {Array<Object>}
  192. */
  193. _toDisplayableList() {
  194. const { _eventList, t } = this.props;
  195. const now = new Date();
  196. const { createSection } = NavigateSectionList;
  197. const TODAY_SECTION = 'today';
  198. const sectionMap = new Map();
  199. for (const event of _eventList) {
  200. const displayableEvent = this._toDisplayableItem(event);
  201. const startDate = new Date(event.startDate).getDate();
  202. if (startDate === now.getDate()) {
  203. let todaySection = sectionMap.get(TODAY_SECTION);
  204. if (!todaySection) {
  205. todaySection
  206. = createSection(t('calendarSync.today'), TODAY_SECTION);
  207. sectionMap.set(TODAY_SECTION, todaySection);
  208. }
  209. todaySection.data.push(displayableEvent);
  210. } else if (sectionMap.has(startDate)) {
  211. const section = sectionMap.get(startDate);
  212. if (section) {
  213. section.data.push(displayableEvent);
  214. }
  215. } else {
  216. const newSection
  217. = createSection(this._toDateString(event), startDate);
  218. sectionMap.set(startDate, newSection);
  219. newSection.data.push(displayableEvent);
  220. }
  221. }
  222. return Array.from(sectionMap.values());
  223. }
  224. _toTimeString: Object => string;
  225. /**
  226. * Generates a time (interval) string for a given event.
  227. *
  228. * @param {Object} event - The event.
  229. * @private
  230. * @returns {string}
  231. */
  232. _toTimeString(event) {
  233. const startDateTime
  234. = getLocalizedDateFormatter(event.startDate).format('lll');
  235. const endTime
  236. = getLocalizedDateFormatter(event.endDate).format('LT');
  237. return `${startDateTime} - ${endTime}`;
  238. }
  239. }
  240. /**
  241. * Maps redux state to component props.
  242. *
  243. * @param {Object} state - The redux state.
  244. * @returns {{
  245. * _eventList: Array<Object>
  246. * }}
  247. */
  248. function _mapStateToProps(state: Object) {
  249. return {
  250. _eventList: state['features/calendar-sync'].events
  251. };
  252. }
  253. export default isCalendarEnabled()
  254. ? translate(connect(_mapStateToProps)(CalendarListContent))
  255. : undefined;