/
Header.js
172 lines (160 loc) · 5.49 KB
/
Header.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import PropTypes from 'prop-types';
import React, { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router-dom';
import { selectIsSaving, selectLastSaved } from '../reducers/saving';
import { getIsAdmin } from '../reducers/user.selector';
import { t } from '../i18n';
import DashboardButton from './DashboardButton';
import SaveMessage from './SaveMessage';
import Icon, {
Check,
faChevronDown,
faChevronLeft,
faEdit,
faSignOutAlt,
Spinner
} from './Icons';
class Header extends Component {
constructor(props) {
super(props);
this.state = {
ariaExpanded: props.ariaExpanded
};
this.node = React.createRef();
}
handleOutsideClick = e => {
if (this.node.current.contains(e.target)) {
return;
}
this.setState({ ariaExpanded: false });
// remove the global click handler when the dropdown is collapsed
document.removeEventListener('click', this.handleOutsideClick);
};
toggleDropdown = () => {
this.setState(prev => {
if (!prev.ariaExpanded) {
// add global click handler when the dropdown is expanded
document.addEventListener('click', this.handleOutsideClick);
} else {
// remove the global click handler when the dropdown is collapsed
document.removeEventListener('click', this.handleOutsideClick);
}
return { ariaExpanded: !prev.ariaExpanded };
});
};
render() {
const {
authenticated,
currentUser,
isAdmin,
isSaving,
lastSaved,
showSiteTitle
} = this.props;
const { ariaExpanded } = this.state;
return (
<header ref={this.node}>
<div className="ds-l-container">
<div className="ds-l-row">
<div className="ds-l-col--12 ds-l-md-col--3 site-title">
{showSiteTitle || !authenticated ? (
<DashboardButton>{t('titleBasic')}</DashboardButton>
) : (
<DashboardButton>
<Icon icon={faChevronLeft} size="sm" />
{isAdmin
? 'Admin Dashboard'
: `${currentUser.state.id.toUpperCase()} APD Home`}
</DashboardButton>
)}
</div>
{authenticated && (
<Fragment>
<div className="ds-l-col--12 ds-l-md-col--5">
{!showSiteTitle && !isAdmin && (
<span>
{isSaving ? (
<span>
<Spinner spin /> Saving...
</span>
) : (
<span>
<Check /> <SaveMessage lastSaved={lastSaved} />
</span>
)}
</span>
)}
</div>
<div className="ds-l-col--12 ds-l-md-col--4">
<ul className="nav--dropdown">
<li>
<button
type="button"
className="nav--dropdown__trigger ds-c-button ds-c-button--small ds-c-button--transparent"
onClick={this.toggleDropdown}
aria-expanded={ariaExpanded ? 'true' : 'false'}
aria-haspopup="true"
aria-label={`Logged in as ${currentUser.username}. Click to manage your account.`}
>
{currentUser ? currentUser.username : 'Your account'}
<Icon icon={faChevronDown} style={{ width: '8px' }} />
</button>
<ul className="nav--submenu" aria-hidden={!ariaExpanded}>
<li>
<Link
to="/me"
onClick={this.toggleDropdown}
className="nav--dropdown__action"
>
<Icon icon={faEdit} style={{ width: '14px' }} />
Manage account
</Link>
</li>
<li>
<Link
to="/logout"
onClick={this.toggleDropdown}
className="nav--dropdown__action"
>
<Icon
icon={faSignOutAlt}
style={{ width: '14px' }}
/>
{t('logout')}
</Link>
</li>
</ul>
</li>
</ul>
</div>
</Fragment>
)}
</div>
</div>
</header>
);
}
}
Header.propTypes = {
ariaExpanded: PropTypes.bool,
authenticated: PropTypes.bool.isRequired,
currentUser: PropTypes.object,
isAdmin: PropTypes.bool.isRequired,
isSaving: PropTypes.bool.isRequired,
lastSaved: PropTypes.string.isRequired,
showSiteTitle: PropTypes.bool.isRequired
};
Header.defaultProps = {
ariaExpanded: false,
currentUser: null
};
const mapStateToProps = state => ({
authenticated: state.auth.authenticated,
currentUser: state.auth.user,
isAdmin: getIsAdmin(state),
isSaving: selectIsSaving(state),
lastSaved: selectLastSaved(state)
});
export default connect(mapStateToProps)(Header);
export { Header as plain, mapStateToProps };