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

MeetingList.native.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { refreshCalendarEntryList } from '../actions';
  5. import { appNavigate } from '../../app';
  6. import { getLocalizedDateFormatter, translate } from '../../base/i18n';
  7. import { NavigateSectionList } from '../../base/react';
  8. type Props = {
  9. /**
  10. * Indicates if the list is disabled or not.
  11. */
  12. disabled: boolean,
  13. /**
  14. * The Redux dispatch function.
  15. */
  16. dispatch: Function,
  17. /**
  18. * Tells the component if it's being displayed at the moment, or not.
  19. * Note: as an example, on Android it can happen that the component
  20. * is rendered but not displayed, because components like ViewPagerAndroid
  21. * render their children even if they are not visible at the moment.
  22. */
  23. displayed: boolean,
  24. /**
  25. * The calendar event list.
  26. */
  27. _eventList: Array<Object>,
  28. /**
  29. * The translate function.
  30. */
  31. t: Function
  32. };
  33. /**
  34. * Component to display a list of events from the (mobile) user's calendar.
  35. */
  36. class MeetingList extends Component<Props> {
  37. _initialLoaded: boolean
  38. /**
  39. * Default values for the component's props.
  40. */
  41. static defaultProps = {
  42. _eventList: []
  43. };
  44. /**
  45. * Constructor of the MeetingList component.
  46. *
  47. * @inheritdoc
  48. */
  49. constructor(props) {
  50. super(props);
  51. this._onPress = this._onPress.bind(this);
  52. this._onRefresh = this._onRefresh.bind(this);
  53. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  54. this._toDisplayableList = this._toDisplayableList.bind(this);
  55. this._toDateString = this._toDateString.bind(this);
  56. }
  57. /**
  58. * Implements React Component's componentWillReceiveProps function.
  59. *
  60. * @inheritdoc
  61. */
  62. componentWillReceiveProps(newProps) {
  63. // This is a conditional logic to refresh the calendar entries (thus
  64. // to request access to calendar) on component first receives a
  65. // displayed=true prop - to avoid requesting calendar access on
  66. // app start.
  67. if (!this._initialLoaded
  68. && newProps.displayed
  69. && !this.props.displayed) {
  70. const { dispatch } = this.props;
  71. this._initialLoaded = true;
  72. dispatch(refreshCalendarEntryList());
  73. }
  74. }
  75. /**
  76. * Implements the React Components's render method.
  77. *
  78. * @inheritdoc
  79. */
  80. render() {
  81. const { disabled } = this.props;
  82. return (
  83. <NavigateSectionList
  84. disabled = { disabled }
  85. onPress = { this._onPress }
  86. onRefresh = { this._onRefresh }
  87. sections = { this._toDisplayableList() } />
  88. );
  89. }
  90. _onPress: string => Function
  91. /**
  92. * Handles the list's navigate action.
  93. *
  94. * @private
  95. * @param {string} url - The url string to navigate to.
  96. * @returns {void}
  97. */
  98. _onPress(url) {
  99. const { dispatch } = this.props;
  100. dispatch(appNavigate(url));
  101. }
  102. _onRefresh: () => void
  103. /**
  104. * Callback to execute when the list is doing a pull-to-refresh.
  105. *
  106. * @private
  107. * @returns {void}
  108. */
  109. _onRefresh() {
  110. const { dispatch } = this.props;
  111. dispatch(refreshCalendarEntryList());
  112. }
  113. _toDisplayableItem: Object => Object
  114. /**
  115. * Creates a displayable object from an event.
  116. *
  117. * @private
  118. * @param {Object} event - The calendar event.
  119. * @returns {Object}
  120. */
  121. _toDisplayableItem(event) {
  122. return {
  123. key: `${event.id}-${event.startDate}`,
  124. lines: [
  125. event.url,
  126. this._toDateString(event)
  127. ],
  128. title: event.title,
  129. url: event.url
  130. };
  131. }
  132. _toDisplayableList: () => Array<Object>
  133. /**
  134. * Transforms the event list to a displayable list
  135. * with sections.
  136. *
  137. * @private
  138. * @returns {Array<Object>}
  139. */
  140. _toDisplayableList() {
  141. const { _eventList, t } = this.props;
  142. const now = Date.now();
  143. const nowSection = NavigateSectionList.createSection(
  144. t('calendarSync.now'),
  145. 'now'
  146. );
  147. const nextSection = NavigateSectionList.createSection(
  148. t('calendarSync.next'),
  149. 'next'
  150. );
  151. const laterSection = NavigateSectionList.createSection(
  152. t('calendarSync.later'),
  153. 'later'
  154. );
  155. for (const event of _eventList) {
  156. const displayableEvent = this._toDisplayableItem(event);
  157. if (event.startDate < now && event.endDate > now) {
  158. nowSection.data.push(displayableEvent);
  159. } else if (event.startDate > now) {
  160. if (nextSection.data.length
  161. && nextSection.data[0].startDate !== event.startDate) {
  162. laterSection.data.push(displayableEvent);
  163. } else {
  164. nextSection.data.push(displayableEvent);
  165. }
  166. }
  167. }
  168. const sectionList = [];
  169. for (const section of [
  170. nowSection,
  171. nextSection,
  172. laterSection
  173. ]) {
  174. if (section.data.length) {
  175. sectionList.push(section);
  176. }
  177. }
  178. return sectionList;
  179. }
  180. _toDateString: Object => string
  181. /**
  182. * Generates a date (interval) string for a given event.
  183. *
  184. * @private
  185. * @param {Object} event - The event.
  186. * @returns {string}
  187. */
  188. _toDateString(event) {
  189. /* eslint-disable max-len */
  190. const startDateTime = getLocalizedDateFormatter(event.startDate).format('lll');
  191. const endTime = getLocalizedDateFormatter(event.endDate).format('LT');
  192. return `${startDateTime} - ${endTime}`;
  193. /* eslint-enable max-len */
  194. }
  195. }
  196. /**
  197. * Maps redux state to component props.
  198. *
  199. * @param {Object} state - The redux state.
  200. * @returns {{
  201. * _eventList: Array
  202. * }}
  203. */
  204. export function _mapStateToProps(state: Object) {
  205. return {
  206. _eventList: state['features/calendar-sync'].events
  207. };
  208. }
  209. export default translate(connect(_mapStateToProps)(MeetingList));