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

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