Skip to content

chore(site): replace create user machine by react-query #9810

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 2 commits into from
Sep 22, 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
6 changes: 6 additions & 0 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export const updatePassword = () => {
};
};

export const createUser = () => {
return {
mutationFn: API.createUser,
};
};

export const createFirstUser = () => {
return {
mutationFn: API.createFirstUser,
Expand Down
3 changes: 1 addition & 2 deletions site/src/pages/CreateUserPage/CreateUserPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
renderWithAuth,
waitForLoaderToBeRemoved,
} from "testHelpers/renderHelpers";
import { Language as CreateUserLanguage } from "xServices/users/createUserXService";
import { CreateUserPage } from "./CreateUserPage";

const renderCreateUserPage = async () => {
Expand Down Expand Up @@ -47,7 +46,7 @@ describe("Create User Page", () => {
await renderCreateUserPage();
await fillForm({});
const successMessage = await screen.findByText(
CreateUserLanguage.createUserSuccess,
"Successfully created user.",
);
expect(successMessage).toBeDefined();
});
Expand Down
30 changes: 11 additions & 19 deletions site/src/pages/CreateUserPage/CreateUserPage.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { useMachine } from "@xstate/react";
import { useOrganizationId } from "hooks/useOrganizationId";
import { FC } from "react";
import { Helmet } from "react-helmet-async";
import { useNavigate } from "react-router-dom";
import { createUserMachine } from "xServices/users/createUserXService";
import * as TypesGen from "api/typesGenerated";
import { CreateUserForm } from "./CreateUserForm";
import { Margins } from "components/Margins/Margins";
import { pageTitle } from "utils/page";
import { getAuthMethods } from "api/api";
import { useQuery } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { createUser } from "api/queries/users";
import { displaySuccess } from "components/GlobalSnackbar/utils";

export const Language = {
unknownError: "Oops, an unknown error occurred.",
Expand All @@ -18,15 +17,7 @@ export const Language = {
export const CreateUserPage: FC = () => {
const myOrgId = useOrganizationId();
const navigate = useNavigate();
const [createUserState, createUserSend] = useMachine(createUserMachine, {
actions: {
redirectToUsersPage: () => {
navigate("/users");
},
},
});
const { error } = createUserState.context;

const createUserMutation = useMutation(createUser());
// 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({
Expand All @@ -41,16 +32,17 @@ export const CreateUserPage: FC = () => {
</Helmet>

<CreateUserForm
error={error}
error={createUserMutation.error}
authMethods={authMethods}
onSubmit={(user: TypesGen.CreateUserRequest) =>
createUserSend({ type: "CREATE", user })
}
onSubmit={async (user) => {
await createUserMutation.mutateAsync(user);
displaySuccess("Successfully created user.");
navigate("/users");
}}
onCancel={() => {
createUserSend("CANCEL_CREATE_USER");
navigate("/users");
}}
isLoading={createUserState.hasTag("loading")}
isLoading={createUserMutation.isLoading}
myOrgId={myOrgId}
/>
</Margins>
Expand Down
72 changes: 0 additions & 72 deletions site/src/xServices/users/createUserXService.ts

This file was deleted.