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.

CalendarList.web.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // @flow
  2. import Button from '@atlaskit/button';
  3. import Spinner from '@atlaskit/spinner';
  4. import React from 'react';
  5. import { connect } from 'react-redux';
  6. import { translate } from '../../base/i18n';
  7. import { AbstractPage } from '../../base/react';
  8. import { openSettingsDialog, SETTINGS_TABS } from '../../settings';
  9. import {
  10. createCalendarClickedEvent,
  11. sendAnalytics
  12. } from '../../analytics';
  13. import { refreshCalendar } from '../actions';
  14. import { isCalendarEnabled } from '../functions';
  15. import CalendarListContent from './CalendarListContent';
  16. declare var interfaceConfig: Object;
  17. /**
  18. * The type of the React {@code Component} props of {@link CalendarList}.
  19. */
  20. type Props = {
  21. /**
  22. * Whether or not a calendar may be connected for fetching calendar events.
  23. */
  24. _hasIntegrationSelected: boolean,
  25. /**
  26. * Whether or not events have been fetched from a calendar.
  27. */
  28. _hasLoadedEvents: boolean,
  29. /**
  30. * Indicates if the list is disabled or not.
  31. */
  32. disabled: boolean,
  33. /**
  34. * The Redux dispatch function.
  35. */
  36. dispatch: Function,
  37. /**
  38. * The translate function.
  39. */
  40. t: Function
  41. };
  42. /**
  43. * Component to display a list of events from the user's calendar.
  44. */
  45. class CalendarList extends AbstractPage<Props> {
  46. /**
  47. * Initializes a new {@code CalendarList} instance.
  48. *
  49. * @inheritdoc
  50. */
  51. constructor(props) {
  52. super(props);
  53. // Bind event handlers so they are only bound once per instance.
  54. this._getRenderListEmptyComponent
  55. = this._getRenderListEmptyComponent.bind(this);
  56. this._onOpenSettings = this._onOpenSettings.bind(this);
  57. this._onRefreshEvents = this._onRefreshEvents.bind(this);
  58. }
  59. /**
  60. * Implements React's {@link Component#render}.
  61. *
  62. * @inheritdoc
  63. */
  64. render() {
  65. const { disabled } = this.props;
  66. return (
  67. CalendarListContent
  68. ? <CalendarListContent
  69. disabled = { disabled }
  70. renderListEmptyComponent
  71. = { this._getRenderListEmptyComponent() } />
  72. : null
  73. );
  74. }
  75. _getRenderListEmptyComponent: () => Object;
  76. /**
  77. * Returns a list empty component if a custom one has to be rendered instead
  78. * of the default one in the {@link NavigateSectionList}.
  79. *
  80. * @private
  81. * @returns {React$Component}
  82. */
  83. _getRenderListEmptyComponent() {
  84. const { _hasIntegrationSelected, _hasLoadedEvents, t } = this.props;
  85. if (_hasIntegrationSelected && _hasLoadedEvents) {
  86. return (
  87. <div className = 'navigate-section-list-empty'>
  88. <div>{ t('calendarSync.noEvents') }</div>
  89. <Button
  90. appearance = 'primary'
  91. className = 'calendar-button'
  92. id = 'connect_calendar_button'
  93. onClick = { this._onRefreshEvents }
  94. type = 'button'>
  95. { t('calendarSync.refresh') }
  96. </Button>
  97. </div>
  98. );
  99. } else if (_hasIntegrationSelected && !_hasLoadedEvents) {
  100. return (
  101. <div className = 'navigate-section-list-empty'>
  102. <Spinner
  103. invertColor = { true }
  104. isCompleting = { false }
  105. size = 'medium' />
  106. </div>
  107. );
  108. }
  109. return (
  110. <div className = 'navigate-section-list-empty'>
  111. <p className = 'header-text-description'>
  112. { t('welcomepage.connectCalendarText', {
  113. app: interfaceConfig.APP_NAME
  114. }) }
  115. </p>
  116. <Button
  117. appearance = 'primary'
  118. className = 'calendar-button'
  119. id = 'connect_calendar_button'
  120. onClick = { this._onOpenSettings }
  121. type = 'button'>
  122. { t('welcomepage.connectCalendarButton') }
  123. </Button>
  124. </div>
  125. );
  126. }
  127. _onOpenSettings: () => void;
  128. /**
  129. * Opens {@code SettingsDialog}.
  130. *
  131. * @private
  132. * @returns {void}
  133. */
  134. _onOpenSettings() {
  135. sendAnalytics(createCalendarClickedEvent('calendar.connect'));
  136. this.props.dispatch(openSettingsDialog(SETTINGS_TABS.CALENDAR));
  137. }
  138. _onRefreshEvents: () => void;
  139. /**
  140. * Gets an updated list of calendar events.
  141. *
  142. * @private
  143. * @returns {void}
  144. */
  145. _onRefreshEvents() {
  146. this.props.dispatch(refreshCalendar(true));
  147. }
  148. }
  149. /**
  150. * Maps (parts of) the Redux state to the associated props for the
  151. * {@code CalendarList} component.
  152. *
  153. * @param {Object} state - The Redux state.
  154. * @private
  155. * @returns {{
  156. * _hasIntegrationSelected: boolean,
  157. * _hasLoadedEvents: boolean
  158. * }}
  159. */
  160. function _mapStateToProps(state) {
  161. const {
  162. events,
  163. integrationType,
  164. isLoadingEvents
  165. } = state['features/calendar-sync'];
  166. return {
  167. _hasIntegrationSelected: Boolean(integrationType),
  168. _hasLoadedEvents: Boolean(events) || !isLoadingEvents
  169. };
  170. }
  171. export default isCalendarEnabled()
  172. ? translate(connect(_mapStateToProps)(CalendarList))
  173. : undefined;