import { Component } from 'react'; import PropTypes from 'prop-types'; import { defineMessages, injectIntl } from 'react-intl'; import { inject, observer } from 'mobx-react'; import { RouterStore } from 'mobx-react-router'; import { LOCAL_SERVER, LIVE_FERDI_API, LIVE_FRANZ_API } from '../../../config'; import Link from '../../ui/Link'; import UIStore from '../../../stores/UIStore'; import SettingsStore from '../../../stores/SettingsStore'; import UserStore from '../../../stores/UserStore'; import globalMessages from '../../../i18n/globalMessages'; const messages = defineMessages({ availableServices: { id: 'settings.navigation.availableServices', defaultMessage: 'Available services', }, yourServices: { id: 'settings.navigation.yourServices', defaultMessage: 'Your services', }, yourWorkspaces: { id: 'settings.navigation.yourWorkspaces', defaultMessage: 'Your workspaces', }, account: { id: 'settings.navigation.account', defaultMessage: 'Account', }, team: { id: 'settings.navigation.team', defaultMessage: 'Manage Team', }, supportFerdi: { id: 'settings.navigation.supportFerdi', defaultMessage: 'About Ferdi', }, logout: { id: 'settings.navigation.logout', defaultMessage: 'Logout', }, }); @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.instanceOf(SettingsStore).isRequired, }).isRequired, serviceCount: PropTypes.number.isRequired, workspaceCount: PropTypes.number.isRequired, }; 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_FERDI_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 { intl } = this.props; const isLoggedIn = Boolean(localStorage.getItem('authToken')); const isUsingWithoutAccount = stores.settings.app.server === LOCAL_SERVER; const isUsingFranzServer = stores.settings.app.server === LIVE_FRANZ_API; return (
{intl.formatMessage(messages.availableServices)} {intl.formatMessage(messages.yourServices)}{' '} {serviceCount} {intl.formatMessage(messages.yourWorkspaces)}{' '} {workspaceCount} {!isUsingWithoutAccount && ( {intl.formatMessage(messages.account)} )} {isUsingFranzServer && ( {intl.formatMessage(messages.team)} )} {intl.formatMessage(globalMessages.settings)} {intl.formatMessage(messages.supportFerdi)}
); } } export default injectIntl(SettingsNavigation);