import React from 'react'; import { connect } from 'react-redux'; import clsx from 'clsx'; import { Link } from 'react-router-dom'; import { FormattedMessage as Message } from 'react-intl'; import loader from 'app/services/loader'; import { SKIN_DARK, COLOR_WHITE, Skin } from 'app/components/ui'; import { Button } from 'app/components/ui/form'; import { authenticate, revoke } from 'app/components/accounts/actions'; import { getActiveAccount, Account } from 'app/components/accounts/reducer'; import { RootState } from 'app/reducers'; import styles from './accountSwitcher.scss'; import messages from './AccountSwitcher.intl.json'; interface Props { switchAccount: (account: Account) => Promise; removeAccount: (account: Account) => Promise; // called after each action performed onAfterAction: () => void; // called after switching an account. The active account will be passed as arg onSwitch: (account: Account) => void; accounts: RootState['accounts']; skin: Skin; // whether active account should be expanded and shown on the top highlightActiveAccount: boolean; // whether to show logout icon near each account allowLogout: boolean; // whether to show add account button allowAdd: boolean; } export class AccountSwitcher extends React.Component { static defaultProps: Partial = { skin: SKIN_DARK, highlightActiveAccount: true, allowLogout: true, allowAdd: true, onAfterAction() {}, onSwitch() {}, }; render() { const { accounts, skin, allowAdd, allowLogout, highlightActiveAccount, } = this.props; const activeAccount = getActiveAccount({ accounts }); if (!activeAccount) { return null; } let { available } = accounts; if (highlightActiveAccount) { available = available.filter(account => account.id !== activeAccount.id); } return (
{highlightActiveAccount ? (
{activeAccount.username}
{activeAccount.email}
) : null} {available.map((account, index) => (
{allowLogout ? (
) : (
)}
{account.username}
{account.email}
))} {allowAdd ? (