@@ -7,25 +7,33 @@ import { Input } from "@heroui/input";
77
88import { title } from "@/components/primitives" ;
99import DefaultLayout from "@/layouts/default" ;
10- import { userHasPermission } from "@/components/auth0" ;
10+ import { postJsonToSecuredApi , userHasPermission } from "@/components/auth0" ;
1111
1212export default function AddNewUser ( ) {
1313 const { t } = useTranslation ( ) ;
1414 const { isAuthenticated, getAccessTokenSilently } = useAuth0 ( ) ;
1515
1616 const [ hasPermission , setHasPermission ] = useState ( false ) ;
17- const [ submitted , setSubmitted ] = useState (
17+ const [ apiResponse , setApiResponse ] = useState (
1818 null as {
1919 [ k : string ] : FormDataEntryValue ;
2020 } | null ,
2121 ) ;
2222
23- const onSubmit = ( e : FormEvent < HTMLFormElement > ) => {
23+ const onSubmit = async ( e : FormEvent < HTMLFormElement > ) => {
2424 e . preventDefault ( ) ;
2525
2626 const data = Object . fromEntries ( new FormData ( e . currentTarget ) ) ;
27+ const ids = [ data [ "oauthId" ] as string ] ;
28+ const name = data [ "name" ] as string ;
2729
28- setSubmitted ( data ) ;
30+ const apiResponse = await postJsonToSecuredApi (
31+ `${ import . meta. env . API_BASE_URL } /tester` ,
32+ { name, ids } ,
33+ getAccessTokenSilently ,
34+ ) ;
35+
36+ setApiResponse ( apiResponse ) ;
2937 } ;
3038
3139 useEffect ( ( ) => {
@@ -38,7 +46,9 @@ export default function AddNewUser() {
3846 getAccessTokenSilently ,
3947 ) ,
4048 ) ;
41- console . log ( "You have permission to add a new user." ) ;
49+ console . log (
50+ `You have ${ hasPermission ? "" : "not " } permission to add a new user.` ,
51+ ) ;
4252 } catch ( error ) {
4353 console . log ( error as Error ) ;
4454 }
@@ -60,28 +70,29 @@ export default function AddNewUser() {
6070 < Form className = "w-full max-w-xs" onSubmit = { onSubmit } >
6171 < Input
6272 isRequired
63- errorMessage = { t ( ' please-enter-a-name' ) }
64- label = { t ( ' name' ) }
73+ errorMessage = { t ( " please-enter-a-name" ) }
74+ label = { t ( " name" ) }
6575 labelPlacement = "outside"
6676 name = "name"
67- placeholder = { t ( ' enter-the-user-name' ) }
77+ placeholder = { t ( " enter-the-user-name" ) }
6878 type = "text"
6979 />
7080 < Input
7181 isRequired
72- errorMessage = { t ( ' please-enter-a-oauth-id' ) }
73- label = { t ( ' oauth-id' ) }
82+ errorMessage = { t ( " please-enter-a-oauth-id" ) }
83+ label = { t ( " oauth-id" ) }
7484 labelPlacement = "outside"
7585 name = "oauthId"
76- placeholder = { t ( 'enter-the-oauth-id' ) }
86+ pattern = "^[a-zA-Z0-9]{4,30}\|[a-zA-Z0-9]{4,30}$"
87+ placeholder = { t ( "enter-the-oauth-id" ) }
7788 type = "text"
7889 />
7990 < Button type = "submit" variant = "bordered" >
80- { t ( ' submit' ) }
91+ { t ( " submit" ) }
8192 </ Button >
82- { submitted && (
93+ { apiResponse && (
8394 < div className = "text-small text-default-500" >
84- You submitted : < code > { JSON . stringify ( submitted ) } </ code >
95+ { t ( 'api-response' ) } : < code > { JSON . stringify ( apiResponse ) } </ code >
8596 </ div >
8697 ) }
8798 </ Form >
0 commit comments