Skip to content

Commit

Permalink
BED-5065: Add field specific error to principal name
Browse files Browse the repository at this point in the history
Move error check to useEffect
  • Loading branch information
wes-mil committed Dec 9, 2024
1 parent bca2808 commit 44f6466
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const CreateUserForm: React.FC<{
handleSubmit,
setValue,
formState: { errors },
setError,
} = useForm<CreateUserRequestForm>({
defaultValues: {
emailAddress: '',
Expand All @@ -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)
Expand All @@ -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 (
<form autoComplete='off' onSubmit={handleSubmit(onSubmit)}>
{!(getRolesQuery.isLoading || listSSOProvidersQuery.isLoading) && (
Expand Down Expand Up @@ -344,9 +345,9 @@ const CreateUserForm: React.FC<{
)}
/>
</Grid>
{error && (
{!!errors.generic && (
<Grid item xs={12}>
<Alert severity='error'>{checkError(error)}</Alert>
<Alert severity='error'>{errors.generic.message}</Alert>
</Grid>
)}
</Grid>
Expand Down

0 comments on commit 44f6466

Please sign in to comment.