1
1
import Box from "@material-ui/core/Box"
2
+ import { makeStyles } from "@material-ui/core/styles"
2
3
import Table from "@material-ui/core/Table"
3
4
import TableBody from "@material-ui/core/TableBody"
4
5
import TableCell from "@material-ui/core/TableCell"
5
6
import TableHead from "@material-ui/core/TableHead"
6
7
import TableRow from "@material-ui/core/TableRow"
7
8
import { FC } from "react"
8
9
import * as TypesGen from "../../api/typesGenerated"
10
+ import { combineClasses } from "../../util/combineClasses"
9
11
import { AvatarData } from "../AvatarData/AvatarData"
10
12
import { EmptyState } from "../EmptyState/EmptyState"
11
13
import { RoleSelect } from "../RoleSelect/RoleSelect"
@@ -45,6 +47,8 @@ export const UsersTable: FC<UsersTableProps> = ({
45
47
canEditUsers,
46
48
isLoading,
47
49
} ) => {
50
+ const styles = useStyles ( )
51
+
48
52
return (
49
53
< Table >
50
54
< TableHead >
@@ -60,55 +64,75 @@ export const UsersTable: FC<UsersTableProps> = ({
60
64
{ isLoading && < TableLoader /> }
61
65
{ ! isLoading &&
62
66
users &&
63
- users . map ( ( u ) => (
64
- < TableRow key = { u . id } >
65
- < TableCell >
66
- < AvatarData title = { u . username } subtitle = { u . email } />
67
- </ TableCell >
68
- < TableCell > { u . status } </ TableCell >
69
- < TableCell >
70
- { canEditUsers ? (
71
- < RoleSelect
72
- roles = { roles ?? [ ] }
73
- selectedRoles = { u . roles }
74
- loading = { isUpdatingUserRoles }
75
- onChange = { ( roles ) => onUpdateUserRoles ( u , roles ) }
76
- />
77
- ) : (
78
- < > { u . roles . map ( ( r ) => r . display_name ) . join ( ", " ) } </ >
79
- ) }
80
- </ TableCell >
81
- { canEditUsers && (
67
+ users . map ( ( user ) => {
68
+ // When the user has no role we want to show they are a Member
69
+ const fallbackRole : TypesGen . Role = {
70
+ name : "member" ,
71
+ display_name : "Member" ,
72
+ }
73
+ const userRoles = user . roles . length === 0 ? [ fallbackRole ] : user . roles
74
+
75
+ return (
76
+ < TableRow key = { user . id } >
77
+ < TableCell >
78
+ < AvatarData title = { user . username } subtitle = { user . email } />
79
+ </ TableCell >
80
+ < TableCell
81
+ className = { combineClasses ( [
82
+ styles . status ,
83
+ user . status === "suspended" ? styles . suspended : undefined ,
84
+ ] ) }
85
+ >
86
+ { user . status }
87
+ </ TableCell >
82
88
< TableCell >
83
- < TableRowMenu
84
- data = { u }
85
- menuItems = {
86
- // Return either suspend or activate depending on status
87
- ( u . status === "active"
88
- ? [
89
- {
90
- label : Language . suspendMenuItem ,
91
- onClick : onSuspendUser ,
92
- } ,
93
- ]
94
- : [
95
- // TODO: Uncomment this and add activate user functionality.
96
- // {
97
- // label: Language.activateMenuItem,
98
- // // eslint-disable-next-line @typescript-eslint/no-empty-function
99
- // onClick: function () {},
100
- // },
101
- ]
102
- ) . concat ( {
103
- label : Language . resetPasswordMenuItem ,
104
- onClick : onResetUserPassword ,
105
- } )
106
- }
107
- />
89
+ { canEditUsers ? (
90
+ < RoleSelect
91
+ roles = { roles ?? [ ] }
92
+ selectedRoles = { userRoles }
93
+ loading = { isUpdatingUserRoles }
94
+ onChange = { ( roles ) => {
95
+ // Remove the fallback role because it is only for the UI
96
+ roles = roles . filter ( ( role ) => role !== fallbackRole . name )
97
+ onUpdateUserRoles ( user , roles )
98
+ } }
99
+ />
100
+ ) : (
101
+ < > { userRoles . map ( ( role ) => role . display_name ) . join ( ", " ) } </ >
102
+ ) }
108
103
</ TableCell >
109
- ) }
110
- </ TableRow >
111
- ) ) }
104
+ { canEditUsers && (
105
+ < TableCell >
106
+ < TableRowMenu
107
+ data = { user }
108
+ menuItems = {
109
+ // Return either suspend or activate depending on status
110
+ ( user . status === "active"
111
+ ? [
112
+ {
113
+ label : Language . suspendMenuItem ,
114
+ onClick : onSuspendUser ,
115
+ } ,
116
+ ]
117
+ : [
118
+ // TODO: Uncomment this and add activate user functionality.
119
+ // {
120
+ // label: Language.activateMenuItem,
121
+ // // eslint-disable-next-line @typescript-eslint/no-empty-function
122
+ // onClick: function () {},
123
+ // },
124
+ ]
125
+ ) . concat ( {
126
+ label : Language . resetPasswordMenuItem ,
127
+ onClick : onResetUserPassword ,
128
+ } )
129
+ }
130
+ />
131
+ </ TableCell >
132
+ ) }
133
+ </ TableRow >
134
+ )
135
+ } ) }
112
136
113
137
{ users && users . length === 0 && (
114
138
< TableRow >
@@ -123,3 +147,12 @@ export const UsersTable: FC<UsersTableProps> = ({
123
147
</ Table >
124
148
)
125
149
}
150
+
151
+ const useStyles = makeStyles ( ( theme ) => ( {
152
+ status : {
153
+ textTransform : "capitalize" ,
154
+ } ,
155
+ suspended : {
156
+ color : theme . palette . text . secondary ,
157
+ } ,
158
+ } ) )
0 commit comments