22
33import { estimatePasswordStrength } from '@douglasneuroinformatics/libpasswd' ;
44import { Form , Heading } from '@douglasneuroinformatics/libui/components' ;
5+ import type { FormProps } from '@douglasneuroinformatics/libui/components' ;
56import { useTranslation } from '@douglasneuroinformatics/libui/hooks' ;
67import { $BasePermissionLevel , $CreateUserData } from '@opendatacapture/schemas/user' ;
7- import type { CreateUserData , User } from '@opendatacapture/schemas/user' ;
8+ import type { CreateUserData } from '@opendatacapture/schemas/user' ;
89import { createFileRoute , useNavigate } from '@tanstack/react-router' ;
910import axios from 'axios' ;
1011import { z } from 'zod/v4' ;
@@ -13,19 +14,26 @@ import { PageHeader } from '@/components/PageHeader';
1314import { useCreateUserMutation } from '@/hooks/useCreateUserMutation' ;
1415import { groupsQueryOptions , useGroupsQuery } from '@/hooks/useGroupsQuery' ;
1516
16- const RouteComponent = async ( ) => {
17+ const RouteComponent = ( ) => {
1718 const { t } = useTranslation ( ) ;
1819 const navigate = useNavigate ( ) ;
1920 const groupsQuery = useGroupsQuery ( ) ;
2021 const createUserMutation = useCreateUserMutation ( ) ;
2122
22- const handleSubmit = ( data : CreateUserData ) => {
23+ const handleSubmit : FormProps < any > [ 'onSubmit' ] = async ( data : CreateUserData ) => {
24+ // check if username exists
25+
26+ const exisitingUsername = await axios . get ( `/v1/users/check-username/${ encodeURIComponent ( data . username ) } ` ) ;
27+ if ( exisitingUsername ) {
28+ return { success : false , errorMessage : t ( 'common.usernameExists' ) } ;
29+ }
30+
2331 createUserMutation . mutate ( { data } ) ;
32+
2433 void navigate ( { to : '..' } ) ;
34+ return { success : true } ;
2535 } ;
2636
27- const existingUsers = await axios . get ( `/v1/users` ) ;
28-
2937 return (
3038 < div >
3139 < PageHeader >
@@ -158,18 +166,6 @@ const RouteComponent = async () => {
158166 path : [ 'confirmPassword' ]
159167 } ) ;
160168 }
161-
162- if ( existingUsers . data ) {
163- const usersList = existingUsers . data as Omit < User , 'hashedpassword' > [ ] ;
164-
165- if ( usersList . some ( ( usersList ) => usersList . username === ctx . value . username ) )
166- ctx . issues . push ( {
167- code : 'custom' ,
168- input : ctx . value . username ,
169- message : t ( 'common.usernameExists' ) ,
170- path : [ 'username' ]
171- } ) ;
172- }
173169 } ) }
174170 onSubmit = { ( data ) => handleSubmit ( { ...data , groupIds : Array . from ( data . groupIds ?? [ ] ) } ) }
175171 />
0 commit comments