123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- // @flow
-
- import React, { Component } from 'react';
- import { Text, TouchableOpacity, View } from 'react-native';
- import { connect } from 'react-redux';
-
- import { appNavigate } from '../../app';
- import { getLocalizedDateFormatter, translate } from '../../base/i18n';
- import { NavigateSectionList } from '../../base/react';
- import { openSettings } from '../../mobile/permissions';
-
- import { refreshCalendar } from '../actions';
- import { CALENDAR_ENABLED } from '../constants';
- import styles from './styles';
-
- type Props = {
-
- /**
- * The current state of the calendar access permission.
- */
- _authorization: string,
-
- /**
- * The calendar event list.
- */
- _eventList: Array<Object>,
-
- /**
- * Indicates if the list is disabled or not.
- */
- disabled: boolean,
-
- /**
- * The Redux dispatch function.
- */
- dispatch: Function,
-
- /**
- * The translate function.
- */
- t: Function
- };
-
- /**
- * Component to display a list of events from the (mobile) user's calendar.
- */
- class MeetingList extends Component<Props> {
- /**
- * Default values for the component's props.
- */
- static defaultProps = {
- _eventList: []
- };
-
- /**
- * Public API method for {@code Component}s rendered in
- * {@link AbstractPagedList}. When invoked, refreshes the calendar entries
- * in the app.
- *
- * Note: It is a static method as the {@code Component} may not be
- * initialized yet when the UI invokes refresh (e.g. {@link TabBarIOS} tab
- * change).
- *
- * @param {Function} dispatch - The Redux dispatch function.
- * @public
- * @returns {void}
- */
- static refresh(dispatch) {
- dispatch(refreshCalendar());
- }
-
- /**
- * Constructor of the MeetingList component.
- *
- * @inheritdoc
- */
- constructor(props) {
- super(props);
-
- this._getRenderListEmptyComponent
- = this._getRenderListEmptyComponent.bind(this);
- this._onPress = this._onPress.bind(this);
- this._onRefresh = this._onRefresh.bind(this);
- this._toDisplayableItem = this._toDisplayableItem.bind(this);
- this._toDisplayableList = this._toDisplayableList.bind(this);
- this._toDateString = this._toDateString.bind(this);
- }
-
- /**
- * Implements the React Components's render.
- *
- * @inheritdoc
- */
- render() {
- const { disabled } = this.props;
-
- return (
- <NavigateSectionList
- disabled = { disabled }
- onPress = { this._onPress }
- onRefresh = { this._onRefresh }
- renderListEmptyComponent = { this._getRenderListEmptyComponent }
- sections = { this._toDisplayableList() } />
- );
- }
-
- _getRenderListEmptyComponent: () => Object;
-
- /**
- * Returns a list empty component if a custom one has to be rendered instead
- * of the default one in the {@link NavigateSectionList}.
- *
- * @private
- * @returns {Component}
- */
- _getRenderListEmptyComponent() {
- const { _authorization, t } = this.props;
-
- if (_authorization === 'denied') {
- return (
- <View style = { styles.noPermissionMessageView }>
- <Text style = { styles.noPermissionMessageText }>
- { t('calendarSync.permissionMessage') }
- </Text>
- <TouchableOpacity
- onPress = { openSettings }
- style = { styles.noPermissionMessageButton } >
- <Text style = { styles.noPermissionMessageButtonText }>
- { t('calendarSync.permissionButton') }
- </Text>
- </TouchableOpacity>
- </View>
- );
- }
-
- return null;
- }
-
- _onPress: string => Function;
-
- /**
- * Handles the list's navigate action.
- *
- * @private
- * @param {string} url - The url string to navigate to.
- * @returns {void}
- */
- _onPress(url) {
- this.props.dispatch(appNavigate(url));
- }
-
- _onRefresh: () => void;
-
- /**
- * Callback to execute when the list is doing a pull-to-refresh.
- *
- * @private
- * @returns {void}
- */
- _onRefresh() {
- this.props.dispatch(refreshCalendar(true));
- }
-
- _toDisplayableItem: Object => Object;
-
- /**
- * Creates a displayable object from an event.
- *
- * @param {Object} event - The calendar event.
- * @private
- * @returns {Object}
- */
- _toDisplayableItem(event) {
- return {
- key: `${event.id}-${event.startDate}`,
- lines: [
- event.url,
- this._toDateString(event)
- ],
- title: event.title,
- url: event.url
- };
- }
-
- _toDisplayableList: () => Array<Object>;
-
- /**
- * Transforms the event list to a displayable list with sections.
- *
- * @private
- * @returns {Array<Object>}
- */
- _toDisplayableList() {
- const { _eventList, t } = this.props;
- const now = Date.now();
- const { createSection } = NavigateSectionList;
- const nowSection = createSection(t('calendarSync.now'), 'now');
- const nextSection = createSection(t('calendarSync.next'), 'next');
- const laterSection = createSection(t('calendarSync.later'), 'later');
-
- for (const event of _eventList) {
- const displayableEvent = this._toDisplayableItem(event);
-
- if (event.startDate < now && event.endDate > now) {
- nowSection.data.push(displayableEvent);
- } else if (event.startDate > now) {
- if (nextSection.data.length
- && nextSection.data[0].startDate !== event.startDate) {
- laterSection.data.push(displayableEvent);
- } else {
- nextSection.data.push(displayableEvent);
- }
- }
- }
-
- const sectionList = [];
-
- for (const section of [
- nowSection,
- nextSection,
- laterSection
- ]) {
- if (section.data.length) {
- sectionList.push(section);
- }
- }
-
- return sectionList;
- }
-
- _toDateString: Object => string;
-
- /**
- * Generates a date (interval) string for a given event.
- *
- * @param {Object} event - The event.
- * @private
- * @returns {string}
- */
- _toDateString(event) {
- const startDateTime
- = getLocalizedDateFormatter(event.startDate).format('lll');
- const endTime
- = getLocalizedDateFormatter(event.endDate).format('LT');
-
- return `${startDateTime} - ${endTime}`;
- }
- }
-
- /**
- * Maps redux state to component props.
- *
- * @param {Object} state - The redux state.
- * @returns {{
- * _eventList: Array
- * }}
- */
- function _mapStateToProps(state: Object) {
- const calendarSyncState = state['features/calendar-sync'];
-
- return {
- _authorization: calendarSyncState.authorization,
- _eventList: calendarSyncState.events
- };
- }
-
- export default CALENDAR_ENABLED
- ? translate(connect(_mapStateToProps)(MeetingList))
- : undefined;
|