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.1KB

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