Skip to content

Commit 22c33d2

Browse files
committed
feat: notify user if username already exists
1 parent 8a916e5 commit 22c33d2

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

apps/web/src/routes/_app/admin/users/create.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import { estimatePasswordStrength } from '@douglasneuroinformatics/libpasswd';
44
import { Form, Heading } from '@douglasneuroinformatics/libui/components';
5-
import type { FormProps } from '@douglasneuroinformatics/libui/components';
6-
import { useTranslation } from '@douglasneuroinformatics/libui/hooks';
5+
import { useNotificationsStore, useTranslation } from '@douglasneuroinformatics/libui/hooks';
76
import { $BasePermissionLevel, $CreateUserData } from '@opendatacapture/schemas/user';
87
import type { CreateUserData } from '@opendatacapture/schemas/user';
98
import { createFileRoute, useNavigate } from '@tanstack/react-router';
@@ -19,20 +18,22 @@ const RouteComponent = () => {
1918
const navigate = useNavigate();
2019
const groupsQuery = useGroupsQuery();
2120
const createUserMutation = useCreateUserMutation();
21+
const notification = useNotificationsStore();
2222

23-
const handleSubmit: FormProps<any>['onSubmit'] = async (data: CreateUserData) => {
23+
const handleSubmit = async (data: CreateUserData) => {
2424
// check if username exists
25-
2625
const existingUsername = await axios.get(`/v1/users/check-username/${encodeURIComponent(data.username)}`);
2726

28-
if (existingUsername) {
29-
return { success: false, errorMessage: t('common.usernameExists') };
30-
}
31-
32-
createUserMutation.mutate({ data });
27+
if (existingUsername.data !== false) {
28+
notification.addNotification({
29+
type: 'error',
30+
message: t('common.usernameExists')
31+
});
32+
} else {
33+
createUserMutation.mutate({ data });
3334

34-
void navigate({ to: '..' });
35-
return { success: true };
35+
void navigate({ to: '..' });
36+
}
3637
};
3738

3839
return (

0 commit comments

Comments
 (0)