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.

CalendarTab.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // @flow
  2. import Button from '@atlaskit/button/standard-button';
  3. import Spinner from '@atlaskit/spinner';
  4. import React, { Component } from 'react';
  5. import { translate } from '../../../base/i18n';
  6. import { connect } from '../../../base/redux';
  7. import {
  8. CALENDAR_TYPE,
  9. MicrosoftSignInButton,
  10. clearCalendarIntegration,
  11. bootstrapCalendarIntegration,
  12. isCalendarEnabled,
  13. signIn
  14. } from '../../../calendar-sync';
  15. import { GoogleSignInButton } from '../../../google-api';
  16. import logger from '../../logger';
  17. declare var interfaceConfig: Object;
  18. /**
  19. * The type of the React {@code Component} props of {@link CalendarTab}.
  20. */
  21. type Props = {
  22. /**
  23. * The name given to this Jitsi Application.
  24. */
  25. _appName: string,
  26. /**
  27. * Whether or not to display a button to sign in to Google.
  28. */
  29. _enableGoogleIntegration: boolean,
  30. /**
  31. * Whether or not to display a button to sign in to Microsoft.
  32. */
  33. _enableMicrosoftIntegration: boolean,
  34. /**
  35. * The current calendar integration in use, if any.
  36. */
  37. _isConnectedToCalendar: boolean,
  38. /**
  39. * The email address associated with the calendar integration in use.
  40. */
  41. _profileEmail: string,
  42. /**
  43. * Invoked to change the configured calendar integration.
  44. */
  45. dispatch: Function,
  46. /**
  47. * Invoked to obtain translated strings.
  48. */
  49. t: Function
  50. };
  51. /**
  52. * The type of the React {@code Component} state of {@link CalendarTab}.
  53. */
  54. type State = {
  55. /**
  56. * Whether or not any third party APIs are being loaded.
  57. */
  58. loading: boolean
  59. };
  60. /**
  61. * React {@code Component} for modifying calendar integration.
  62. *
  63. * @extends Component
  64. */
  65. class CalendarTab extends Component<Props, State> {
  66. /**
  67. * Initializes a new {@code CalendarTab} instance.
  68. *
  69. * @inheritdoc
  70. */
  71. constructor(props: Props) {
  72. super(props);
  73. this.state = {
  74. loading: true
  75. };
  76. // Bind event handlers so they are only bound once for every instance.
  77. this._onClickDisconnect = this._onClickDisconnect.bind(this);
  78. this._onClickGoogle = this._onClickGoogle.bind(this);
  79. this._onClickMicrosoft = this._onClickMicrosoft.bind(this);
  80. }
  81. /**
  82. * Loads third party APIs as needed and bootstraps the initial calendar
  83. * state if not already set.
  84. *
  85. * @inheritdoc
  86. */
  87. componentDidMount() {
  88. this.props.dispatch(bootstrapCalendarIntegration())
  89. .catch(err => logger.error('CalendarTab bootstrap failed', err))
  90. .then(() => this.setState({ loading: false }));
  91. }
  92. /**
  93. * Implements React's {@link Component#render()}.
  94. *
  95. * @inheritdoc
  96. * @returns {ReactElement}
  97. */
  98. render() {
  99. let view;
  100. if (this.state.loading) {
  101. view = this._renderLoadingState();
  102. } else if (this.props._isConnectedToCalendar) {
  103. view = this._renderSignOutState();
  104. } else {
  105. view = this._renderSignInState();
  106. }
  107. return (
  108. <div className = 'calendar-tab'>
  109. { view }
  110. </div>
  111. );
  112. }
  113. /**
  114. * Dispatches the action to start the sign in flow for a given calendar
  115. * integration type.
  116. *
  117. * @param {string} type - The calendar type to try integrating with.
  118. * @private
  119. * @returns {void}
  120. */
  121. _attemptSignIn(type) {
  122. this.props.dispatch(signIn(type));
  123. }
  124. _onClickDisconnect: (Object) => void;
  125. /**
  126. * Dispatches an action to sign out of the currently connected third party
  127. * used for calendar integration.
  128. *
  129. * @private
  130. * @returns {void}
  131. */
  132. _onClickDisconnect() {
  133. // We clear the integration state instead of actually signing out. This
  134. // is for two primary reasons. Microsoft does not support a sign out and
  135. // instead relies on clearing of local auth data. Google signout can
  136. // also sign the user out of YouTube. So for now we've decided not to
  137. // do an actual sign out.
  138. this.props.dispatch(clearCalendarIntegration());
  139. }
  140. _onClickGoogle: () => void;
  141. /**
  142. * Starts the sign in flow for Google calendar integration.
  143. *
  144. * @private
  145. * @returns {void}
  146. */
  147. _onClickGoogle() {
  148. this._attemptSignIn(CALENDAR_TYPE.GOOGLE);
  149. }
  150. _onClickMicrosoft: () => void;
  151. /**
  152. * Starts the sign in flow for Microsoft calendar integration.
  153. *
  154. * @private
  155. * @returns {void}
  156. */
  157. _onClickMicrosoft() {
  158. this._attemptSignIn(CALENDAR_TYPE.MICROSOFT);
  159. }
  160. /**
  161. * Render a React Element to indicate third party APIs are being loaded.
  162. *
  163. * @private
  164. * @returns {ReactElement}
  165. */
  166. _renderLoadingState() {
  167. return (
  168. <Spinner
  169. isCompleting = { false }
  170. size = 'medium' />
  171. );
  172. }
  173. /**
  174. * Render a React Element to sign into a third party for calendar
  175. * integration.
  176. *
  177. * @private
  178. * @returns {ReactElement}
  179. */
  180. _renderSignInState() {
  181. const {
  182. _appName,
  183. _enableGoogleIntegration,
  184. _enableMicrosoftIntegration,
  185. t
  186. } = this.props;
  187. return (
  188. <div>
  189. <p>
  190. { t('settings.calendar.about',
  191. { appName: _appName || '' }) }
  192. </p>
  193. { _enableGoogleIntegration
  194. && <div className = 'calendar-tab-sign-in'>
  195. <GoogleSignInButton
  196. onClick = { this._onClickGoogle }
  197. text = { t('liveStreaming.signIn') } />
  198. </div> }
  199. { _enableMicrosoftIntegration
  200. && <div className = 'calendar-tab-sign-in'>
  201. <MicrosoftSignInButton
  202. onClick = { this._onClickMicrosoft }
  203. text = { t('settings.calendar.microsoftSignIn') } />
  204. </div> }
  205. </div>
  206. );
  207. }
  208. /**
  209. * Render a React Element to sign out of the currently connected third
  210. * party used for calendar integration.
  211. *
  212. * @private
  213. * @returns {ReactElement}
  214. */
  215. _renderSignOutState() {
  216. const { _profileEmail, t } = this.props;
  217. return (
  218. <div>
  219. <div className = 'sign-out-cta'>
  220. { t('settings.calendar.signedIn',
  221. { email: _profileEmail }) }
  222. </div>
  223. <Button
  224. appearance = 'primary'
  225. id = 'calendar_logout'
  226. onClick = { this._onClickDisconnect }
  227. type = 'button'>
  228. { t('settings.calendar.disconnect') }
  229. </Button>
  230. </div>
  231. );
  232. }
  233. }
  234. /**
  235. * Maps (parts of) the Redux state to the associated props for the
  236. * {@code CalendarTab} component.
  237. *
  238. * @param {Object} state - The Redux state.
  239. * @private
  240. * @returns {{
  241. * _appName: string,
  242. * _enableGoogleIntegration: boolean,
  243. * _enableMicrosoftIntegration: boolean,
  244. * _isConnectedToCalendar: boolean,
  245. * _profileEmail: string
  246. * }}
  247. */
  248. function _mapStateToProps(state) {
  249. const calendarState = state['features/calendar-sync'] || {};
  250. const {
  251. googleApiApplicationClientID,
  252. microsoftApiApplicationClientID
  253. } = state['features/base/config'];
  254. const calendarEnabled = isCalendarEnabled(state);
  255. return {
  256. _appName: interfaceConfig.APP_NAME,
  257. _enableGoogleIntegration: Boolean(
  258. calendarEnabled && googleApiApplicationClientID),
  259. _enableMicrosoftIntegration: Boolean(
  260. calendarEnabled && microsoftApiApplicationClientID),
  261. _isConnectedToCalendar: calendarState.integrationReady,
  262. _profileEmail: calendarState.profileEmail
  263. };
  264. }
  265. export default translate(connect(_mapStateToProps)(CalendarTab));