1
- import { useMachine } from "@xstate/react" ;
2
1
import { useAuth } from "components/AuthProvider/AuthProvider" ;
3
2
import { FC } from "react" ;
4
3
import { Helmet } from "react-helmet-async" ;
5
4
import { pageTitle } from "utils/page" ;
6
- import { setupMachine } from "xServices/setup/setupXService" ;
7
5
import { SetupPageView } from "./SetupPageView" ;
8
6
import { Navigate } from "react-router-dom" ;
7
+ import { useMutation } from "@tanstack/react-query" ;
8
+ import { createFirstUser } from "api/queries/users" ;
9
9
10
10
export const SetupPage : FC = ( ) => {
11
11
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 ( ) ) ;
28
13
const userIsSignedIn = authState . matches ( "signedIn" ) ;
29
14
const setupIsComplete =
30
15
! authState . matches ( "loadingInitialAuthData" ) &&
@@ -46,10 +31,15 @@ export const SetupPage: FC = () => {
46
31
< title > { pageTitle ( "Set up your account" ) } </ title >
47
32
</ Helmet >
48
33
< 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
+ } ) ;
53
43
} }
54
44
/>
55
45
</ >
0 commit comments