2020-09-21 06:31:53 +02:00
|
|
|
import React from 'react';
|
2020-09-07 10:22:16 +02:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2020-08-06 08:03:07 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import { useRouter } from 'next/router';
|
2020-09-21 06:31:53 +02:00
|
|
|
import MenuButton from 'components/common/MenuButton';
|
|
|
|
import Icon from 'components/common/Icon';
|
2020-08-06 08:03:07 +02:00
|
|
|
import User from 'assets/user.svg';
|
|
|
|
import Chevron from 'assets/chevron-down.svg';
|
|
|
|
import styles from './UserButton.module.css';
|
2022-01-23 09:32:17 +01:00
|
|
|
import { removeItem } from 'lib/web';
|
|
|
|
import { AUTH_TOKEN } from 'lib/constants';
|
2020-08-06 08:03:07 +02:00
|
|
|
|
|
|
|
export default function UserButton() {
|
|
|
|
const user = useSelector(state => state.user);
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const menuOptions = [
|
|
|
|
{
|
|
|
|
label: (
|
2020-09-07 10:22:16 +02:00
|
|
|
<FormattedMessage
|
|
|
|
id="label.logged-in-as"
|
|
|
|
defaultMessage="Logged in as {username}"
|
|
|
|
values={{ username: <b>{user.username}</b> }}
|
|
|
|
/>
|
2020-08-06 08:03:07 +02:00
|
|
|
),
|
|
|
|
value: 'username',
|
|
|
|
className: styles.username,
|
|
|
|
},
|
2020-09-17 06:55:32 +02:00
|
|
|
{ label: <FormattedMessage id="label.profile" defaultMessage="Profile" />, value: 'profile' },
|
2020-09-07 10:22:16 +02:00
|
|
|
{ label: <FormattedMessage id="label.logout" defaultMessage="Logout" />, value: 'logout' },
|
2020-08-06 08:03:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
function handleSelect(value) {
|
2020-08-09 08:48:43 +02:00
|
|
|
if (value === 'logout') {
|
2022-01-23 09:32:17 +01:00
|
|
|
removeItem(AUTH_TOKEN);
|
|
|
|
router.push('/login');
|
2020-09-17 06:55:32 +02:00
|
|
|
} else if (value === 'profile') {
|
|
|
|
router.push('/settings/profile');
|
2020-08-06 08:03:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-09-21 06:31:53 +02:00
|
|
|
<MenuButton
|
|
|
|
icon={<Icon icon={<User />} size="large" />}
|
|
|
|
value={<Icon icon={<Chevron />} size="small" />}
|
|
|
|
options={menuOptions}
|
|
|
|
onSelect={handleSelect}
|
|
|
|
/>
|
2020-08-06 08:03:07 +02:00
|
|
|
);
|
|
|
|
}
|