您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CalendarListContent.native.js 6.9KB

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