Skip to content

refactor: Display member role when user has no role #1965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: Display member role when user has no role
  • Loading branch information
BrunoQuaresma committed Jun 1, 2022
commit a25dd55fe27524f7ab646d8fbc3f8b0b8e1627fc
2 changes: 1 addition & 1 deletion site/src/components/RoleSelect/RoleSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const RoleSelect: FC<RoleSelectProps> = ({ roles, selectedRoles, loading,

return (
<MenuItem key={r.name} value={r.name} disabled={loading}>
<Checkbox color="primary" checked={isChecked} /> {r.display_name}
<Checkbox size="small" color="primary" checked={isChecked} /> {r.display_name}
</MenuItem>
)
})}
Expand Down
125 changes: 78 additions & 47 deletions site/src/components/UsersTable/UsersTable.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Box from "@material-ui/core/Box"
import { makeStyles } from "@material-ui/core/styles"
import Table from "@material-ui/core/Table"
import TableBody from "@material-ui/core/TableBody"
import TableCell from "@material-ui/core/TableCell"
import TableHead from "@material-ui/core/TableHead"
import TableRow from "@material-ui/core/TableRow"
import { FC } from "react"
import * as TypesGen from "../../api/typesGenerated"
import { combineClasses } from "../../util/combineClasses"
import { AvatarData } from "../AvatarData/AvatarData"
import { EmptyState } from "../EmptyState/EmptyState"
import { RoleSelect } from "../RoleSelect/RoleSelect"
Expand Down Expand Up @@ -45,6 +47,8 @@ export const UsersTable: FC<UsersTableProps> = ({
canEditUsers,
isLoading,
}) => {
const styles = useStyles()

return (
<Table>
<TableHead>
Expand All @@ -60,55 +64,73 @@ export const UsersTable: FC<UsersTableProps> = ({
{isLoading && <TableLoader />}
{!isLoading &&
users &&
users.map((u) => (
<TableRow key={u.id}>
<TableCell>
<AvatarData title={u.username} subtitle={u.email} />
</TableCell>
<TableCell>{u.status}</TableCell>
<TableCell>
{canEditUsers ? (
<RoleSelect
roles={roles ?? []}
selectedRoles={u.roles}
loading={isUpdatingUserRoles}
onChange={(roles) => onUpdateUserRoles(u, roles)}
/>
) : (
<>{u.roles.map((r) => r.display_name).join(", ")}</>
)}
</TableCell>
{canEditUsers && (
users.map((user) => {
// When the user has no role, it is because they are a member
const fallbackRoles: TypesGen.Role[] = [
{
name: "member",
display_name: "Member",
},
]
const userRoles = user.roles.length === 0 ? fallbackRoles : user.roles

return (
<TableRow key={user.id}>
<TableCell>
<AvatarData title={user.username} subtitle={user.email} />
</TableCell>
<TableCell
className={combineClasses([
styles.status,
user.status === "suspended" ? styles.suspended : undefined,
])}
>
{user.status}
</TableCell>
<TableCell>
<TableRowMenu
data={u}
menuItems={
// Return either suspend or activate depending on status
(u.status === "active"
? [
{
label: Language.suspendMenuItem,
onClick: onSuspendUser,
},
]
: [
// TODO: Uncomment this and add activate user functionality.
// {
// label: Language.activateMenuItem,
// // eslint-disable-next-line @typescript-eslint/no-empty-function
// onClick: function () {},
// },
]
).concat({
label: Language.resetPasswordMenuItem,
onClick: onResetUserPassword,
})
}
/>
{canEditUsers ? (
<RoleSelect
roles={roles ?? []}
selectedRoles={userRoles}
loading={isUpdatingUserRoles}
onChange={(roles) => onUpdateUserRoles(user, roles)}
/>
) : (
<>{userRoles.map((role) => role.display_name).join(", ")}</>
)}
</TableCell>
)}
</TableRow>
))}
{canEditUsers && (
<TableCell>
<TableRowMenu
data={user}
menuItems={
// Return either suspend or activate depending on status
(user.status === "active"
? [
{
label: Language.suspendMenuItem,
onClick: onSuspendUser,
},
]
: [
// TODO: Uncomment this and add activate user functionality.
// {
// label: Language.activateMenuItem,
// // eslint-disable-next-line @typescript-eslint/no-empty-function
// onClick: function () {},
// },
]
).concat({
label: Language.resetPasswordMenuItem,
onClick: onResetUserPassword,
})
}
/>
</TableCell>
)}
</TableRow>
)
})}

{users && users.length === 0 && (
<TableRow>
Expand All @@ -123,3 +145,12 @@ export const UsersTable: FC<UsersTableProps> = ({
</Table>
)
}

const useStyles = makeStyles((theme) => ({
status: {
textTransform: "capitalize",
},
suspended: {
color: theme.palette.text.secondary,
},
}))