Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

MeetingList.native.js 7.5KB

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