Newer
Older
import React from 'react'
import { get } from 'lodash'
import { Icon } from '@pubsweet/ui'
import styled from 'styled-components'
import { th } from '@pubsweet/ui-toolkit'
import {
compose,
withProps,
withHandlers,
setDisplayName,
withStateHandlers,
} from 'recompose'
const AppBarMenu = ({
goTo,
logout,
expanded,
username,
toggleMenu,
<Root>
<User onClick={toggleMenu}>
<Text>{username}</Text>
<Icon secondary size={2}>
{expanded ? 'chevron-up' : 'chevron-down'}
</Icon>
</User>
{expanded && (
<Dropdown>
<DropdownOption onClick={goTo('/profile')}>My profile</DropdownOption>
{currentUser.user.admin && (
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<DropdownOption onClick={goTo('/admin')}>
Admin dashboard
</DropdownOption>
)}
<DropdownOption onClick={logout}>Logout</DropdownOption>
</Dropdown>
)}
{expanded && <ToggleOverlay onClick={toggleMenu} />}
</Root>
) : null
export default compose(
withStateHandlers(
{ expanded: false },
{ toggleMenu: ({ expanded }) => () => ({ expanded: !expanded }) },
),
withProps(({ currentUser }) => ({
username: get(
currentUser,
'user.firstName',
get(currentUser, 'user.username', 'User'),
),
})),
withHandlers({
goTo: ({ toggleMenu, goTo }) => path => () => {
toggleMenu()
goTo(path)
},
logout: ({ logout, toggleMenu }) => () => {
toggleMenu()
logout()
},
}),
setDisplayName('AppBarMenu'),
)(AppBarMenu)
// #region styles
const User = styled.div`
align-items: center;
display: flex;
cursor: pointer;
`
const Dropdown = styled.div`
background-color: ${th('appBar.colorBackground')};
border-radius: ${th('borderRadius')};
box-shadow: ${th('boxShadow')};
position: absolute;
top: calc(${th('gridUnit')} * 4);
width: calc(${th('gridUnit')} * 18);
z-index: 10;
`
const DropdownOption = styled.div`
align-items: center;
color: ${th('colorText')};
cursor: pointer;
display: flex;
justify-content: flex-start;
font-family: ${th('fontReading')};
font-size: ${th('fontSizeBase')};
line-height: ${th('lineHeightBase')};
height: calc(${th('gridUnit')} * 4);
padding: ${th('gridUnit')};
&:hover {
background-color: ${th('menu.optionBackground')};
}
`
const Root = styled.div`
align-items: center;
display: flex;
justify-content: flex-end;
margin-left: ${th('gridUnit')};
position: relative;
`
const ToggleOverlay = styled.div`
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
`
// #endregion