@@ -3,7 +3,7 @@ import { Button, Flex, FlexProps, Text } from '@chakra-ui/react'
3
3
import { useMutation , UseMutationOptions , useQueryClient } from '@tanstack/react-query'
4
4
import { useClient } from '@vocdoni/react-providers'
5
5
import { Account , RemoteSigner } from '@vocdoni/sdk'
6
- import { useEffect , useState } from 'react'
6
+ import { useState } from 'react'
7
7
import { FormProvider , useForm } from 'react-hook-form'
8
8
import { Trans , useTranslation } from 'react-i18next'
9
9
import { Link as ReactRouterLink , To , useNavigate } from 'react-router-dom'
@@ -21,17 +21,60 @@ type FormData = PrivateOrgFormData & CreateOrgParams
21
21
22
22
type OrganizationCreateResponse = {
23
23
address : string
24
+ account : Account
25
+ signer : RemoteSigner
26
+ client : ReturnType < typeof useClient > [ 'client' ]
24
27
}
25
28
26
29
const useOrganizationCreate = (
27
- options ?: Omit < UseMutationOptions < OrganizationCreateResponse , Error , CreateOrgParams > , 'mutationFn' >
30
+ options ?: Omit < UseMutationOptions < OrganizationCreateResponse , Error , FormData > , 'mutationFn' >
28
31
) => {
29
32
const { bearedFetch } = useAuth ( )
30
- const client = useQueryClient ( )
31
- return useMutation < OrganizationCreateResponse , Error , CreateOrgParams > ( {
32
- mutationFn : ( params : CreateOrgParams ) => bearedFetch ( ApiEndpoints . Organizations , { body : params , method : 'POST' } ) ,
33
- onSuccess : ( ) => {
34
- client . invalidateQueries ( { queryKey : QueryKeys . profile } )
33
+ const { client, setSigner, signer : csigner } = useClient ( )
34
+ const { bearer, signerRefresh } = useAuthProvider ( )
35
+ const qclient = useQueryClient ( )
36
+
37
+ return useMutation < OrganizationCreateResponse , Error , FormData > ( {
38
+ mutationFn : async ( values : FormData ) => {
39
+ // Create account on the saas to generate new priv keys
40
+ const { address } : { address : string } = await bearedFetch ( ApiEndpoints . Organizations , {
41
+ body : {
42
+ name : values . name ,
43
+ website : values . website ,
44
+ description : values . description ,
45
+ size : values . size ?. value ,
46
+ country : values . country ?. value ,
47
+ type : values . type ?. value ,
48
+ communications : values . communications ,
49
+ } ,
50
+ method : 'POST' ,
51
+ } )
52
+
53
+ const signer = new RemoteSigner ( {
54
+ url : import . meta. env . SAAS_URL ,
55
+ token : bearer ,
56
+ } )
57
+
58
+ signer . address = address
59
+ client . wallet = signer
60
+
61
+ const account = new Account ( {
62
+ name : typeof values . name === 'object' ? values . name . default : values . name ,
63
+ description : typeof values . description === 'object' ? values . description . default : values . description ,
64
+ } )
65
+
66
+ await client . createAccount ( { account } )
67
+
68
+ localStorage . setItem ( LocalStorageKeys . SignerAddress , address )
69
+ qclient . invalidateQueries ( { queryKey : QueryKeys . profile } )
70
+
71
+ // Refresh the signer if it was already set
72
+ if ( csigner !== null ) {
73
+ setSigner ( signer )
74
+ await signerRefresh ( )
75
+ }
76
+
77
+ return { address, account, signer, client }
35
78
} ,
36
79
...options ,
37
80
} )
@@ -49,73 +92,33 @@ export const OrganizationCreate = ({
49
92
const navigate = useNavigate ( )
50
93
const [ isPending , setIsPending ] = useState ( false )
51
94
const methods = useForm < FormData > ( )
95
+ const { fetchAccount } = useClient ( )
52
96
const { handleSubmit } = methods
53
- const { bearer, signerRefresh } = useAuthProvider ( )
54
- const { client, fetchAccount, setClient, setSigner, signer : osigner } = useClient ( )
55
- const [ promiseError , setPromiseError ] = useState < Error | null > ( null )
56
- const [ redirect , setRedirect ] = useState < To | null > ( null )
57
-
58
- const { mutateAsync : createSaasAccount , isError : isSaasError , error : saasError } = useOrganizationCreate ( )
59
97
60
- const error = saasError || promiseError
61
- const isError = isSaasError || ! ! promiseError
98
+ const {
99
+ mutateAsync : createOrganization ,
100
+ isError,
101
+ error,
102
+ } = useOrganizationCreate ( {
103
+ onSuccess : async ( { signer } ) => {
104
+ navigate ( onSuccessRoute )
105
+ setTimeout ( async ( ) => {
106
+ await fetchAccount ( )
107
+ } , 50 )
108
+ } ,
109
+ } )
62
110
63
- const onSubmit = ( values : FormData ) => {
111
+ const onSubmit = async ( values : FormData ) => {
64
112
setIsPending ( true )
65
- // Create account on the saas to generate new priv keys
66
- createSaasAccount ( {
67
- name : values . name ,
68
- website : values . website ,
69
- description : values . description ,
70
- size : values . size ?. value ,
71
- country : values . country ?. value ,
72
- type : values . type ?. value ,
73
- communications : values . communications ,
74
- } )
75
- . then ( ( { address } : { address : string } ) => {
76
- const signer = new RemoteSigner ( {
77
- url : import . meta. env . SAAS_URL ,
78
- token : bearer ,
79
- } )
80
-
81
- signer . address = address
82
- client . wallet = signer
83
-
84
- // setting the signer when there's no signer causes the page to reload
85
- if ( osigner !== null ) {
86
- setSigner ( signer )
87
- setClient ( client )
88
- }
89
- localStorage . setItem ( LocalStorageKeys . SignerAddress , address )
90
-
91
- return client . createAccount ( {
92
- account : new Account ( {
93
- name : typeof values . name === 'object' ? values . name . default : values . name ,
94
- description : typeof values . description === 'object' ? values . description . default : values . description ,
95
- } ) ,
96
- } )
97
- } )
98
- // store redirect
99
- . then ( ( ) => {
100
- setRedirect ( onSuccessRoute )
101
- } )
102
- . catch ( ( e ) => {
103
- setPromiseError ( e )
104
- } )
105
- . finally ( ( ) => {
106
- setIsPending ( false )
107
- } )
113
+ try {
114
+ await createOrganization ( values )
115
+ } catch ( e ) {
116
+ // Error handling is managed by react-query
117
+ } finally {
118
+ setIsPending ( false )
119
+ }
108
120
}
109
121
110
- // redirect on success
111
- useEffect ( ( ) => {
112
- if ( ! redirect ) return
113
- // update account and signer
114
- fetchAccount ( ) . then ( ( ) => signerRefresh ( ) )
115
- // "redirect"
116
- navigate ( redirect )
117
- } , [ redirect ] )
118
-
119
122
return (
120
123
< FormProvider { ...methods } >
121
124
< Flex
0 commit comments