1
- import React from 'react' ;
1
+ import React , { useContext , useState , useEffect } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
4
4
import {
@@ -13,86 +13,87 @@ import { Link } from 'react-router-dom';
13
13
import { AuthService } from '../services/auth' ;
14
14
import { IbutsuContext } from '../services/context' ;
15
15
16
- export class UserDropdown extends React . Component {
17
- static contextType = IbutsuContext ;
18
- static propTypes = {
19
- eventEmitter : PropTypes . object
20
- }
16
+ const UserDropdown = ( props ) => {
17
+ const context = useContext ( IbutsuContext ) ;
18
+ // TODO:
19
+ // static propTypes = {
20
+ // eventEmitter: PropTypes.object
21
+ // }
21
22
22
- constructor ( props ) {
23
- super ( props ) ;
24
- this . eventEmitter = props . eventEmitter ;
25
- this . state = {
26
- displayName : 'User' ,
27
- isDropdownOpen : false ,
28
- isSuperAdmin : false
29
- } ;
30
- this . eventEmitter . on ( 'updateUserName' , ( userName ) => {
31
- this . updateUserName ( userName ) ;
32
- } ) ;
33
- }
23
+ // const eventEmitter = props.eventEmitter;
24
+
25
+ const [ displayName , setDisplayName ] = useState ( 'User' ) ;
26
+ const [ isDropdownOpen , setIsDropdownOpen ] = useState ( false ) ;
27
+ const [ isSuperAdmin , setIsSuperAdmin ] = useState ( false ) ;
34
28
35
- updateUserName ( userName ) {
29
+
30
+ // TODO:
31
+ // this.eventEmitter.on('updateUserName', (userName) => {
32
+ // this.updateUserName(userName);
33
+ // })
34
+
35
+ function updateUserName ( userName ) {
36
36
// Update the user in the browser
37
37
const sessionUser = AuthService . getUser ( ) ;
38
38
sessionUser . name = userName ;
39
39
AuthService . setUser ( sessionUser ) ;
40
- this . setState ( { displayName : userName } ) ;
40
+ setDisplayName ( userName ) ;
41
41
}
42
42
43
- onDropdownToggle = ( ) => {
44
- this . setState ( { isDropdownOpen : ! this . state . isDropdownOpen } ) ;
45
- } ;
43
+ function onDropdownToggle ( ) {
44
+ setIsDropdownOpen ( ! isDropdownOpen ) ;
45
+ }
46
46
47
- onDropdownSelect = ( ) => {
48
- this . setState ( { isDropdownOpen : false } ) ;
49
- } ;
47
+ function onDropdownSelect ( ) {
48
+ setIsDropdownOpen ( false ) ;
49
+ }
50
50
51
- logout = ( ) => {
52
- const { setPrimaryObject } = this . context ;
53
- setPrimaryObject ( ) ;
51
+ function logout ( ) {
52
+ const { primaryObject } = context ;
54
53
AuthService . logout ( ) ;
55
54
window . location = '/' ;
56
55
}
57
56
58
- componentDidMount ( ) {
59
- AuthService . isSuperAdmin ( ) . then ( isSuperAdmin => this . setState ( { isSuperAdmin} ) ) ;
60
- this . setState ( {
61
- displayName : AuthService . getUser ( ) && ( AuthService . getUser ( ) . name || AuthService . getUser ( ) . email )
62
- } ) ;
63
- }
57
+ useEffect ( ( ) => {
58
+ AuthService . isSuperAdmin ( ) . then ( isSuperAdmin => setIsSuperAdmin ( isSuperAdmin ) ) ;
59
+ setDisplayName ( AuthService . getUser ( ) && ( AuthService . getUser ( ) . name || AuthService . getUser ( ) . email ) ) ;
60
+ } ) ;
64
61
65
- render ( ) {
66
- return (
67
- < Dropdown
68
- isOpen = { this . state . isDropdownOpen }
69
- onSelect = { this . onDropdownSelect }
70
- onOpenChange = { ( ) => this . setState ( { isDropdownOpen : false } ) }
71
- toggle = { toggleRef => (
72
- < MenuToggle
73
- ref = { toggleRef }
74
- onClick = { this . onDropdownToggle }
75
- isExpanded = { this . state . isDropdownOpen }
76
- icon = { < UserIcon /> }
77
- >
78
- { this . state . displayName }
79
- </ MenuToggle >
80
- ) }
81
- >
82
- < DropdownList >
83
- < DropdownItem key = "profile" >
84
- < Link to = "/profile/user" className = "pf-v5-c-menu__list-item" > Profile</ Link >
85
- </ DropdownItem >
86
- { ! ! this . state . isSuperAdmin &&
87
- < DropdownItem key = "admin" >
88
- < Link to = "/admin/home" className = "pf-v5-c-menu__list-item" > Administration</ Link >
89
- </ DropdownItem >
90
- }
91
- < DropdownItem key = "logout" onClick = { this . logout } >
92
- Logout
62
+ return (
63
+ < Dropdown
64
+ isOpen = { isDropdownOpen }
65
+ onSelect = { onDropdownSelect }
66
+ onOpenChange = { ( ) => setIsDropdownOpen ( false ) }
67
+ toggle = { toggleRef => (
68
+ < MenuToggle
69
+ ref = { toggleRef }
70
+ onClick = { onDropdownToggle }
71
+ isExpanded = { isDropdownOpen }
72
+ icon = { < UserIcon /> }
73
+ >
74
+ { displayName }
75
+ </ MenuToggle >
76
+ ) }
77
+ >
78
+ < DropdownList >
79
+ < DropdownItem key = "profile" >
80
+ < Link to = "/profile/user" className = "pf-v5-c-menu__list-item" > Profile</ Link >
81
+ </ DropdownItem >
82
+ { ! ! isSuperAdmin &&
83
+ < DropdownItem key = "admin" >
84
+ < Link to = "/admin/home" className = "pf-v5-c-menu__list-item" > Administration</ Link >
93
85
</ DropdownItem >
94
- </ DropdownList >
95
- </ Dropdown >
96
- ) ;
97
- }
86
+ }
87
+ < DropdownItem key = "logout" onClick = { logout } >
88
+ Logout
89
+ </ DropdownItem >
90
+ </ DropdownList >
91
+ </ Dropdown >
92
+ ) ;
98
93
}
94
+
95
+ UserDropdown . propTypes = {
96
+ // eventEmitter: PropTypes.object
97
+ } ;
98
+
99
+ export default UserDropdown ;
0 commit comments