2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2019-12-08 01:13:08 +05:30
|
|
|
import clsx from 'clsx';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { AccountSwitcher } from 'app/components/accounts';
|
2016-11-05 16:27:01 +05:30
|
|
|
|
|
|
|
import styles from './loggedInPanel.scss';
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export default class LoggedInPanel extends React.Component<
|
2019-11-27 14:33:32 +05:30
|
|
|
{
|
2019-12-07 16:58:52 +05:30
|
|
|
username: string;
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
{
|
2019-12-07 16:58:52 +05:30
|
|
|
isAccountSwitcherActive: boolean;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
> {
|
|
|
|
state = {
|
|
|
|
isAccountSwitcherActive: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
_isMounted: boolean = false;
|
2019-12-07 16:58:52 +05:30
|
|
|
el: HTMLElement | null;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
if (window.document) {
|
|
|
|
window.document.addEventListener('click', this.onBodyClick);
|
2016-11-11 13:09:01 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this._isMounted = true;
|
|
|
|
}
|
2017-07-13 00:00:05 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentWillUnmount() {
|
|
|
|
if (window.document) {
|
|
|
|
window.document.removeEventListener('click', this.onBodyClick);
|
2016-11-11 13:09:01 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this._isMounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { username } = this.props;
|
|
|
|
const { isAccountSwitcherActive } = this.state;
|
|
|
|
|
|
|
|
return (
|
2019-12-08 01:13:08 +05:30
|
|
|
<div ref={el => (this.el = el)} className={clsx(styles.loggedInPanel)}>
|
2019-11-27 14:33:32 +05:30
|
|
|
<div
|
2019-12-08 01:13:08 +05:30
|
|
|
className={clsx(styles.activeAccount, {
|
2019-11-27 14:33:32 +05:30
|
|
|
[styles.activeAccountExpanded]: isAccountSwitcherActive,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<button
|
|
|
|
className={styles.activeAccountButton}
|
|
|
|
onClick={this.onExpandAccountSwitcher}
|
|
|
|
>
|
|
|
|
<span className={styles.userIcon} />
|
|
|
|
<span className={styles.userName}>{username}</span>
|
|
|
|
<span className={styles.expandIcon} />
|
|
|
|
</button>
|
|
|
|
|
2019-12-08 01:13:08 +05:30
|
|
|
<div className={clsx(styles.accountSwitcherContainer)}>
|
2019-11-27 14:33:32 +05:30
|
|
|
<AccountSwitcher
|
|
|
|
skin="light"
|
|
|
|
onAfterAction={this.onToggleAccountSwitcher}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-11-11 13:09:01 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
toggleAccountSwitcher = () =>
|
|
|
|
this._isMounted &&
|
|
|
|
this.setState({
|
|
|
|
isAccountSwitcherActive: !this.state.isAccountSwitcherActive,
|
2016-11-13 02:01:44 +05:30
|
|
|
});
|
2016-11-11 13:09:01 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onToggleAccountSwitcher = () => {
|
|
|
|
this.toggleAccountSwitcher();
|
|
|
|
};
|
2017-07-13 00:00:05 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
onExpandAccountSwitcher = (event: React.MouseEvent<HTMLButtonElement>) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
event.preventDefault();
|
2016-11-11 13:09:01 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.toggleAccountSwitcher();
|
|
|
|
};
|
2016-11-11 13:09:01 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onBodyClick = createOnOutsideComponentClickHandler(
|
|
|
|
() => this.el,
|
|
|
|
() => this.state.isAccountSwitcherActive && this._isMounted,
|
|
|
|
() => this.toggleAccountSwitcher(),
|
|
|
|
);
|
2016-11-11 13:09:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an event handling function to handle clicks outside the component
|
|
|
|
*
|
|
|
|
* The handler will check if current click was outside container el and if so
|
|
|
|
* and component isActive, it will call the callback
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @param {Function} getEl - the function, that returns reference to container el
|
|
|
|
* @param {Function} isActive - whether the component is active and callback may be called
|
|
|
|
* @param {Function} callback - the callback to call, when there was a click outside el
|
2017-04-19 23:36:08 +05:30
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Function}
|
2016-11-11 13:09:01 +05:30
|
|
|
*/
|
2017-07-13 00:00:05 +05:30
|
|
|
function createOnOutsideComponentClickHandler(
|
2019-12-07 16:58:52 +05:30
|
|
|
getEl: () => HTMLElement | null,
|
2019-11-27 14:33:32 +05:30
|
|
|
isActive: () => boolean,
|
2019-12-07 16:58:52 +05:30
|
|
|
callback: () => void,
|
2017-07-13 00:00:05 +05:30
|
|
|
) {
|
2019-11-27 14:33:32 +05:30
|
|
|
// TODO: we have the same logic in LangMenu
|
|
|
|
// Probably we should decouple this into some helper function
|
|
|
|
// TODO: the name of function may be better...
|
|
|
|
return (event: { target: HTMLElement } & MouseEvent) => {
|
|
|
|
const el = getEl();
|
|
|
|
|
|
|
|
if (isActive() && el) {
|
|
|
|
if (!el.contains(event.target) && el !== event.target) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// add a small delay for the case someone have alredy called toggle
|
|
|
|
setTimeout(() => isActive() && callback(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-11-05 16:27:01 +05:30
|
|
|
}
|