@@ -5,12 +5,32 @@ import api from '@/_api'
5
5
6
6
import BasePageContainer from '@/_common/BasePageContainer'
7
7
import BasePageToolbar from '@/_common/BasePageToolbar'
8
- import Grid from "@material-ui/core/Grid" ;
8
+ import { Save as SaveIcon } from '@material-ui/icons/'
9
+ import {
10
+ Input ,
11
+ Select ,
12
+ MenuItem ,
13
+ Paper ,
14
+ Button ,
15
+ FormControl ,
16
+ InputLabel ,
17
+ makeStyles ,
18
+ Grid
19
+ } from '@material-ui/core'
9
20
10
21
const UserEditor = ( props ) => {
22
+ const classes = useStyles ( )
11
23
12
24
const { userId } = props
13
- const [ user , setUser ] = useState ( { } )
25
+ const [ user , setUser ] = useState ( {
26
+ avatarUrl : '' ,
27
+ email : '' ,
28
+ firstName : '' ,
29
+ globalRole : '' ,
30
+ lastName : '' ,
31
+ userToOrganizations : [ { } ] ,
32
+ username : ''
33
+ } )
14
34
15
35
useEffect ( ( ) => {
16
36
if ( ! userId ) {
@@ -27,11 +47,78 @@ const UserEditor = (props) => {
27
47
fetchUser ( )
28
48
} , [ userId ] )
29
49
50
+ const onChangeHandler = ( e ) => setUser ( {
51
+ ...user ,
52
+ [ e . target . name ] : e . target . value
53
+ } )
54
+
55
+ const setGlobalRole = ( e ) => setUser ( {
56
+ ...user ,
57
+ globalRole : e . target . value
58
+ } )
59
+
60
+ const handleSubmit = async ( e ) => {
61
+ e . preventDefault ( ) ;
62
+ try {
63
+ if ( userId ) {
64
+ await api . users . update ( userId , user )
65
+ } else {
66
+ await api . users . create ( user )
67
+ }
68
+ } catch ( e ) {
69
+ console . log ( e )
70
+ }
71
+ }
72
+
30
73
return (
31
74
< BasePageContainer >
32
75
< BasePageToolbar title = { 'Edit user' } > </ BasePageToolbar >
33
- < Grid container spacing = { 3 } >
34
- UserEditor
76
+ < Grid container xs = { 12 } >
77
+ < Grid item xs = { 6 } >
78
+ < Paper >
79
+ < form onSubmit = { handleSubmit } >
80
+ < FormControl className = { classes . control } >
81
+ < InputLabel > First Name</ InputLabel >
82
+ < Input value = { user . firstName } name = "firstName" className = { classes . width } onChange = { e => onChangeHandler ( e ) } />
83
+ </ FormControl >
84
+ < FormControl className = { classes . control } >
85
+ < InputLabel > Last Name</ InputLabel >
86
+ < Input value = { user . lastName } name = "lastName" className = { classes . width } onChange = { e => onChangeHandler ( e ) } />
87
+ </ FormControl >
88
+ < FormControl className = { classes . control } >
89
+ < InputLabel > User Name</ InputLabel >
90
+ < Input value = { user . username } name = "username" className = { classes . width } onChange = { e => onChangeHandler ( e ) } />
91
+ </ FormControl >
92
+ < FormControl className = { classes . control } >
93
+ < InputLabel > Email</ InputLabel >
94
+ < Input value = { user . email } name = "email" className = { classes . width } onChange = { e => onChangeHandler ( e ) } />
95
+ </ FormControl >
96
+ < FormControl className = { classes . control } >
97
+ < InputLabel > Global Role</ InputLabel >
98
+ < Select
99
+ labelId = "Set Global Role"
100
+ id = "globalRole"
101
+ value = { user . globalRole }
102
+ onChange = { setGlobalRole }
103
+ className = { classes . width }
104
+ >
105
+ < MenuItem value = { user . globalRole } > { user . globalRole } </ MenuItem >
106
+ < MenuItem value = 'user' > user</ MenuItem >
107
+ < MenuItem value = 'support' > support</ MenuItem >
108
+ </ Select >
109
+ </ FormControl >
110
+ < Button
111
+ variant = "contained"
112
+ color = "primary"
113
+ type = "submit"
114
+ className = { `${ classes . margin } ${ classes . width } ` }
115
+ startIcon = { < SaveIcon /> }
116
+ >
117
+ Edit User
118
+ </ Button >
119
+ </ form >
120
+ </ Paper >
121
+ </ Grid >
35
122
</ Grid >
36
123
</ BasePageContainer >
37
124
)
@@ -41,4 +128,16 @@ UserEditor.propTypes = {
41
128
userId : PropTypes . number
42
129
}
43
130
131
+ const useStyles = makeStyles ( theme => ( {
132
+ control : {
133
+ display : 'block'
134
+ } ,
135
+ margin : {
136
+ marginTop : 16
137
+ } ,
138
+ width : {
139
+ minWidth : 200
140
+ }
141
+ } ) )
142
+
44
143
export default UserEditor
0 commit comments