From 7e332dc4b7a5fe0c275a8d802752d7ba5dc3d4ec Mon Sep 17 00:00:00 2001 From: vantezzen Date: Sun, 20 Oct 2019 20:08:57 +0200 Subject: Improve switch between accounts and servers --- .../settings/navigation/SettingsNavigation.js | 50 ++++++++++++++++++++-- src/components/ui/AppLoader/index.js | 9 +++- src/containers/auth/AuthLayoutContainer.js | 13 +++++- src/i18n/locales/defaultMessages.json | 32 +++++++------- .../settings/navigation/SettingsNavigation.json | 32 +++++++------- src/stores/UserStore.js | 2 + 6 files changed, 99 insertions(+), 39 deletions(-) diff --git a/src/components/settings/navigation/SettingsNavigation.js b/src/components/settings/navigation/SettingsNavigation.js index 2711bc107..49e73e569 100644 --- a/src/components/settings/navigation/SettingsNavigation.js +++ b/src/components/settings/navigation/SettingsNavigation.js @@ -3,10 +3,13 @@ import PropTypes from 'prop-types'; import { defineMessages, intlShape } from 'react-intl'; import { inject, observer } from 'mobx-react'; import { ProBadge } from '@meetfranz/ui'; +import { RouterStore } from 'mobx-react-router'; +import { LOCAL_SERVER, LIVE_API } from '../../../config'; import Link from '../../ui/Link'; import { workspaceStore } from '../../../features/workspaces'; import UIStore from '../../../stores/UIStore'; +import SettingsStore from '../../../stores/SettingsStore'; import UserStore from '../../../stores/UserStore'; import { serviceLimitStore } from '../../../features/serviceLimit'; @@ -45,11 +48,18 @@ const messages = defineMessages({ }, }); -export default @inject('stores') @observer class SettingsNavigation extends Component { +export default @inject('stores', 'actions') @observer class SettingsNavigation extends Component { static propTypes = { stores: PropTypes.shape({ ui: PropTypes.instanceOf(UIStore).isRequired, user: PropTypes.instanceOf(UserStore).isRequired, + settings: PropTypes.instanceOf(SettingsStore).isRequired, + router: PropTypes.instanceOf(RouterStore).isRequired, + }).isRequired, + actions: PropTypes.shape({ + settings: PropTypes.shape({ + update: PropTypes.func.isRequired, + }).isRequired, }).isRequired, serviceCount: PropTypes.number.isRequired, workspaceCount: PropTypes.number.isRequired, @@ -59,12 +69,42 @@ export default @inject('stores') @observer class SettingsNavigation extends Comp intl: intlShape, }; + handleLoginLogout() { + const isLoggedIn = Boolean(localStorage.getItem('authToken')); + const isUsingWithoutAccount = this.props.stores.settings.app.server === LOCAL_SERVER; + + if (isLoggedIn) { + // Remove current auth token + localStorage.removeItem('authToken'); + + if (isUsingWithoutAccount) { + // Reset server back to Ferdi API + this.props.actions.settings.update({ + type: 'app', + data: { + server: LIVE_API, + }, + }); + } + } + + this.props.stores.user.isLoggingOut = true; + this.props.stores.router.push(isLoggedIn ? '/auth/logout' : '/auth/welcome'); + + if (isLoggedIn) { + // Reload Ferdi, otherwise many settings won't sync correctly with the server + // after logging into another account + window.location.reload(); + } + } + render() { const { serviceCount, workspaceCount, stores } = this.props; const { isDarkThemeActive } = stores.ui; const { router, user } = stores; const { intl } = this.context; const isLoggedIn = Boolean(localStorage.getItem('authToken')); + const isUsingWithoutAccount = stores.settings.app.server === LOCAL_SERVER; return (
@@ -136,12 +176,14 @@ export default @inject('stores') @observer class SettingsNavigation extends Comp {intl.formatMessage(messages.supportFerdi)} - - { isLoggedIn ? intl.formatMessage(messages.logout) : 'Login'} - + { isLoggedIn && !isUsingWithoutAccount ? intl.formatMessage(messages.logout) : 'Login'} +
); } diff --git a/src/components/ui/AppLoader/index.js b/src/components/ui/AppLoader/index.js index 1fd247d17..a7f6f4545 100644 --- a/src/components/ui/AppLoader/index.js +++ b/src/components/ui/AppLoader/index.js @@ -22,8 +22,13 @@ export default @injectSheet(styles) @withTheme class AppLoader extends Component static propTypes = { classes: PropTypes.object.isRequired, theme: PropTypes.object.isRequired, + texts: PropTypes.array, }; + static defaultProps = { + texts: textList, + } + state = { step: 0, }; @@ -43,7 +48,7 @@ export default @injectSheet(styles) @withTheme class AppLoader extends Component } render() { - const { classes, theme } = this.props; + const { classes, theme, texts } = this.props; const { step } = this.state; return ( @@ -52,7 +57,7 @@ export default @injectSheet(styles) @withTheme class AppLoader extends Component className={classes.component} spinnerColor={theme.colorAppLoaderSpinner} > - {textList.map((text, i) => ( + {texts.map((text, i) => ( + + + ); + } + return (