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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { appNavigate } from '../../app';
  5. import { translate } from '../../base/i18n';
  6. import { NavigateSectionList } from '../../base/react';
  7. import { getLocalizedDateFormatter } from '../../base/util';
  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._toDisplayableItem = this._toDisplayableItem.bind(this);
  45. this._toDisplayableList = this._toDisplayableList.bind(this);
  46. this._toDateString = this._toDateString.bind(this);
  47. }
  48. /**
  49. * Implements the React Components's render method.
  50. *
  51. * @inheritdoc
  52. */
  53. render() {
  54. const { disabled } = this.props;
  55. return (
  56. <NavigateSectionList
  57. disabled = { disabled }
  58. onPress = { this._onPress }
  59. sections = { this._toDisplayableList() } />
  60. );
  61. }
  62. _onPress: string => Function
  63. /**
  64. * Handles the list's navigate action.
  65. *
  66. * @private
  67. * @param {string} url - The url string to navigate to.
  68. * @returns {void}
  69. */
  70. _onPress(url) {
  71. const { dispatch } = this.props;
  72. dispatch(appNavigate(url));
  73. }
  74. _toDisplayableItem: Object => Object
  75. /**
  76. * Creates a displayable object from an event.
  77. *
  78. * @private
  79. * @param {Object} event - The calendar event.
  80. * @returns {Object}
  81. */
  82. _toDisplayableItem(event) {
  83. return {
  84. key: `${event.id}-${event.startDate}`,
  85. lines: [
  86. event.url,
  87. this._toDateString(event)
  88. ],
  89. title: event.title,
  90. url: event.url
  91. };
  92. }
  93. _toDisplayableList: () => Array<Object>
  94. /**
  95. * Transforms the event list to a displayable list
  96. * with sections.
  97. *
  98. * @private
  99. * @returns {Array<Object>}
  100. */
  101. _toDisplayableList() {
  102. const { _eventList, t } = this.props;
  103. const now = Date.now();
  104. const nowSection = NavigateSectionList.createSection(
  105. t('calendarSync.now'),
  106. 'now'
  107. );
  108. const nextSection = NavigateSectionList.createSection(
  109. t('calendarSync.next'),
  110. 'next'
  111. );
  112. const laterSection = NavigateSectionList.createSection(
  113. t('calendarSync.later'),
  114. 'later'
  115. );
  116. for (const event of _eventList) {
  117. const displayableEvent = this._toDisplayableItem(event);
  118. if (event.startDate < now && event.endDate > now) {
  119. nowSection.data.push(displayableEvent);
  120. } else if (event.startDate > now) {
  121. if (nextSection.data.length
  122. && nextSection.data[0].startDate !== event.startDate) {
  123. laterSection.data.push(displayableEvent);
  124. } else {
  125. nextSection.data.push(displayableEvent);
  126. }
  127. }
  128. }
  129. const sectionList = [];
  130. for (const section of [
  131. nowSection,
  132. nextSection,
  133. laterSection
  134. ]) {
  135. if (section.data.length) {
  136. sectionList.push(section);
  137. }
  138. }
  139. return sectionList;
  140. }
  141. _toDateString: Object => string
  142. /**
  143. * Generates a date (interval) string for a given event.
  144. *
  145. * @private
  146. * @param {Object} event - The event.
  147. * @returns {string}
  148. */
  149. _toDateString(event) {
  150. /* eslint-disable max-len */
  151. const startDateTime = getLocalizedDateFormatter(event.startDate).format('lll');
  152. const endTime = getLocalizedDateFormatter(event.endDate).format('LT');
  153. return `${startDateTime} - ${endTime}`;
  154. /* eslint-enable max-len */
  155. }
  156. }
  157. /**
  158. * Maps redux state to component props.
  159. *
  160. * @param {Object} state - The redux state.
  161. * @returns {{
  162. * _eventList: Array
  163. * }}
  164. */
  165. export function _mapStateToProps(state: Object) {
  166. return {
  167. _eventList: state['features/calendar-sync'].events
  168. };
  169. }
  170. export default translate(connect(_mapStateToProps)(MeetingList));