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.

MeetingList.native.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Text, TouchableOpacity, View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { appNavigate } from '../../app';
  6. import { getLocalizedDateFormatter, translate } from '../../base/i18n';
  7. import { NavigateSectionList } from '../../base/react';
  8. import { openSettings } from '../../mobile/permissions';
  9. import { refreshCalendar } from '../actions';
  10. import { CALENDAR_ENABLED } from '../constants';
  11. import styles from './styles';
  12. /**
  13. * The tyoe of the React {@code Component} props of {@link MeetingList}.
  14. */
  15. type Props = {
  16. /**
  17. * The current state of the calendar access permission.
  18. */
  19. _authorization: string,
  20. /**
  21. * The calendar event list.
  22. */
  23. _eventList: Array<Object>,
  24. /**
  25. * Indicates if the list is disabled or not.
  26. */
  27. disabled: boolean,
  28. /**
  29. * The Redux dispatch function.
  30. */
  31. dispatch: Function,
  32. /**
  33. * The translate function.
  34. */
  35. t: Function
  36. };
  37. /**
  38. * Component to display a list of events from the (mobile) user's calendar.
  39. */
  40. class MeetingList extends Component<Props> {
  41. /**
  42. * Default values for the component's props.
  43. */
  44. static defaultProps = {
  45. _eventList: []
  46. };
  47. /**
  48. * Public API method for {@code Component}s rendered in
  49. * {@link AbstractPagedList}. When invoked, refreshes the calendar entries
  50. * in the app.
  51. *
  52. * Note: It is a static method as the {@code Component} may not be
  53. * initialized yet when the UI invokes refresh (e.g. {@link TabBarIOS} tab
  54. * change).
  55. *
  56. * @param {Function} dispatch - The Redux dispatch function.
  57. * @public
  58. * @returns {void}
  59. */
  60. static refresh(dispatch) {
  61. dispatch(refreshCalendar());
  62. }
  63. /**
  64. * Constructor of the MeetingList component.
  65. *
  66. * @inheritdoc
  67. */
  68. constructor(props) {
  69. super(props);
  70. // Bind event handlers so they are only bound once per instance.
  71. this._getRenderListEmptyComponent
  72. = this._getRenderListEmptyComponent.bind(this);
  73. this._onPress = this._onPress.bind(this);
  74. this._onRefresh = this._onRefresh.bind(this);
  75. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  76. this._toDisplayableList = this._toDisplayableList.bind(this);
  77. this._toDateString = this._toDateString.bind(this);
  78. }
  79. /**
  80. * Implements the React Components's render.
  81. *
  82. * @inheritdoc
  83. */
  84. render() {
  85. const { disabled } = this.props;
  86. return (
  87. <NavigateSectionList
  88. disabled = { disabled }
  89. onPress = { this._onPress }
  90. onRefresh = { this._onRefresh }
  91. renderListEmptyComponent = { this._getRenderListEmptyComponent }
  92. sections = { this._toDisplayableList() } />
  93. );
  94. }
  95. _getRenderListEmptyComponent: () => Object;
  96. /**
  97. * Returns a list empty component if a custom one has to be rendered instead
  98. * of the default one in the {@link NavigateSectionList}.
  99. *
  100. * @private
  101. * @returns {Component}
  102. */
  103. _getRenderListEmptyComponent() {
  104. const { _authorization, t } = this.props;
  105. if (_authorization === 'denied') {
  106. return (
  107. <View style = { styles.noPermissionMessageView }>
  108. <Text style = { styles.noPermissionMessageText }>
  109. { t('calendarSync.permissionMessage') }
  110. </Text>
  111. <TouchableOpacity
  112. onPress = { openSettings }
  113. style = { styles.noPermissionMessageButton } >
  114. <Text style = { styles.noPermissionMessageButtonText }>
  115. { t('calendarSync.permissionButton') }
  116. </Text>
  117. </TouchableOpacity>
  118. </View>
  119. );
  120. }
  121. return null;
  122. }
  123. _onPress: string => Function;
  124. /**
  125. * Handles the list's navigate action.
  126. *
  127. * @private
  128. * @param {string} url - The url string to navigate to.
  129. * @returns {void}
  130. */
  131. _onPress(url) {
  132. this.props.dispatch(appNavigate(url));
  133. }
  134. _onRefresh: () => void;
  135. /**
  136. * Callback to execute when the list is doing a pull-to-refresh.
  137. *
  138. * @private
  139. * @returns {void}
  140. */
  141. _onRefresh() {
  142. this.props.dispatch(refreshCalendar(true));
  143. }
  144. _toDisplayableItem: Object => Object;
  145. /**
  146. * Creates a displayable object from an event.
  147. *
  148. * @param {Object} event - The calendar event.
  149. * @private
  150. * @returns {Object}
  151. */
  152. _toDisplayableItem(event) {
  153. return {
  154. key: `${event.id}-${event.startDate}`,
  155. lines: [
  156. event.url,
  157. this._toDateString(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 = Date.now();
  173. const { createSection } = NavigateSectionList;
  174. const nowSection = createSection(t('calendarSync.now'), 'now');
  175. const nextSection = createSection(t('calendarSync.next'), 'next');
  176. const laterSection = createSection(t('calendarSync.later'), 'later');
  177. for (const event of _eventList) {
  178. const displayableEvent = this._toDisplayableItem(event);
  179. if (event.startDate < now && event.endDate > now) {
  180. nowSection.data.push(displayableEvent);
  181. } else if (event.startDate > now) {
  182. if (nextSection.data.length
  183. && nextSection.data[0].startDate !== event.startDate) {
  184. laterSection.data.push(displayableEvent);
  185. } else {
  186. nextSection.data.push(displayableEvent);
  187. }
  188. }
  189. }
  190. const sectionList = [];
  191. for (const section of [
  192. nowSection,
  193. nextSection,
  194. laterSection
  195. ]) {
  196. if (section.data.length) {
  197. sectionList.push(section);
  198. }
  199. }
  200. return sectionList;
  201. }
  202. _toDateString: Object => string;
  203. /**
  204. * Generates a date (interval) string for a given event.
  205. *
  206. * @param {Object} event - The event.
  207. * @private
  208. * @returns {string}
  209. */
  210. _toDateString(event) {
  211. const startDateTime
  212. = getLocalizedDateFormatter(event.startDate).format('lll');
  213. const endTime
  214. = getLocalizedDateFormatter(event.endDate).format('LT');
  215. return `${startDateTime} - ${endTime}`;
  216. }
  217. }
  218. /**
  219. * Maps redux state to component props.
  220. *
  221. * @param {Object} state - The redux state.
  222. * @returns {{
  223. * _eventList: Array
  224. * }}
  225. */
  226. function _mapStateToProps(state: Object) {
  227. const calendarSyncState = state['features/calendar-sync'];
  228. return {
  229. _authorization: calendarSyncState.authorization,
  230. _eventList: calendarSyncState.events
  231. };
  232. }
  233. export default CALENDAR_ENABLED
  234. ? translate(connect(_mapStateToProps)(MeetingList))
  235. : undefined;