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 auth methods queries to the users query module
  • Loading branch information
BrunoQuaresma committed Sep 29, 2023
commit 76e94fb4e46bf99d6657f0d5d81ee4fbb368fb2b
9 changes: 9 additions & 0 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ export const updateRoles = (queryClient: QueryClient) => {
},
};
};

export const authMethods = () => {
return {
// Even the endpoint being /users/authmethods we don't want to revalidate it
// when users change so its better add a unique query key
queryKey: ["authMethods"],
queryFn: API.getAuthMethods,
};
};
12 changes: 3 additions & 9 deletions site/src/pages/CreateUserPage/CreateUserPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { useNavigate } from "react-router-dom";
import { CreateUserForm } from "./CreateUserForm";
import { Margins } from "components/Margins/Margins";
import { pageTitle } from "utils/page";
import { getAuthMethods } from "api/api";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { createUser } from "api/queries/users";
import { authMethods, createUser } from "api/queries/users";
import { displaySuccess } from "components/GlobalSnackbar/utils";

export const Language = {
Expand All @@ -19,12 +18,7 @@ export const CreateUserPage: FC = () => {
const navigate = useNavigate();
const queryClient = useQueryClient();
const createUserMutation = useMutation(createUser(queryClient));
// TODO: We should probably place this somewhere else to reduce the number of calls.
// This would be called each time this page is loaded.
const { data: authMethods } = useQuery({
queryKey: ["authMethods"],
queryFn: getAuthMethods,
});
const authMethodsQuery = useQuery(authMethods());

return (
<Margins>
Expand All @@ -34,7 +28,7 @@ export const CreateUserPage: FC = () => {

<CreateUserForm
error={createUserMutation.error}
authMethods={authMethods}
authMethods={authMethodsQuery.data}
onSubmit={async (user) => {
await createUserMutation.mutateAsync(user);
displaySuccess("Successfully created user.");
Expand Down
13 changes: 5 additions & 8 deletions site/src/pages/UserSettingsPage/SecurityPage/SecurityPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ import { ComponentProps, FC } from "react";
import { Section } from "components/SettingsLayout/Section";
import { SecurityForm } from "./SettingsSecurityForm";
import { useMutation, useQuery } from "@tanstack/react-query";
import { getAuthMethods, getUserLoginType } from "api/api";
import { getUserLoginType } from "api/api";
import {
SingleSignOnSection,
useSingleSignOnSection,
} from "./SingleSignOnSection";
import { Loader } from "components/Loader/Loader";
import { Stack } from "components/Stack/Stack";
import { updatePassword } from "api/queries/users";
import { authMethods, updatePassword } from "api/queries/users";
import { displaySuccess } from "components/GlobalSnackbar/utils";

export const SecurityPage: FC = () => {
const me = useMe();
const updatePasswordMutation = useMutation(updatePassword());
const { data: authMethods } = useQuery({
queryKey: ["authMethods"],
queryFn: getAuthMethods,
});
const authMethodsQuery = useQuery(authMethods());
const { data: userLoginType } = useQuery({
queryKey: ["loginType"],
queryFn: getUserLoginType,
});
const singleSignOnSection = useSingleSignOnSection();

if (!authMethods || !userLoginType) {
if (!authMethodsQuery.data || !userLoginType) {
Copy link
Member

Choose a reason for hiding this comment

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

Do we handle error state? Does data always return (empty) if the request errors?

Copy link
Member

Choose a reason for hiding this comment

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

yeah, error values are always exposed through error and isError

return <Loader />;
}

Expand All @@ -51,7 +48,7 @@ export const SecurityPage: FC = () => {
}}
oidc={{
section: {
authMethods,
authMethods: authMethodsQuery.data,
userLoginType,
...singleSignOnSection,
},
Expand Down
13 changes: 4 additions & 9 deletions site/src/pages/UsersPage/UsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { useStatusFilterMenu } from "./UsersFilter";
import { useFilter } from "components/Filter/filter";
import { useDashboard } from "components/Dashboard/DashboardProvider";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { getAuthMethods } from "api/api";
import { roles } from "api/queries/roles";
import { deploymentConfig } from "api/queries/deployment";
import { prepareQuery } from "utils/filters";
Expand All @@ -26,6 +25,7 @@ import {
deleteUser,
updatePassword,
updateRoles,
authMethods,
} from "api/queries/users";
import { displayError, displaySuccess } from "components/GlobalSnackbar/utils";
import { getErrorMessage } from "api/errors";
Expand Down Expand Up @@ -74,14 +74,9 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
status: option?.value,
}),
});
const authMethods = useQuery({
queryKey: ["authMethods"],
queryFn: () => {
return getAuthMethods();
},
});
const authMethodsQuery = useQuery(authMethods());
const isLoading =
usersQuery.isLoading || rolesQuery.isLoading || authMethods.isLoading;
usersQuery.isLoading || rolesQuery.isLoading || authMethodsQuery.isLoading;
// Suspend
Copy link
Member

Choose a reason for hiding this comment

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

these comments don't really add much value, and feel like the kind that are prone to eventual drift. maybe just separate each group with a blank line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I personally like to use comments to separate things sometimes 😁 going to remove them and add blank lines instead

const [confirmSuspendUser, setConfirmSuspendUser] = useState<User>();
const suspendUserMutation = useMutation(suspendUser(queryClient));
Expand Down Expand Up @@ -109,7 +104,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
oidcRoleSyncEnabled={oidcRoleSyncEnabled}
roles={rolesQuery.data}
users={usersQuery.data?.users}
authMethods={authMethods.data}
authMethods={authMethodsQuery.data}
onListWorkspaces={(user) => {
navigate(
"/workspaces?filter=" +
Expand Down