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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * @param {boolean} isInteractive - If true this refresh was caused by
  58. * direct user interaction, false otherwise.
  59. * @public
  60. * @returns {void}
  61. */
  62. static refresh(dispatch, isInteractive) {
  63. dispatch(refreshCalendar(false, isInteractive));
  64. }
  65. /**
  66. * Initializes a new {@code MeetingList} instance.
  67. *
  68. * @inheritdoc
  69. */
  70. constructor(props) {
  71. super(props);
  72. // Bind event handlers so they are only bound once per instance.
  73. this._getRenderListEmptyComponent
  74. = this._getRenderListEmptyComponent.bind(this);
  75. this._onPress = this._onPress.bind(this);
  76. this._onRefresh = this._onRefresh.bind(this);
  77. this._toDateString = this._toDateString.bind(this);
  78. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  79. this._toDisplayableList = this._toDisplayableList.bind(this);
  80. }
  81. /**
  82. * Implements React's {@link Component#render}.
  83. *
  84. * @inheritdoc
  85. */
  86. render() {
  87. const { disabled } = this.props;
  88. return (
  89. <NavigateSectionList
  90. disabled = { disabled }
  91. onPress = { this._onPress }
  92. onRefresh = { this._onRefresh }
  93. renderListEmptyComponent
  94. = { this._getRenderListEmptyComponent() }
  95. sections = { this._toDisplayableList() } />
  96. );
  97. }
  98. _getRenderListEmptyComponent: () => Object;
  99. /**
  100. * Returns a list empty component if a custom one has to be rendered instead
  101. * of the default one in the {@link NavigateSectionList}.
  102. *
  103. * @private
  104. * @returns {?React$Component}
  105. */
  106. _getRenderListEmptyComponent() {
  107. const { _authorization, t } = this.props;
  108. // If we don't provide a list specific renderListEmptyComponent, then
  109. // the default empty component of the NavigateSectionList will be
  110. // rendered, which (atm) is a simple "Pull to refresh" message.
  111. if (_authorization !== 'denied') {
  112. return undefined;
  113. }
  114. return (
  115. <View style = { styles.noPermissionMessageView }>
  116. <Text style = { styles.noPermissionMessageText }>
  117. { t('calendarSync.permissionMessage') }
  118. </Text>
  119. <TouchableOpacity
  120. onPress = { openSettings }
  121. style = { styles.noPermissionMessageButton } >
  122. <Text style = { styles.noPermissionMessageButtonText }>
  123. { t('calendarSync.permissionButton') }
  124. </Text>
  125. </TouchableOpacity>
  126. </View>
  127. );
  128. }
  129. _onPress: string => Function;
  130. /**
  131. * Handles the list's navigate action.
  132. *
  133. * @private
  134. * @param {string} url - The url string to navigate to.
  135. * @returns {void}
  136. */
  137. _onPress(url) {
  138. this.props.dispatch(appNavigate(url));
  139. }
  140. _onRefresh: () => void;
  141. /**
  142. * Callback to execute when the list is doing a pull-to-refresh.
  143. *
  144. * @private
  145. * @returns {void}
  146. */
  147. _onRefresh() {
  148. this.props.dispatch(refreshCalendar(true));
  149. }
  150. _toDateString: Object => string;
  151. /**
  152. * Generates a date (interval) string for a given event.
  153. *
  154. * @param {Object} event - The event.
  155. * @private
  156. * @returns {string}
  157. */
  158. _toDateString(event) {
  159. const startDateTime
  160. = getLocalizedDateFormatter(event.startDate).format('lll');
  161. const endTime
  162. = getLocalizedDateFormatter(event.endDate).format('LT');
  163. return `${startDateTime} - ${endTime}`;
  164. }
  165. _toDisplayableItem: Object => Object;
  166. /**
  167. * Creates a displayable object from an event.
  168. *
  169. * @param {Object} event - The calendar event.
  170. * @private
  171. * @returns {Object}
  172. */
  173. _toDisplayableItem(event) {
  174. return {
  175. key: `${event.id}-${event.startDate}`,
  176. lines: [
  177. event.url,
  178. this._toDateString(event)
  179. ],
  180. title: event.title,
  181. url: event.url
  182. };
  183. }
  184. _toDisplayableList: () => Array<Object>;
  185. /**
  186. * Transforms the event list to a displayable list with sections.
  187. *
  188. * @private
  189. * @returns {Array<Object>}
  190. */
  191. _toDisplayableList() {
  192. const { _eventList, t } = this.props;
  193. const now = Date.now();
  194. const { createSection } = NavigateSectionList;
  195. const nowSection = createSection(t('calendarSync.now'), 'now');
  196. const nextSection = createSection(t('calendarSync.next'), 'next');
  197. const laterSection = createSection(t('calendarSync.later'), 'later');
  198. for (const event of _eventList) {
  199. const displayableEvent = this._toDisplayableItem(event);
  200. if (event.startDate < now && event.endDate > now) {
  201. nowSection.data.push(displayableEvent);
  202. } else if (event.startDate > now) {
  203. if (nextSection.data.length
  204. && nextSection.data[0].startDate !== event.startDate) {
  205. laterSection.data.push(displayableEvent);
  206. } else {
  207. nextSection.data.push(displayableEvent);
  208. }
  209. }
  210. }
  211. const sectionList = [];
  212. for (const section of [
  213. nowSection,
  214. nextSection,
  215. laterSection
  216. ]) {
  217. section.data.length && sectionList.push(section);
  218. }
  219. return sectionList;
  220. }
  221. }
  222. /**
  223. * Maps redux state to component props.
  224. *
  225. * @param {Object} state - The redux state.
  226. * @returns {{
  227. * _authorization: ?string,
  228. * _eventList: Array<Object>
  229. * }}
  230. */
  231. function _mapStateToProps(state: Object) {
  232. const { authorization, events } = state['features/calendar-sync'];
  233. return {
  234. _authorization: authorization,
  235. _eventList: events
  236. };
  237. }
  238. export default CALENDAR_ENABLED
  239. ? translate(connect(_mapStateToProps)(MeetingList))
  240. : undefined;