From 44f64668b8109a017a0764a328890a7fa27dcb6e Mon Sep 17 00:00:00 2001 From: Wes Date: Mon, 9 Dec 2024 15:00:22 -0500 Subject: [PATCH] BED-5065: Add field specific error to principal name Move error check to useEffect --- .../CreateUserForm/CreateUserForm.tsx | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/javascript/bh-shared-ui/src/components/CreateUserForm/CreateUserForm.tsx b/packages/javascript/bh-shared-ui/src/components/CreateUserForm/CreateUserForm.tsx index fec60be0b..bbb508491 100644 --- a/packages/javascript/bh-shared-ui/src/components/CreateUserForm/CreateUserForm.tsx +++ b/packages/javascript/bh-shared-ui/src/components/CreateUserForm/CreateUserForm.tsx @@ -49,6 +49,7 @@ const CreateUserForm: React.FC<{ handleSubmit, setValue, formState: { errors }, + setError, } = useForm({ defaultValues: { emailAddress: '', @@ -68,7 +69,15 @@ const CreateUserForm: React.FC<{ if (authenticationMethod === 'password') { setValue('SSOProviderId', undefined); } - }, [authenticationMethod, setValue]); + + if (error) { + if (error.response?.data?.errors[0]?.message == 'principal name must be unique') { + setError('principal', { type: 'custom', message: 'Principal name is already in use.' }); + } else { + setError('generic', { type: 'custom', message: 'An unexpected error occurred. Please try again.' }); + } + } + }, [authenticationMethod, setValue, error, setError]); const getRolesQuery = useQuery(['getRoles'], ({ signal }) => apiClient.getRoles({ signal }).then((res) => res.data?.data?.roles) @@ -83,14 +92,6 @@ const CreateUserForm: React.FC<{ onCancel(); }; - const checkError = (err): string => { - if (err.response?.data?.errors[0]?.message == 'principal name must be unique') { - return 'Principal name is already in use.'; - } else { - return 'An unexpected error occurred. Please try again.'; - } - }; - return (
{!(getRolesQuery.isLoading || listSSOProvidersQuery.isLoading) && ( @@ -344,9 +345,9 @@ const CreateUserForm: React.FC<{ )} /> - {error && ( + {!!errors.generic && ( - {checkError(error)} + {errors.generic.message} )}