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

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