Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CalendarList.web.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 { Icon, IconPlusCalendar } from '../../base/icons';
  10. import { AbstractPage } from '../../base/react';
  11. import { connect } from '../../base/redux';
  12. import { openSettingsDialog, SETTINGS_TABS } from '../../settings';
  13. import { refreshCalendar } from '../actions';
  14. import { ERRORS } from '../constants';
  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._onKeyPressOpenSettings = this._onKeyPressOpenSettings.bind(this);
  63. this._onRefreshEvents = this._onRefreshEvents.bind(this);
  64. }
  65. /**
  66. * Implements React's {@link Component#render}.
  67. *
  68. * @inheritdoc
  69. */
  70. render() {
  71. const { disabled } = this.props;
  72. return (
  73. CalendarListContent
  74. ? <CalendarListContent
  75. disabled = { disabled }
  76. listEmptyComponent
  77. = { this._getRenderListEmptyComponent() } />
  78. : null
  79. );
  80. }
  81. /**
  82. * Returns a component for showing the error message related to calendar
  83. * sync.
  84. *
  85. * @private
  86. * @returns {React$Component}
  87. */
  88. _getErrorMessage() {
  89. const { _calendarError = {}, t } = this.props;
  90. let errorMessageKey = 'calendarSync.error.generic';
  91. let showRefreshButton = true;
  92. let showSettingsButton = true;
  93. if (_calendarError.error === ERRORS.GOOGLE_APP_MISCONFIGURED) {
  94. errorMessageKey = 'calendarSync.error.appConfiguration';
  95. showRefreshButton = false;
  96. showSettingsButton = false;
  97. } else if (_calendarError.error === ERRORS.AUTH_FAILED) {
  98. errorMessageKey = 'calendarSync.error.notSignedIn';
  99. showRefreshButton = false;
  100. }
  101. return (
  102. <div className = 'meetings-list-empty'>
  103. <p className = 'description'>
  104. { t(errorMessageKey) }
  105. </p>
  106. <div className = 'calendar-action-buttons'>
  107. { showSettingsButton
  108. && <div
  109. className = 'button'
  110. onClick = { this._onOpenSettings }>
  111. { t('calendarSync.permissionButton') }
  112. </div>
  113. }
  114. { showRefreshButton
  115. && <div
  116. className = 'button'
  117. onClick = { this._onRefreshEvents }>
  118. { t('calendarSync.refresh') }
  119. </div>
  120. }
  121. </div>
  122. </div>
  123. );
  124. }
  125. _getRenderListEmptyComponent: () => Object;
  126. /**
  127. * Returns a list empty component if a custom one has to be rendered instead
  128. * of the default one in the {@link NavigateSectionList}.
  129. *
  130. * @private
  131. * @returns {React$Component}
  132. */
  133. _getRenderListEmptyComponent() {
  134. const {
  135. _calendarError,
  136. _hasIntegrationSelected,
  137. _hasLoadedEvents,
  138. t
  139. } = this.props;
  140. if (_calendarError) {
  141. return this._getErrorMessage();
  142. } else if (_hasIntegrationSelected && _hasLoadedEvents) {
  143. return (
  144. <div className = 'meetings-list-empty'>
  145. <p className = 'description'>
  146. { t('calendarSync.noEvents') }
  147. </p>
  148. <div
  149. className = 'button'
  150. onClick = { this._onRefreshEvents }>
  151. { t('calendarSync.refresh') }
  152. </div>
  153. </div>
  154. );
  155. } else if (_hasIntegrationSelected && !_hasLoadedEvents) {
  156. return (
  157. <div className = 'meetings-list-empty'>
  158. <Spinner
  159. invertColor = { true }
  160. isCompleting = { false }
  161. size = 'medium' />
  162. </div>
  163. );
  164. }
  165. return (
  166. <div className = 'meetings-list-empty'>
  167. <div className = 'meetings-list-empty-image'>
  168. <img
  169. alt = { t('welcomepage.logo.calendar') }
  170. src = './images/calendar.svg' />
  171. </div>
  172. <div className = 'description'>
  173. { t('welcomepage.connectCalendarText', {
  174. app: interfaceConfig.APP_NAME,
  175. provider: interfaceConfig.PROVIDER_NAME
  176. }) }
  177. </div>
  178. <div
  179. className = 'meetings-list-empty-button'
  180. onClick = { this._onOpenSettings }
  181. onKeyPress = { this._onKeyPressOpenSettings }
  182. role = 'button'>
  183. <Icon
  184. className = 'meetings-list-empty-icon'
  185. src = { IconPlusCalendar } />
  186. <span>{ t('welcomepage.connectCalendarButton') }</span>
  187. </div>
  188. </div>
  189. );
  190. }
  191. _onOpenSettings: () => void;
  192. /**
  193. * Opens {@code SettingsDialog}.
  194. *
  195. * @private
  196. * @returns {void}
  197. */
  198. _onOpenSettings() {
  199. sendAnalytics(createCalendarClickedEvent('calendar.connect'));
  200. this.props.dispatch(openSettingsDialog(SETTINGS_TABS.CALENDAR));
  201. }
  202. _onKeyPressOpenSettings: (Object) => void;
  203. /**
  204. * KeyPress handler for accessibility.
  205. *
  206. * @param {Object} e - The key event to handle.
  207. *
  208. * @returns {void}
  209. */
  210. _onKeyPressOpenSettings(e) {
  211. if (e.key === ' ' || e.key === 'Enter') {
  212. e.preventDefault();
  213. this._onOpenSettings();
  214. }
  215. }
  216. _onRefreshEvents: () => void;
  217. /**
  218. * Gets an updated list of calendar events.
  219. *
  220. * @private
  221. * @returns {void}
  222. */
  223. _onRefreshEvents() {
  224. this.props.dispatch(refreshCalendar(true));
  225. }
  226. }
  227. /**
  228. * Maps (parts of) the Redux state to the associated props for the
  229. * {@code CalendarList} component.
  230. *
  231. * @param {Object} state - The Redux state.
  232. * @private
  233. * @returns {{
  234. * _calendarError: Object,
  235. * _hasIntegrationSelected: boolean,
  236. * _hasLoadedEvents: boolean
  237. * }}
  238. */
  239. function _mapStateToProps(state) {
  240. const {
  241. error,
  242. events,
  243. integrationType,
  244. isLoadingEvents
  245. } = state['features/calendar-sync'];
  246. return {
  247. _calendarError: error,
  248. _hasIntegrationSelected: Boolean(integrationType),
  249. _hasLoadedEvents: Boolean(events) || !isLoadingEvents
  250. };
  251. }
  252. export default translate(connect(_mapStateToProps)(CalendarList));