Skip to content

Commit 801c6c9

Browse files
chore(site): replace setup machine by react-query (#9809)
1 parent 866ba8e commit 801c6c9

File tree

3 files changed

+18
-103
lines changed

3 files changed

+18
-103
lines changed

site/src/api/queries/users.ts

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ export const updatePassword = () => {
1010
API.updateUserPassword(userId, request),
1111
};
1212
};
13+
14+
export const createFirstUser = () => {
15+
return {
16+
mutationFn: API.createFirstUser,
17+
};
18+
};

site/src/pages/SetupPage/SetupPage.tsx

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
1-
import { useMachine } from "@xstate/react";
21
import { useAuth } from "components/AuthProvider/AuthProvider";
32
import { FC } from "react";
43
import { Helmet } from "react-helmet-async";
54
import { pageTitle } from "utils/page";
6-
import { setupMachine } from "xServices/setup/setupXService";
75
import { SetupPageView } from "./SetupPageView";
86
import { Navigate } from "react-router-dom";
7+
import { useMutation } from "@tanstack/react-query";
8+
import { createFirstUser } from "api/queries/users";
99

1010
export const SetupPage: FC = () => {
1111
const [authState, authSend] = useAuth();
12-
const [setupState, setupSend] = useMachine(setupMachine, {
13-
actions: {
14-
onCreateFirstUser: ({ firstUser }) => {
15-
if (!firstUser) {
16-
throw new Error("First user was not defined.");
17-
}
18-
authSend({
19-
type: "SIGN_IN",
20-
email: firstUser.email,
21-
password: firstUser.password,
22-
});
23-
},
24-
},
25-
});
26-
const { error } = setupState.context;
27-
12+
const createFirstUserMutation = useMutation(createFirstUser());
2813
const userIsSignedIn = authState.matches("signedIn");
2914
const setupIsComplete =
3015
!authState.matches("loadingInitialAuthData") &&
@@ -46,10 +31,15 @@ export const SetupPage: FC = () => {
4631
<title>{pageTitle("Set up your account")}</title>
4732
</Helmet>
4833
<SetupPageView
49-
isLoading={setupState.hasTag("loading")}
50-
error={error}
51-
onSubmit={(firstUser) => {
52-
setupSend({ type: "CREATE_FIRST_USER", firstUser });
34+
isLoading={createFirstUserMutation.isLoading}
35+
error={createFirstUserMutation.error}
36+
onSubmit={async (firstUser) => {
37+
await createFirstUserMutation.mutateAsync(firstUser);
38+
authSend({
39+
type: "SIGN_IN",
40+
email: firstUser.email,
41+
password: firstUser.password,
42+
});
5343
}}
5444
/>
5545
</>

site/src/xServices/setup/setupXService.ts

-81
This file was deleted.

0 commit comments

Comments
 (0)