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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // @flow
  2. import Spinner from '@atlaskit/spinner';
  3. import React from 'react';
  4. import {
  5. createCalendarClickedEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import { translate } from '../../base/i18n';
  9. import { AbstractPage } from '../../base/react';
  10. import { connect } from '../../base/redux';
  11. import { openSettingsDialog, SETTINGS_TABS } from '../../settings';
  12. import { refreshCalendar } from '../actions';
  13. import { ERRORS } from '../constants';
  14. import CalendarListContent from './CalendarListContent';
  15. declare var interfaceConfig: Object;
  16. /**
  17. * The type of the React {@code Component} props of {@link CalendarList}.
  18. */
  19. type Props = {
  20. /**
  21. * The error object containing details about any error that has occurred
  22. * while interacting with calendar integration.
  23. */
  24. _calendarError: ?Object,
  25. /**
  26. * Whether or not a calendar may be connected for fetching calendar events.
  27. */
  28. _hasIntegrationSelected: boolean,
  29. /**
  30. * Whether or not events have been fetched from a calendar.
  31. */
  32. _hasLoadedEvents: boolean,
  33. /**
  34. * Indicates if the list is disabled or not.
  35. */
  36. disabled: boolean,
  37. /**
  38. * The Redux dispatch function.
  39. */
  40. dispatch: Function,
  41. /**
  42. * The translate function.
  43. */
  44. t: Function
  45. };
  46. /**
  47. * Component to display a list of events from the user's calendar.
  48. */
  49. class CalendarList extends AbstractPage<Props> {
  50. /**
  51. * Initializes a new {@code CalendarList} instance.
  52. *
  53. * @inheritdoc
  54. */
  55. constructor(props) {
  56. super(props);
  57. // Bind event handlers so they are only bound once per instance.
  58. this._getRenderListEmptyComponent
  59. = this._getRenderListEmptyComponent.bind(this);
  60. this._onOpenSettings = this._onOpenSettings.bind(this);
  61. this._onRefreshEvents = this._onRefreshEvents.bind(this);
  62. }
  63. /**
  64. * Implements React's {@link Component#render}.
  65. *
  66. * @inheritdoc
  67. */
  68. render() {
  69. const { disabled } = this.props;
  70. return (
  71. CalendarListContent
  72. ? <CalendarListContent
  73. disabled = { disabled }
  74. listEmptyComponent
  75. = { this._getRenderListEmptyComponent() } />
  76. : null
  77. );
  78. }
  79. /**
  80. * Returns a component for showing the error message related to calendar
  81. * sync.
  82. *
  83. * @private
  84. * @returns {React$Component}
  85. */
  86. _getErrorMessage() {
  87. const { _calendarError = {}, t } = this.props;
  88. let errorMessageKey = 'calendarSync.error.generic';
  89. let showRefreshButton = true;
  90. let showSettingsButton = true;
  91. if (_calendarError.error === ERRORS.GOOGLE_APP_MISCONFIGURED) {
  92. errorMessageKey = 'calendarSync.error.appConfiguration';
  93. showRefreshButton = false;
  94. showSettingsButton = false;
  95. } else if (_calendarError.error === ERRORS.AUTH_FAILED) {
  96. errorMessageKey = 'calendarSync.error.notSignedIn';
  97. showRefreshButton = false;
  98. }
  99. return (
  100. <div className = 'meetings-list-empty'>
  101. <p className = 'description'>
  102. { t(errorMessageKey) }
  103. </p>
  104. <div className = 'calendar-action-buttons'>
  105. { showSettingsButton
  106. && <div
  107. className = 'button'
  108. onClick = { this._onOpenSettings }>
  109. { t('calendarSync.permissionButton') }
  110. </div>
  111. }
  112. { showRefreshButton
  113. && <div
  114. className = 'button'
  115. onClick = { this._onRefreshEvents }>
  116. { t('calendarSync.refresh') }
  117. </div>
  118. }
  119. </div>
  120. </div>
  121. );
  122. }
  123. _getRenderListEmptyComponent: () => Object;
  124. /**
  125. * Returns a list empty component if a custom one has to be rendered instead
  126. * of the default one in the {@link NavigateSectionList}.
  127. *
  128. * @private
  129. * @returns {React$Component}
  130. */
  131. _getRenderListEmptyComponent() {
  132. const {
  133. _calendarError,
  134. _hasIntegrationSelected,
  135. _hasLoadedEvents,
  136. t
  137. } = this.props;
  138. if (_calendarError) {
  139. return this._getErrorMessage();
  140. } else if (_hasIntegrationSelected && _hasLoadedEvents) {
  141. return (
  142. <div className = 'meetings-list-empty'>
  143. <p className = 'description'>
  144. { t('calendarSync.noEvents') }
  145. </p>
  146. <div
  147. className = 'button'
  148. onClick = { this._onRefreshEvents }>
  149. { t('calendarSync.refresh') }
  150. </div>
  151. </div>
  152. );
  153. } else if (_hasIntegrationSelected && !_hasLoadedEvents) {
  154. return (
  155. <div className = 'meetings-list-empty'>
  156. <Spinner
  157. invertColor = { true }
  158. isCompleting = { false }
  159. size = 'medium' />
  160. </div>
  161. );
  162. }
  163. return (
  164. <div className = 'meetings-list-empty'>
  165. <p className = 'description'>
  166. { t('welcomepage.connectCalendarText', {
  167. app: interfaceConfig.APP_NAME,
  168. provider: interfaceConfig.PROVIDER_NAME
  169. }) }
  170. </p>
  171. <div
  172. className = 'button'
  173. onClick = { this._onOpenSettings }>
  174. { t('welcomepage.connectCalendarButton') }
  175. </div>
  176. </div>
  177. );
  178. }
  179. _onOpenSettings: () => void;
  180. /**
  181. * Opens {@code SettingsDialog}.
  182. *
  183. * @private
  184. * @returns {void}
  185. */
  186. _onOpenSettings() {
  187. sendAnalytics(createCalendarClickedEvent('calendar.connect'));
  188. this.props.dispatch(openSettingsDialog(SETTINGS_TABS.CALENDAR));
  189. }
  190. _onRefreshEvents: () => void;
  191. /**
  192. * Gets an updated list of calendar events.
  193. *
  194. * @private
  195. * @returns {void}
  196. */
  197. _onRefreshEvents() {
  198. this.props.dispatch(refreshCalendar(true));
  199. }
  200. }
  201. /**
  202. * Maps (parts of) the Redux state to the associated props for the
  203. * {@code CalendarList} component.
  204. *
  205. * @param {Object} state - The Redux state.
  206. * @private
  207. * @returns {{
  208. * _calendarError: Object,
  209. * _hasIntegrationSelected: boolean,
  210. * _hasLoadedEvents: boolean
  211. * }}
  212. */
  213. function _mapStateToProps(state) {
  214. const {
  215. error,
  216. events,
  217. integrationType,
  218. isLoadingEvents
  219. } = state['features/calendar-sync'];
  220. return {
  221. _calendarError: error,
  222. _hasIntegrationSelected: Boolean(integrationType),
  223. _hasLoadedEvents: Boolean(events) || !isLoadingEvents
  224. };
  225. }
  226. export default translate(connect(_mapStateToProps)(CalendarList));