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

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