2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2018-02-28 02:47:31 +05:30
|
|
|
import { connect } from 'react-redux';
|
2019-12-08 01:13:08 +05:30
|
|
|
import clsx from 'clsx';
|
2017-05-26 00:41:57 +05:30
|
|
|
import { Link } from 'react-router-dom';
|
2016-11-06 01:53:56 +05:30
|
|
|
import { FormattedMessage as Message } from 'react-intl';
|
2020-01-18 02:07:52 +05:30
|
|
|
import * as loader from 'app/services/loader';
|
2019-12-08 00:32:00 +05:30
|
|
|
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';
|
2016-11-06 01:53:56 +05:30
|
|
|
|
2016-11-05 16:27:01 +05:30
|
|
|
import styles from './accountSwitcher.scss';
|
2016-11-06 01:53:56 +05:30
|
|
|
import messages from './AccountSwitcher.intl.json';
|
2016-11-05 16:27:01 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
interface Props {
|
|
|
|
switchAccount: (account: Account) => Promise<Account>;
|
|
|
|
removeAccount: (account: Account) => Promise<void>;
|
|
|
|
// 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;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export class AccountSwitcher extends React.Component<Props> {
|
|
|
|
static defaultProps: Partial<Props> = {
|
2019-11-27 14:33:32 +05:30
|
|
|
skin: SKIN_DARK,
|
|
|
|
highlightActiveAccount: true,
|
|
|
|
allowLogout: true,
|
|
|
|
allowAdd: true,
|
|
|
|
onAfterAction() {},
|
|
|
|
onSwitch() {},
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
accounts,
|
|
|
|
skin,
|
|
|
|
allowAdd,
|
|
|
|
allowLogout,
|
|
|
|
highlightActiveAccount,
|
|
|
|
} = this.props;
|
|
|
|
const activeAccount = getActiveAccount({ accounts });
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
if (!activeAccount) {
|
2019-12-08 03:23:22 +05:30
|
|
|
return null;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
let { available } = accounts;
|
|
|
|
|
|
|
|
if (highlightActiveAccount) {
|
2020-05-20 22:05:52 +05:30
|
|
|
available = available.filter(
|
|
|
|
(account) => account.id !== activeAccount.id,
|
|
|
|
);
|
2016-11-05 16:27:01 +05:30
|
|
|
}
|
2016-11-13 02:01:44 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return (
|
|
|
|
<div
|
2019-12-08 01:13:08 +05:30
|
|
|
className={clsx(
|
2019-11-27 14:33:32 +05:30
|
|
|
styles.accountSwitcher,
|
|
|
|
styles[`${skin}AccountSwitcher`],
|
|
|
|
)}
|
2019-12-28 14:58:25 +05:30
|
|
|
data-testid="account-switcher"
|
2019-11-27 14:33:32 +05:30
|
|
|
>
|
2019-12-28 14:58:25 +05:30
|
|
|
{highlightActiveAccount && (
|
|
|
|
<div className={styles.item} data-testid="active-account">
|
2019-11-27 14:33:32 +05:30
|
|
|
<div
|
2019-12-08 01:13:08 +05:30
|
|
|
className={clsx(
|
2019-11-27 14:33:32 +05:30
|
|
|
styles.accountIcon,
|
|
|
|
styles.activeAccountIcon,
|
|
|
|
styles.accountIcon1,
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<div className={styles.activeAccountInfo}>
|
|
|
|
<div className={styles.activeAccountUsername}>
|
|
|
|
{activeAccount.username}
|
|
|
|
</div>
|
|
|
|
<div
|
2019-12-08 01:13:08 +05:30
|
|
|
className={clsx(styles.accountEmail, styles.activeAccountEmail)}
|
2019-11-27 14:33:32 +05:30
|
|
|
>
|
|
|
|
{activeAccount.email}
|
|
|
|
</div>
|
|
|
|
<div className={styles.links}>
|
|
|
|
<div className={styles.link}>
|
|
|
|
<a
|
|
|
|
href={`http://ely.by/u${activeAccount.id}`}
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
<Message {...messages.goToEly} />
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
<div className={styles.link}>
|
|
|
|
<a
|
|
|
|
className={styles.link}
|
2019-12-28 14:58:25 +05:30
|
|
|
data-testid="logout-account"
|
2019-11-27 14:33:32 +05:30
|
|
|
onClick={this.onRemove(activeAccount)}
|
|
|
|
href="#"
|
|
|
|
>
|
|
|
|
<Message {...messages.logout} />
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-12-28 14:58:25 +05:30
|
|
|
)}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
{available.map((account, index) => (
|
|
|
|
<div
|
2019-12-08 01:13:08 +05:30
|
|
|
className={clsx(styles.item, styles.accountSwitchItem)}
|
2019-11-27 14:33:32 +05:30
|
|
|
key={account.id}
|
2019-12-28 14:58:25 +05:30
|
|
|
data-e2e-account-id={account.id}
|
2019-11-27 14:33:32 +05:30
|
|
|
onClick={this.onSwitch(account)}
|
|
|
|
>
|
|
|
|
<div
|
2019-12-08 01:13:08 +05:30
|
|
|
className={clsx(
|
2019-11-27 14:33:32 +05:30
|
|
|
styles.accountIcon,
|
|
|
|
styles[
|
|
|
|
`accountIcon${(index % 7) + (highlightActiveAccount ? 2 : 1)}`
|
|
|
|
],
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{allowLogout ? (
|
|
|
|
<div
|
|
|
|
className={styles.logoutIcon}
|
2019-12-28 14:58:25 +05:30
|
|
|
data-testid="logout-account"
|
2019-11-27 14:33:32 +05:30
|
|
|
onClick={this.onRemove(account)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className={styles.nextIcon} />
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className={styles.accountInfo}>
|
|
|
|
<div className={styles.accountUsername}>{account.username}</div>
|
|
|
|
<div className={styles.accountEmail}>{account.email}</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
{allowAdd ? (
|
|
|
|
<Link to="/login" onClick={this.props.onAfterAction}>
|
|
|
|
<Button
|
|
|
|
color={COLOR_WHITE}
|
2019-12-28 14:58:25 +05:30
|
|
|
data-testid="add-account"
|
2019-11-27 14:33:32 +05:30
|
|
|
block
|
|
|
|
small
|
|
|
|
className={styles.addAccount}
|
|
|
|
label={
|
|
|
|
<Message {...messages.addAccount}>
|
2020-05-20 22:05:52 +05:30
|
|
|
{(message) => (
|
2019-11-27 14:33:32 +05:30
|
|
|
<span>
|
|
|
|
<div className={styles.addIcon} />
|
|
|
|
{message}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</Message>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Link>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:29:06 +05:30
|
|
|
onSwitch = (account: Account) => (event: React.MouseEvent<any>) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
loader.show();
|
|
|
|
|
|
|
|
this.props
|
|
|
|
.switchAccount(account)
|
|
|
|
.finally(() => this.props.onAfterAction())
|
|
|
|
.then(() => this.props.onSwitch(account))
|
|
|
|
// we won't sent any logs to sentry, because an error should be already
|
|
|
|
// handled by external logic
|
2020-05-20 22:05:52 +05:30
|
|
|
.catch((error) => console.warn('Error switching account', { error }))
|
2019-11-27 14:33:32 +05:30
|
|
|
.finally(() => loader.hide());
|
|
|
|
};
|
|
|
|
|
2020-05-20 22:29:06 +05:30
|
|
|
onRemove = (account: Account) => (event: React.MouseEvent<any>) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
this.props.removeAccount(account).then(() => this.props.onAfterAction());
|
|
|
|
};
|
2016-11-05 16:27:01 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export default connect(
|
2019-12-07 16:58:52 +05:30
|
|
|
({ accounts }: RootState) => ({
|
2016-11-19 15:49:15 +05:30
|
|
|
accounts,
|
2019-11-27 14:33:32 +05:30
|
|
|
}),
|
|
|
|
{
|
2016-11-13 02:01:44 +05:30
|
|
|
switchAccount: authenticate,
|
2019-11-27 14:33:32 +05:30
|
|
|
removeAccount: revoke,
|
|
|
|
},
|
|
|
|
)(AccountSwitcher);
|