Skip to content

Commit

Permalink
refactor(web): simplify users hooks API
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jul 23, 2024
1 parent 8658920 commit 2ed229f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
9 changes: 2 additions & 7 deletions web/src/components/users/FirstUser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ import { Table, Thead, Tr, Th, Tbody, Td } from "@patternfly/react-table";
import { useNavigate } from "react-router-dom";
import { RowActions, ButtonLink } from "~/components/core";
import { _ } from "~/i18n";
import {
useFirstUser,
useFirstUserChanges,
useFirstUserMutation,
useRemoveFirstUserMutation,
} from "~/queries/users";
import { useFirstUser, useFirstUserChanges, useRemoveFirstUserMutation } from "~/queries/users";

const UserNotDefined = ({ actionCb }) => {
return (
Expand Down Expand Up @@ -78,7 +73,7 @@ const UserData = ({ user, actions }) => {
};

export default function FirstUser() {
const { data: user } = useFirstUser();
const user = useFirstUser();
const removeUser = useRemoveFirstUserMutation();
const navigate = useNavigate();

Expand Down
2 changes: 1 addition & 1 deletion web/src/components/users/FirstUserForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const UsernameSuggestions = ({
// close to the related input.
// TODO: extract the suggestions logic.
export default function FirstUserForm() {
const { data: firstUser } = useFirstUser();
const firstUser = useFirstUser();
const setFirstUser = useFirstUserMutation();
const client = useInstallerClient();
const { cancellablePromise } = useCancellablePromise();
Expand Down
4 changes: 1 addition & 3 deletions web/src/components/users/RootAuthMethods.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export default function RootAuthMethods() {
const [isSSHKeyFormOpen, setIsSSHKeyFormOpen] = useState(false);
const [isPasswordFormOpen, setIsPasswordFormOpen] = useState(false);

const {
data: { password: isPasswordDefined, sshkey: sshKey },
} = useRootUser();
const { password: isPasswordDefined, sshkey: sshKey } = useRootUser();

useRootUserChanges();

Expand Down
10 changes: 8 additions & 2 deletions web/src/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ const firstUserQuery = () => ({
/**
* Hook that returns the first user.
*/
const useFirstUser = () => useSuspenseQuery(firstUserQuery());
const useFirstUser = () => {
const { data: firstUser } = useSuspenseQuery(firstUserQuery());
return firstUser;
};

/*
* Hook that returns a mutation to change the first user.
Expand Down Expand Up @@ -113,7 +116,10 @@ const rootUserQuery = () => ({
queryFn: () => fetch("/api/users/root").then((res) => res.json()),
});

const useRootUser = () => useSuspenseQuery(rootUserQuery());
const useRootUser = () => {
const { data: rootUser } = useSuspenseQuery(rootUserQuery());
return rootUser;
};

/*
* Hook that returns a mutation to change the root user configuration.
Expand Down

0 comments on commit 2ed229f

Please sign in to comment.