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.native.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /**
  15. * The type of the React {@code Component} props of
  16. * {@link CalendarListContent}.
  17. */
  18. type Props = {
  19. /**
  20. * The calendar event list.
  21. */
  22. _eventList: Array<Object>,
  23. /**
  24. * Indicates if the list is disabled or not.
  25. */
  26. disabled: boolean,
  27. /**
  28. * The Redux dispatch function.
  29. */
  30. dispatch: Function,
  31. /**
  32. *
  33. */
  34. listEmptyComponent: React$Node,
  35. /**
  36. * The translate function.
  37. */
  38. t: Function
  39. };
  40. /**
  41. * Component to display a list of events from a connected calendar.
  42. */
  43. class CalendarListContent extends Component<Props> {
  44. /**
  45. * Default values for the component's props.
  46. */
  47. static defaultProps = {
  48. _eventList: []
  49. };
  50. /**
  51. * Initializes a new {@code CalendarListContent} instance.
  52. *
  53. * @inheritdoc
  54. */
  55. constructor(props) {
  56. super(props);
  57. // Bind event handlers so they are only bound once per instance.
  58. this._onPress = this._onPress.bind(this);
  59. this._onRefresh = this._onRefresh.bind(this);
  60. this._onSecondaryAction = this._onSecondaryAction.bind(this);
  61. this._toDateString = this._toDateString.bind(this);
  62. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  63. this._toDisplayableList = this._toDisplayableList.bind(this);
  64. this._toTimeString = this._toTimeString.bind(this);
  65. }
  66. /**
  67. * Implements React's {@link Component#componentDidMount()}. Invoked
  68. * immediately after this component is mounted.
  69. *
  70. * @inheritdoc
  71. * @returns {void}
  72. */
  73. componentDidMount() {
  74. sendAnalytics(createCalendarSelectedEvent());
  75. }
  76. /**
  77. * Implements React's {@link Component#render}.
  78. *
  79. * @inheritdoc
  80. */
  81. render() {
  82. const { disabled, listEmptyComponent } = this.props;
  83. return (
  84. <NavigateSectionList
  85. disabled = { disabled }
  86. onPress = { this._onPress }
  87. onRefresh = { this._onRefresh }
  88. onSecondaryAction = { this._onSecondaryAction }
  89. renderListEmptyComponent
  90. = { listEmptyComponent }
  91. sections = { this._toDisplayableList() } />
  92. );
  93. }
  94. _onPress: (string, string) => Function;
  95. /**
  96. * Handles the list's navigate action.
  97. *
  98. * @private
  99. * @param {string} url - The url string to navigate to.
  100. * @param {string} analyticsEventName - Тhe name of the analytics event.
  101. * associated with this action.
  102. * @returns {void}
  103. */
  104. _onPress(url, analyticsEventName = 'calendar.meeting.tile') {
  105. sendAnalytics(createCalendarClickedEvent(analyticsEventName));
  106. this.props.dispatch(appNavigate(url));
  107. }
  108. _onRefresh: () => void;
  109. /**
  110. * Callback to execute when the list is doing a pull-to-refresh.
  111. *
  112. * @private
  113. * @returns {void}
  114. */
  115. _onRefresh() {
  116. this.props.dispatch(refreshCalendar(true));
  117. }
  118. _onSecondaryAction: string => void;
  119. /**
  120. * Handles the list's secondary action.
  121. *
  122. * @private
  123. * @param {string} id - The ID of the item on which the secondary action was
  124. * performed.
  125. * @returns {void}
  126. */
  127. _onSecondaryAction(id) {
  128. this.props.dispatch(openUpdateCalendarEventDialog(id, ''));
  129. }
  130. _toDateString: Object => string;
  131. /**
  132. * Generates a date string for a given event.
  133. *
  134. * @param {Object} event - The event.
  135. * @private
  136. * @returns {string}
  137. */
  138. _toDateString(event) {
  139. const startDateTime
  140. = getLocalizedDateFormatter(event.startDate).format('MMM Do, YYYY');
  141. return `${startDateTime}`;
  142. }
  143. _toDisplayableItem: Object => Object;
  144. /**
  145. * Creates a displayable object from an event.
  146. *
  147. * @param {Object} event - The calendar event.
  148. * @private
  149. * @returns {Object}
  150. */
  151. _toDisplayableItem(event) {
  152. return {
  153. id: event.id,
  154. key: `${event.id}-${event.startDate}`,
  155. lines: [
  156. event.url,
  157. this._toTimeString(event)
  158. ],
  159. title: event.title,
  160. url: event.url
  161. };
  162. }
  163. _toDisplayableList: () => Array<Object>;
  164. /**
  165. * Transforms the event list to a displayable list with sections.
  166. *
  167. * @private
  168. * @returns {Array<Object>}
  169. */
  170. _toDisplayableList() {
  171. const { _eventList, t } = this.props;
  172. const now = new Date();
  173. const { createSection } = NavigateSectionList;
  174. const TODAY_SECTION = 'today';
  175. const sectionMap = new Map();
  176. for (const event of _eventList) {
  177. const displayableEvent = this._toDisplayableItem(event);
  178. const startDate = new Date(event.startDate).getDate();
  179. if (startDate === now.getDate()) {
  180. let todaySection = sectionMap.get(TODAY_SECTION);
  181. if (!todaySection) {
  182. todaySection
  183. = createSection(t('calendarSync.today'), TODAY_SECTION);
  184. sectionMap.set(TODAY_SECTION, todaySection);
  185. }
  186. todaySection.data.push(displayableEvent);
  187. } else if (sectionMap.has(startDate)) {
  188. const section = sectionMap.get(startDate);
  189. if (section) {
  190. section.data.push(displayableEvent);
  191. }
  192. } else {
  193. const newSection
  194. = createSection(this._toDateString(event), startDate);
  195. sectionMap.set(startDate, newSection);
  196. newSection.data.push(displayableEvent);
  197. }
  198. }
  199. return Array.from(sectionMap.values());
  200. }
  201. _toTimeString: Object => string;
  202. /**
  203. * Generates a time (interval) string for a given event.
  204. *
  205. * @param {Object} event - The event.
  206. * @private
  207. * @returns {string}
  208. */
  209. _toTimeString(event) {
  210. const startDateTime
  211. = getLocalizedDateFormatter(event.startDate).format('lll');
  212. const endTime
  213. = getLocalizedDateFormatter(event.endDate).format('LT');
  214. return `${startDateTime} - ${endTime}`;
  215. }
  216. }
  217. /**
  218. * Maps redux state to component props.
  219. *
  220. * @param {Object} state - The redux state.
  221. * @returns {{
  222. * _eventList: Array<Object>
  223. * }}
  224. */
  225. function _mapStateToProps(state: Object) {
  226. return {
  227. _eventList: state['features/calendar-sync'].events
  228. };
  229. }
  230. export default isCalendarEnabled()
  231. ? translate(connect(_mapStateToProps)(CalendarListContent))
  232. : undefined;