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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { refreshCalendarEntryList } from '../actions';
  5. import { appNavigate } from '../../app';
  6. import { getLocalizedDateFormatter, translate } from '../../base/i18n';
  7. import { NavigateSectionList } from '../../base/react';
  8. type Props = {
  9. /**
  10. * Indicates if the list is disabled or not.
  11. */
  12. disabled: boolean,
  13. /**
  14. * The Redux dispatch function.
  15. */
  16. dispatch: Function,
  17. /**
  18. * The calendar event list.
  19. */
  20. _eventList: Array<Object>,
  21. /**
  22. * The translate function.
  23. */
  24. t: Function
  25. };
  26. /**
  27. * Component to display a list of events from the (mobile) user's calendar.
  28. */
  29. class MeetingList extends Component<Props> {
  30. /**
  31. * Default values for the component's props.
  32. */
  33. static defaultProps = {
  34. _eventList: []
  35. };
  36. /**
  37. * Constructor of the MeetingList component.
  38. *
  39. * @inheritdoc
  40. */
  41. constructor(props) {
  42. super(props);
  43. this._onPress = this._onPress.bind(this);
  44. this._onRefresh = this._onRefresh.bind(this);
  45. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  46. this._toDisplayableList = this._toDisplayableList.bind(this);
  47. this._toDateString = this._toDateString.bind(this);
  48. }
  49. /**
  50. * Implements the React Components's render method.
  51. *
  52. * @inheritdoc
  53. */
  54. render() {
  55. const { disabled } = this.props;
  56. return (
  57. <NavigateSectionList
  58. disabled = { disabled }
  59. onPress = { this._onPress }
  60. onRefresh = { this._onRefresh }
  61. sections = { this._toDisplayableList() } />
  62. );
  63. }
  64. _onPress: string => Function
  65. /**
  66. * Handles the list's navigate action.
  67. *
  68. * @private
  69. * @param {string} url - The url string to navigate to.
  70. * @returns {void}
  71. */
  72. _onPress(url) {
  73. const { dispatch } = this.props;
  74. dispatch(appNavigate(url));
  75. }
  76. _onRefresh: () => void
  77. /**
  78. * Callback to execute when the list is doing a pull-to-refresh.
  79. *
  80. * @private
  81. * @returns {void}
  82. */
  83. _onRefresh() {
  84. const { dispatch } = this.props;
  85. dispatch(refreshCalendarEntryList());
  86. }
  87. _toDisplayableItem: Object => Object
  88. /**
  89. * Creates a displayable object from an event.
  90. *
  91. * @private
  92. * @param {Object} event - The calendar event.
  93. * @returns {Object}
  94. */
  95. _toDisplayableItem(event) {
  96. return {
  97. key: `${event.id}-${event.startDate}`,
  98. lines: [
  99. event.url,
  100. this._toDateString(event)
  101. ],
  102. title: event.title,
  103. url: event.url
  104. };
  105. }
  106. _toDisplayableList: () => Array<Object>
  107. /**
  108. * Transforms the event list to a displayable list
  109. * with sections.
  110. *
  111. * @private
  112. * @returns {Array<Object>}
  113. */
  114. _toDisplayableList() {
  115. const { _eventList, t } = this.props;
  116. const now = Date.now();
  117. const nowSection = NavigateSectionList.createSection(
  118. t('calendarSync.now'),
  119. 'now'
  120. );
  121. const nextSection = NavigateSectionList.createSection(
  122. t('calendarSync.next'),
  123. 'next'
  124. );
  125. const laterSection = NavigateSectionList.createSection(
  126. t('calendarSync.later'),
  127. 'later'
  128. );
  129. for (const event of _eventList) {
  130. const displayableEvent = this._toDisplayableItem(event);
  131. if (event.startDate < now && event.endDate > now) {
  132. nowSection.data.push(displayableEvent);
  133. } else if (event.startDate > now) {
  134. if (nextSection.data.length
  135. && nextSection.data[0].startDate !== event.startDate) {
  136. laterSection.data.push(displayableEvent);
  137. } else {
  138. nextSection.data.push(displayableEvent);
  139. }
  140. }
  141. }
  142. const sectionList = [];
  143. for (const section of [
  144. nowSection,
  145. nextSection,
  146. laterSection
  147. ]) {
  148. if (section.data.length) {
  149. sectionList.push(section);
  150. }
  151. }
  152. return sectionList;
  153. }
  154. _toDateString: Object => string
  155. /**
  156. * Generates a date (interval) string for a given event.
  157. *
  158. * @private
  159. * @param {Object} event - The event.
  160. * @returns {string}
  161. */
  162. _toDateString(event) {
  163. /* eslint-disable max-len */
  164. const startDateTime = getLocalizedDateFormatter(event.startDate).format('lll');
  165. const endTime = getLocalizedDateFormatter(event.endDate).format('LT');
  166. return `${startDateTime} - ${endTime}`;
  167. /* eslint-enable max-len */
  168. }
  169. }
  170. /**
  171. * Maps redux state to component props.
  172. *
  173. * @param {Object} state - The redux state.
  174. * @returns {{
  175. * _eventList: Array
  176. * }}
  177. */
  178. export function _mapStateToProps(state: Object) {
  179. return {
  180. _eventList: state['features/calendar-sync'].events
  181. };
  182. }
  183. export default translate(connect(_mapStateToProps)(MeetingList));