Skip to content

Commit b3c9839

Browse files
chore(site): use react-query to fetch roles (coder#9630)
1 parent b33cb0e commit b3c9839

File tree

4 files changed

+13
-91
lines changed

4 files changed

+13
-91
lines changed

site/src/api/api.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,7 @@ export const updateUserPassword = async (
719719
): Promise<undefined> =>
720720
axios.put(`/api/v2/users/${userId}/password`, updatePassword);
721721

722-
export const getSiteRoles = async (): Promise<
723-
Array<TypesGen.AssignableRoles>
724-
> => {
722+
export const getRoles = async (): Promise<Array<TypesGen.AssignableRoles>> => {
725723
const response = await axios.get<Array<TypesGen.AssignableRoles>>(
726724
`/api/v2/users/roles`,
727725
);

site/src/api/queries/roles.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as API from "api/api";
2+
3+
export const roles = () => {
4+
return {
5+
queryKey: ["roles"],
6+
queryFn: API.getRoles,
7+
};
8+
};

site/src/pages/UsersPage/UsersPage.tsx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { usePermissions } from "hooks/usePermissions";
1010
import { FC, ReactNode, useEffect } from "react";
1111
import { Helmet } from "react-helmet-async";
1212
import { useSearchParams, useNavigate } from "react-router-dom";
13-
import { siteRolesMachine } from "xServices/roles/siteRolesXService";
1413
import { usersMachine } from "xServices/users/usersXService";
1514
import { ConfirmDialog } from "../../components/Dialogs/ConfirmDialog/ConfirmDialog";
1615
import { ResetPasswordDialog } from "./ResetPasswordDialog";
@@ -22,6 +21,7 @@ import { useDashboard } from "components/Dashboard/DashboardProvider";
2221
import { deploymentConfigMachine } from "xServices/deploymentConfig/deploymentConfigMachine";
2322
import { useQuery } from "@tanstack/react-query";
2423
import { getAuthMethods } from "api/api";
24+
import { roles } from "api/queries/roles";
2525

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

6666
const { updateUsers: canEditUsers, viewDeploymentValues } = usePermissions();
67-
const [rolesState] = useMachine(siteRolesMachine, {
68-
context: {
69-
hasPermission: canEditUsers,
70-
},
71-
});
72-
const { roles } = rolesState.context;
67+
const rolesQuery = useQuery({ ...roles(), enabled: canEditUsers });
7368

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

115110
return (
@@ -119,7 +114,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
119114
</Helmet>
120115
<UsersPageView
121116
oidcRoleSyncEnabled={oidcRoleSyncEnabled}
122-
roles={roles}
117+
roles={rolesQuery.data}
123118
users={users}
124119
authMethods={authMethods.data}
125120
count={count}

site/src/xServices/roles/siteRolesXService.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)