Skip to content

chore(site): remove users and pagination services #9932

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 18 commits into from
Oct 2, 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
Move update roles to react-query
  • Loading branch information
BrunoQuaresma committed Sep 29, 2023
commit 269f67fe7cbae0eb7955af9d83bfeedb89d57d82
10 changes: 10 additions & 0 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ export const deleteUser = (queryClient: QueryClient) => {
},
};
};

export const updateRoles = (queryClient: QueryClient) => {
return {
mutationFn: ({ userId, roles }: { userId: string; roles: string[] }) =>
API.updateUserRoles(roles, userId),
onSuccess: async () => {
await queryClient.invalidateQueries(["users"]);
},
};
};
6 changes: 1 addition & 5 deletions site/src/pages/UsersPage/UsersPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
MockAuditorRole,
MockOwnerRole,
} from "testHelpers/entities";
import { Language as usersXServiceLanguage } from "xServices/users/usersXService";
import * as API from "api/api";
import { Role } from "api/typesGenerated";
import { Language as ResetPasswordDialogLanguage } from "./ResetPasswordDialog";
Expand Down Expand Up @@ -406,10 +405,7 @@ describe("UsersPage", () => {
}, MockAuditorRole);

// Check if the error message is displayed
const errorMessage = await screen.findByText(
usersXServiceLanguage.updateUserRolesError,
);
await waitFor(() => expect(errorMessage).toBeDefined());
await waitFor(() => expect("Error updating user roles").toBeDefined());

// Check if the API was called correctly
const currentRoles = MockUser.roles.map((r) => r.name);
Expand Down
24 changes: 14 additions & 10 deletions site/src/pages/UsersPage/UsersPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMachine } from "@xstate/react";
import { User } from "api/typesGenerated";
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog";
import { nonInitialPage } from "components/PaginationWidget/utils";
Expand All @@ -7,7 +6,6 @@ import { usePermissions } from "hooks/usePermissions";
import { FC, ReactNode, useState } from "react";
import { Helmet } from "react-helmet-async";
import { useSearchParams, useNavigate } from "react-router-dom";
import { usersMachine } from "xServices/users/usersXService";
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog";
import { ResetPasswordDialog } from "./ResetPasswordDialog";
import { pageTitle } from "utils/page";
Expand All @@ -27,6 +25,7 @@ import {
activateUser,
deleteUser,
updatePassword,
updateRoles,
} from "api/queries/users";
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils";
import { getErrorMessage } from "api/errors";
Expand All @@ -48,7 +47,6 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
const { entitlements } = useDashboard();
const [searchParams] = searchParamsResult;
const filter = searchParams.get("filter") ?? "";
const [usersState, usersSend] = useMachine(usersMachine);
const pagination = usePagination({
searchParamsResult,
});
Expand Down Expand Up @@ -108,6 +106,8 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
newPassword: string;
}>();
const updatePasswordMutation = useMutation(updatePassword());
// Update roles
const updateRolesMutation = useMutation(updateRoles(queryClient));

return (
<>
Expand Down Expand Up @@ -139,14 +139,18 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
newPassword: generateRandomString(12),
});
}}
onUpdateUserRoles={(user, roles) => {
usersSend({
type: "UPDATE_USER_ROLES",
userId: user.id,
roles,
});
onUpdateUserRoles={async (user, roles) => {
try {
await updateRolesMutation.mutateAsync({
userId: user.id,
roles,
});
displaySuccess("User roles updated");
} catch (e) {
displayError(getErrorMessage(e, "Error updating user roles"));
}
}}
isUpdatingUserRoles={usersState.matches("updatingUserRoles")}
isUpdatingUserRoles={updateRolesMutation.isLoading}
isLoading={isLoading}
canEditUsers={canEditUsers}
canViewActivity={entitlements.features.audit_log.enabled}
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/UsersPage/UsersTable/EditRolesButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const useStyles = makeStyles((theme) => ({
padding: 0,

"&:disabled": {
opacity: 0,
opacity: 0.5,
},
},
options: {
Expand Down
123 changes: 0 additions & 123 deletions site/src/xServices/users/usersXService.ts

This file was deleted.