Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: Improve roles UI #5576

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 11 commits into from
Jan 4, 2023
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
Prev Previous commit
Next Next commit
Predicatable sorting
  • Loading branch information
BrunoQuaresma committed Jan 4, 2023
commit f5b1b23f12cc01c37c073fc8d4f12bbd58290374
47 changes: 14 additions & 33 deletions site/src/components/EditRolesButton/EditRolesButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Popover from "@material-ui/core/Popover"
import { Stack } from "components/Stack/Stack"
import Checkbox from "@material-ui/core/Checkbox"
import UserIcon from "@material-ui/icons/PersonOutline"
import { AssignableRoles, Role } from "api/typesGenerated"
import { Role } from "api/typesGenerated"

const Option: React.FC<{
value: string
Expand Down Expand Up @@ -43,7 +43,7 @@ const Option: React.FC<{

export const EditRolesButton: FC<{
isLoading: boolean
roles: AssignableRoles[]
roles: Role[]
selectedRoles: Role[]
onChange: (roles: Role["name"][]) => void
}> = ({ roles, selectedRoles, onChange, isLoading }) => {
Expand Down Expand Up @@ -92,44 +92,25 @@ export const EditRolesButton: FC<{
>
<fieldset className={styles.fieldset} disabled={isLoading}>
<Stack className={styles.options} spacing={3}>
<Option
onChange={handleChange}
isChecked={selectedRoleNames.includes("owner")}
value="owner"
name="Owner"
description="Admin can manage all the coder resources including users and their permissions"
/>
<Option
onChange={handleChange}
isChecked={selectedRoleNames.includes("user-admin")}
value="user-admin"
name="User admin"
description="Admin can manage all the coder resources including users and their permissions"
/>
<Option
onChange={handleChange}
isChecked={selectedRoleNames.includes("template-admin")}
value="template-admin"
name="Template admin"
description="Admin can manage all the coder resources including users and their permissions"
/>
<Option
onChange={handleChange}
isChecked={selectedRoleNames.includes("auditor")}
value="auditor"
name="Auditor"
description="Admin can manage all the coder resources including users and their permissions"
/>
{roles.map((role) => (
<Option
key={role.name}
onChange={handleChange}
isChecked={selectedRoleNames.includes(role.name)}
value={role.name}
name={role.display_name}
description={t(`roleDescription.${role.name}`)}
/>
))}
</Stack>
</fieldset>
<div className={styles.footer}>
<Stack direction="row" alignItems="flex-start">
<UserIcon className={styles.userIcon} />
<Stack spacing={0.5}>
<strong>Member</strong>
<strong>{t("member")}</strong>
<span className={styles.optionDescription}>
Everybody is a member. This is a shared and default role for all
the users.
{t("roleDescription.member")}
</span>
</Stack>
</Stack>
Expand Down
13 changes: 11 additions & 2 deletions site/src/components/UsersTable/UsersTableBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ import { Stack } from "components/Stack/Stack"
const isAdminRole = (role: TypesGen.Role): boolean => {
return role.name === "owner" || role.name.includes("admin")
}

const roleOrder = ["owner", "user-admin", "template-admin", "auditor"]

const sortRoles = (roles: TypesGen.Role[]) => {
return roles.slice(0).sort((a, b) => {
return roleOrder.indexOf(a.name) - roleOrder.indexOf(b.name)
})
}

interface UsersTableBodyProps {
users?: TypesGen.User[]
roles?: TypesGen.AssignableRoles[]
Expand Down Expand Up @@ -93,7 +102,7 @@ export const UsersTableBody: FC<
display_name: "Member",
}
const userRoles =
user.roles.length === 0 ? [fallbackRole] : user.roles
user.roles.length === 0 ? [fallbackRole] : sortRoles(user.roles)

return (
<TableRow key={user.id}>
Expand All @@ -117,7 +126,7 @@ export const UsersTableBody: FC<
<Stack direction="row" spacing={1}>
{canEditUsers && (
<EditRolesButton
roles={roles ?? []}
roles={roles ? sortRoles(roles) : []}
selectedRoles={userRoles}
isLoading={Boolean(isUpdatingUserRoles)}
onChange={(roles) => {
Expand Down
10 changes: 9 additions & 1 deletion site/src/i18n/en/usersPage.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@
"listWorkspacesMenuItem": "View workspaces",
"activateMenuItem": "Activate",
"resetPasswordMenuItem": "Reset password",
"editUserRolesTooltip": "Edit user roles"
"editUserRolesTooltip": "Edit user roles",
"roleDescription": {
"owner": "Admin can manage all the coder resources including users and their permissions",
"user-admin": "Admin can manage all the coder resources including users and their permissions",
"template-admin": "Admin can manage all the coder resources including users and their permissions",
"auditor": "Admin can manage all the coder resources including users and their permissions",
"member": "Admin can manage all the coder resources including users and their permissions"
},
"member": "Member"
}