import React from 'react'; import { connect } from 'react-redux'; import classNames from 'classnames'; import { Link } from 'react-router-dom'; import { FormattedMessage as Message } from 'react-intl'; import loader from 'services/loader'; import { SKIN_DARK, COLOR_WHITE, Skin } from 'components/ui'; import { Button } from 'components/ui/form'; import { authenticate, revoke } from 'components/accounts/actions'; import { getActiveAccount, Account } from 'components/accounts/reducer'; import { RootState } from '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) { throw new Error('Can not find active account'); } 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 ? (