Skip to content

chore: do less calculation on users page #4801

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 2 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
110 changes: 68 additions & 42 deletions site/src/pages/UsersPage/UsersPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useActor, useMachine } from "@xstate/react"
import { User } from "api/typesGenerated"
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog"
import { getPaginationContext } from "components/PaginationWidget/utils"
import { usePermissions } from "hooks/usePermissions"
Expand All @@ -22,6 +23,9 @@ export const Language = {
activateDialogMessagePrefix: "Do you want to activate the user",
}

const getSelectedUser = (id: string, users?: User[]) =>
users?.find((u) => u.id === id)

export const UsersPage: FC<{ children?: ReactNode }> = () => {
const xServices = useContext(XServiceContext)
const navigate = useNavigate()
Expand All @@ -40,18 +44,14 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
const {
users,
getUsersError,
userIdToDelete,
userIdToSuspend,
userIdToActivate,
usernameToDelete,
usernameToSuspend,
usernameToActivate,
userIdToResetPassword,
newUserPassword,
paginationRef,
} = usersState.context

const userToBeSuspended = users?.find((u) => u.id === userIdToSuspend)
const userToBeDeleted = users?.find((u) => u.id === userIdToDelete)
const userToBeActivated = users?.find((u) => u.id === userIdToActivate)
const userToResetPassword = users?.find((u) => u.id === userIdToResetPassword)
const { updateUsers: canEditUsers } = usePermissions()
const [rolesState, rolesSend] = useActor(xServices.siteRolesXService)
const { roles } = rolesState.context
Expand Down Expand Up @@ -88,13 +88,25 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
)
}}
onDeleteUser={(user) => {
usersSend({ type: "DELETE_USER", userId: user.id })
usersSend({
type: "DELETE_USER",
userId: user.id,
username: user.username,
})
}}
onSuspendUser={(user) => {
usersSend({ type: "SUSPEND_USER", userId: user.id })
usersSend({
type: "SUSPEND_USER",
userId: user.id,
username: user.username,
})
}}
onActivateUser={(user) => {
usersSend({ type: "ACTIVATE_USER", userId: user.id })
usersSend({
type: "ACTIVATE_USER",
userId: user.id,
username: user.username,
})
}}
onResetUserPassword={(user) => {
usersSend({ type: "RESET_USER_PASSWORD", userId: user.id })
Expand All @@ -117,25 +129,29 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
paginationRef={paginationRef}
/>

{userToBeDeleted && (
<DeleteDialog
isOpen={usersState.matches("confirmUserDeletion")}
confirmLoading={usersState.matches("deletingUser")}
name={userToBeDeleted.username}
entity="user"
onConfirm={() => {
usersSend("CONFIRM_USER_DELETE")
}}
onCancel={() => {
usersSend("CANCEL_USER_DELETE")
}}
/>
)}
<DeleteDialog
isOpen={
usersState.matches("confirmUserDeletion") ||
usersState.matches("deletingUser")
}
confirmLoading={usersState.matches("deletingUser")}
name={usernameToDelete ?? ""}
entity="user"
onConfirm={() => {
usersSend("CONFIRM_USER_DELETE")
}}
onCancel={() => {
usersSend("CANCEL_USER_DELETE")
}}
/>

<ConfirmDialog
type="delete"
hideCancel={false}
open={usersState.matches("confirmUserSuspension")}
open={
usersState.matches("confirmUserSuspension") ||
usersState.matches("suspendingUser")
}
confirmLoading={usersState.matches("suspendingUser")}
title={Language.suspendDialogTitle}
confirmText={Language.suspendDialogAction}
Expand All @@ -147,16 +163,20 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
}}
description={
<>
{Language.suspendDialogMessagePrefix}{" "}
<strong>{userToBeSuspended?.username}</strong>?
{Language.suspendDialogMessagePrefix}
{usernameToSuspend && " "}
<strong>{usernameToSuspend ?? ""}</strong>?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlikely to happen but if there is no usernameToSuspend, should we show "User unknown" or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it reads ok with a username there so I just made the space before the ? depend on whether there will be a username.

</>
}
/>

<ConfirmDialog
type="success"
hideCancel={false}
open={usersState.matches("confirmUserActivation")}
open={
usersState.matches("confirmUserActivation") ||
usersState.matches("activatingUser")
}
confirmLoading={usersState.matches("activatingUser")}
title={Language.activateDialogTitle}
confirmText={Language.activateDialogAction}
Expand All @@ -168,24 +188,30 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
}}
description={
<>
{Language.activateDialogMessagePrefix}{" "}
<strong>{userToBeActivated?.username}</strong>?
{Language.activateDialogMessagePrefix}
{usernameToActivate && " "}
<strong>{usernameToActivate ?? ""}</strong>?
</>
}
/>

<ResetPasswordDialog
loading={usersState.matches("resettingUserPassword")}
user={userToResetPassword}
newPassword={newUserPassword}
open={usersState.matches("confirmUserPasswordReset")}
onClose={() => {
usersSend("CANCEL_USER_PASSWORD_RESET")
}}
onConfirm={() => {
usersSend("CONFIRM_USER_PASSWORD_RESET")
}}
/>
{userIdToResetPassword && (
<ResetPasswordDialog
open={
usersState.matches("confirmUserPasswordReset") ||
usersState.matches("resettingUserPassword")
}
loading={usersState.matches("resettingUserPassword")}
user={getSelectedUser(userIdToResetPassword, users)}
newPassword={newUserPassword}
onClose={() => {
usersSend("CANCEL_USER_PASSWORD_RESET")
}}
onConfirm={() => {
usersSend("CONFIRM_USER_PASSWORD_RESET")
}}
/>
)}
</>
)
}
Expand Down
47 changes: 38 additions & 9 deletions site/src/xServices/users/usersXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ export interface UsersContext {
getUsersError?: Error | unknown
// Suspend user
userIdToSuspend?: TypesGen.User["id"]
usernameToSuspend?: TypesGen.User["username"]
suspendUserError?: Error | unknown
// Delete user
userIdToDelete?: TypesGen.User["id"]
usernameToDelete?: TypesGen.User["username"]
deleteUserError?: Error | unknown
// Activate user
userIdToActivate?: TypesGen.User["id"]
usernameToActivate?: TypesGen.User["username"]
activateUserError?: Error | unknown
// Reset user password
userIdToResetPassword?: TypesGen.User["id"]
Expand All @@ -59,15 +62,27 @@ export interface UsersContext {
export type UsersEvent =
| { type: "GET_USERS"; query?: string }
// Suspend events
| { type: "SUSPEND_USER"; userId: TypesGen.User["id"] }
| {
type: "SUSPEND_USER"
userId: TypesGen.User["id"]
username: TypesGen.User["username"]
}
| { type: "CONFIRM_USER_SUSPENSION" }
| { type: "CANCEL_USER_SUSPENSION" }
// Delete events
| { type: "DELETE_USER"; userId: TypesGen.User["id"] }
| {
type: "DELETE_USER"
userId: TypesGen.User["id"]
username: TypesGen.User["username"]
}
| { type: "CONFIRM_USER_DELETE" }
| { type: "CANCEL_USER_DELETE" }
// Activate events
| { type: "ACTIVATE_USER"; userId: TypesGen.User["id"] }
| {
type: "ACTIVATE_USER"
userId: TypesGen.User["id"]
username: TypesGen.User["username"]
}
| { type: "CONFIRM_USER_ACTIVATION" }
| { type: "CANCEL_USER_ACTIVATION" }
// Reset password events
Expand Down Expand Up @@ -152,18 +167,19 @@ export const usersMachine =
tags: "loading",
},
idle: {
entry: "clearSelectedUser",
on: {
SUSPEND_USER: {
target: "confirmUserSuspension",
actions: "assignUserIdToSuspend",
actions: "assignUserToSuspend",
},
DELETE_USER: {
target: "confirmUserDeletion",
actions: "assignUserIdToDelete",
actions: "assignUserToDelete",
},
ACTIVATE_USER: {
target: "confirmUserActivation",
actions: "assignUserIdToActivate",
actions: "assignUserToActivate",
},
RESET_USER_PASSWORD: {
target: "confirmUserPasswordReset",
Expand Down Expand Up @@ -391,6 +407,16 @@ export const usersMachine =
},

actions: {
clearSelectedUser: assign({
userIdToSuspend: (_) => undefined,
usernameToSuspend: (_) => undefined,
userIdToDelete: (_) => undefined,
usernameToDelete: (_) => undefined,
userIdToActivate: (_) => undefined,
usernameToActivate: (_) => undefined,
userIdToResetPassword: (_) => undefined,
userIdToUpdateRoles: (_) => undefined,
}),
assignUsers: assign({
users: (_, event) => event.data,
}),
Expand All @@ -400,14 +426,17 @@ export const usersMachine =
assignGetUsersError: assign({
getUsersError: (_, event) => event.data,
}),
assignUserIdToSuspend: assign({
assignUserToSuspend: assign({
userIdToSuspend: (_, event) => event.userId,
usernameToSuspend: (_, event) => event.username,
}),
assignUserIdToDelete: assign({
assignUserToDelete: assign({
userIdToDelete: (_, event) => event.userId,
usernameToDelete: (_, event) => event.username,
}),
assignUserIdToActivate: assign({
assignUserToActivate: assign({
userIdToActivate: (_, event) => event.userId,
usernameToActivate: (_, event) => event.username,
}),
assignUserIdToResetPassword: assign({
userIdToResetPassword: (_, event) => event.userId,
Expand Down