1
- import { useMachine } from "@xstate/react" ;
2
1
import { useOrganizationId } from "hooks/useOrganizationId" ;
3
2
import { FC } from "react" ;
4
3
import { Helmet } from "react-helmet-async" ;
5
4
import { useNavigate } from "react-router-dom" ;
6
- import { createUserMachine } from "xServices/users/createUserXService" ;
7
- import * as TypesGen from "api/typesGenerated" ;
8
5
import { CreateUserForm } from "./CreateUserForm" ;
9
6
import { Margins } from "components/Margins/Margins" ;
10
7
import { pageTitle } from "utils/page" ;
11
8
import { getAuthMethods } from "api/api" ;
12
- import { useQuery } from "@tanstack/react-query" ;
9
+ import { useMutation , useQuery } from "@tanstack/react-query" ;
10
+ import { createUser } from "api/queries/users" ;
11
+ import { displaySuccess } from "components/GlobalSnackbar/utils" ;
13
12
14
13
export const Language = {
15
14
unknownError : "Oops, an unknown error occurred." ,
@@ -18,15 +17,7 @@ export const Language = {
18
17
export const CreateUserPage : FC = ( ) => {
19
18
const myOrgId = useOrganizationId ( ) ;
20
19
const navigate = useNavigate ( ) ;
21
- const [ createUserState , createUserSend ] = useMachine ( createUserMachine , {
22
- actions : {
23
- redirectToUsersPage : ( ) => {
24
- navigate ( "/users" ) ;
25
- } ,
26
- } ,
27
- } ) ;
28
- const { error } = createUserState . context ;
29
-
20
+ const createUserMutation = useMutation ( createUser ( ) ) ;
30
21
// TODO: We should probably place this somewhere else to reduce the number of calls.
31
22
// This would be called each time this page is loaded.
32
23
const { data : authMethods } = useQuery ( {
@@ -41,16 +32,17 @@ export const CreateUserPage: FC = () => {
41
32
</ Helmet >
42
33
43
34
< CreateUserForm
44
- error = { error }
35
+ error = { createUserMutation . error }
45
36
authMethods = { authMethods }
46
- onSubmit = { ( user : TypesGen . CreateUserRequest ) =>
47
- createUserSend ( { type : "CREATE" , user } )
48
- }
37
+ onSubmit = { async ( user ) => {
38
+ await createUserMutation . mutateAsync ( user ) ;
39
+ displaySuccess ( "Successfully created user." ) ;
40
+ navigate ( "/users" ) ;
41
+ } }
49
42
onCancel = { ( ) => {
50
- createUserSend ( "CANCEL_CREATE_USER" ) ;
51
43
navigate ( "/users" ) ;
52
44
} }
53
- isLoading = { createUserState . hasTag ( "loading" ) }
45
+ isLoading = { createUserMutation . isLoading }
54
46
myOrgId = { myOrgId }
55
47
/>
56
48
</ Margins >
0 commit comments