| author | ymh <ymh.work@gmail.com> |
| Tue, 13 Nov 2018 16:46:15 +0100 | |
| changeset 172 | 4b780ebbedc6 |
| parent 171 | 03334a31130a |
| permissions | -rw-r--r-- |
| 148 | 1 |
import React, { Component } from 'react'; |
2 |
import ReactDOM from 'react-dom'; |
|
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
168
diff
changeset
|
3 |
import { Trans } from 'react-i18next'; |
| 148 | 4 |
|
5 |
export default class LoginNav extends Component { |
|
6 |
||
7 |
state = { |
|
8 |
showDropdown: false |
|
9 |
} |
|
10 |
||
11 |
componentWillMount() { |
|
12 |
document.addEventListener('click', this.handleClickOutside, false); |
|
13 |
} |
|
14 |
||
15 |
componentWillUnmount() { |
|
16 |
document.removeEventListener('click', this.handleClickOutside, false); |
|
17 |
} |
|
18 |
||
19 |
handleClickOutside = (e) => { |
|
| 151 | 20 |
e.preventDefault(); |
|
149
298d0373812e
Correct problem with "findDOMNode" is null
ymh <ymh.work@gmail.com>
parents:
148
diff
changeset
|
21 |
const currentNode = ReactDOM.findDOMNode(this); |
|
298d0373812e
Correct problem with "findDOMNode" is null
ymh <ymh.work@gmail.com>
parents:
148
diff
changeset
|
22 |
if(!currentNode.contains(e.target)) { |
| 148 | 23 |
this.hideDropDown(); |
24 |
} |
|
25 |
} |
|
26 |
||
27 |
onClickSettings = (e) => { |
|
28 |
e.preventDefault(); |
|
29 |
this.props.history.push('/settings'); |
|
30 |
} |
|
31 |
||
32 |
onClickLogin = (e) => { |
|
33 |
e.preventDefault(); |
|
34 |
this.props.history.push('/login'); |
|
35 |
} |
|
36 |
||
37 |
toggleShowDropdown = () => { |
|
38 |
this.setState({showDropdown: !this.state.showDropdown}); |
|
39 |
} |
|
40 |
||
41 |
hideDropDown = () => { |
|
42 |
this.setState({showDropdown: false}); |
|
43 |
} |
|
44 |
||
45 |
render() { |
|
46 |
||
47 |
const {isAuthenticated, currentUser, onLogout} = this.props; |
|
48 |
||
49 |
if (isAuthenticated) { |
|
50 |
return ( |
|
51 |
||
| 151 | 52 |
<li className={`nav-item dropdown ${this.state.showDropdown?'show':''}`}> |
| 172 | 53 |
<span className="nav-link dropdown-toggle username font-weight-bold" id="navbarDropdown" role="button" aria-haspopup="true" aria-expanded={this.state.showDropdown} onClick={this.toggleShowDropdown} onBlur={this.hideDropDown}> |
54 |
{ currentUser.username } |
|
55 |
<span className="caret"></span> |
|
56 |
</span> |
|
| 151 | 57 |
<div className={`dropdown-menu dropdown-menu-right bg-primary border-0 ${this.state.showDropdown?'show':''}`} aria-labelledby="navbarDropdown"> |
| 172 | 58 |
<button className="dropdown-item bg-primary text-secondary font-weight-bold text-capitalize" onClick={this.onClickSettings}><Trans i18nKey='common.parameters'>Paramètres</Trans></button> |
59 |
<button className="dropdown-item bg-primary text-secondary font-weight-bold text-capitalize" onClick={onLogout}><Trans i18nKey='common.disconnect'>Se déconnecter</Trans></button> |
|
| 151 | 60 |
</div> |
| 148 | 61 |
</li> |
62 |
); |
|
63 |
} else { |
|
64 |
return ( |
|
| 151 | 65 |
<li className="nav-item"> |
|
171
03334a31130a
Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents:
168
diff
changeset
|
66 |
<a className="nav-link text-capitalize" onClick={this.onClickLogin} href="/login"><Trans i18nKey='common.connect'>Se connecter</Trans></a> |
| 148 | 67 |
</li> |
68 |
); |
|
69 |
} |
|
70 |
} |
|
71 |
} |