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.6KB

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