Skip to content

chore(site): use react-query to fetch roles #9630

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 3 commits into from
Sep 12, 2023
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
4 changes: 1 addition & 3 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,7 @@ export const updateUserPassword = async (
): Promise<undefined> =>
axios.put(`/api/v2/users/${userId}/password`, updatePassword);

export const getSiteRoles = async (): Promise<
Array<TypesGen.AssignableRoles>
> => {
export const getRoles = async (): Promise<Array<TypesGen.AssignableRoles>> => {
const response = await axios.get<Array<TypesGen.AssignableRoles>>(
`/api/v2/users/roles`,
);
Expand Down
8 changes: 8 additions & 0 deletions site/src/api/queries/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as API from "api/api";

export const roles = () => {
return {
queryKey: ["roles"],
queryFn: API.getRoles,
};
};
13 changes: 4 additions & 9 deletions site/src/pages/UsersPage/UsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { usePermissions } from "hooks/usePermissions";
import { FC, ReactNode, useEffect } from "react";
import { Helmet } from "react-helmet-async";
import { useSearchParams, useNavigate } from "react-router-dom";
import { siteRolesMachine } from "xServices/roles/siteRolesXService";
import { usersMachine } from "xServices/users/usersXService";
import { ConfirmDialog } from "../../components/Dialogs/ConfirmDialog/ConfirmDialog";
import { ResetPasswordDialog } from "./ResetPasswordDialog";
Expand All @@ -22,6 +21,7 @@ import { useDashboard } from "components/Dashboard/DashboardProvider";
import { deploymentConfigMachine } from "xServices/deploymentConfig/deploymentConfigMachine";
import { useQuery } from "@tanstack/react-query";
import { getAuthMethods } from "api/api";
import { roles } from "api/queries/roles";

export const Language = {
suspendDialogTitle: "Suspend user",
Expand Down Expand Up @@ -64,12 +64,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
} = usersState.context;

const { updateUsers: canEditUsers, viewDeploymentValues } = usePermissions();
const [rolesState] = useMachine(siteRolesMachine, {
context: {
hasPermission: canEditUsers,
},
});
const { roles } = rolesState.context;
const rolesQuery = useQuery({ ...roles(), enabled: canEditUsers });

// Ideally this only runs if 'canViewDeployment' is true.
// TODO: Prevent api call if the user does not have the perms.
Expand Down Expand Up @@ -109,7 +104,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
// - the user can edit the users but the roles are loading
const isLoading =
usersState.matches("gettingUsers") ||
(canEditUsers && rolesState.matches("gettingRoles")) ||
rolesQuery.isLoading ||
authMethods.isLoading;

return (
Expand All @@ -119,7 +114,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
</Helmet>
<UsersPageView
oidcRoleSyncEnabled={oidcRoleSyncEnabled}
roles={roles}
roles={rolesQuery.data}
users={users}
authMethods={authMethods.data}
count={count}
Expand Down
79 changes: 0 additions & 79 deletions site/src/xServices/roles/siteRolesXService.ts

This file was deleted.