import type { IUser } from '@rocket.chat/core-typings'; import { TextInput, ButtonGroup, Button, FieldGroup, Field, FieldLabel, FieldRow, FieldError, Box } from '@rocket.chat/fuselage'; import { VerticalWizardLayout, Form, FormContainer, FormFooter, FormHeader, FormSubtitle, FormTitle } from '@rocket.chat/layout'; import { CustomFieldsForm } from '@rocket.chat/ui-client'; import { useSetting, useTranslation, useLogout, useEndpoint, useUserId, useToastMessageDispatch, useAssetWithDarkModePath, useAccountsCustomFields, } from '@rocket.chat/ui-contexts'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useEffect, useId } from 'react'; import { useForm } from 'react-hook-form'; import MarkdownText from '../../../components/MarkdownText'; type RegisterUsernamePayload = { username: Exclude; } & IUser['customFields']; const RegisterUsername = () => { const t = useTranslation(); const uid = useUserId(); const logout = useLogout(); const formLabelId = useId(); const hideLogo = useSetting('Layout_Login_Hide_Logo', false); const customLogo = useAssetWithDarkModePath('logo'); const customBackground = useAssetWithDarkModePath('background'); const dispatchToastMessage = useToastMessageDispatch(); const customFields = useAccountsCustomFields(); if (!uid) { throw new Error('Invalid user'); } const setBasicInfo = useEndpoint('POST', '/v1/users.updateOwnBasicInfo'); const usernameSuggestion = useEndpoint('GET', '/v1/users.getUsernameSuggestion'); const { data, isLoading } = useQuery({ queryKey: ['suggestion'], queryFn: async () => usernameSuggestion(), }); const { register, handleSubmit, setValue, getValues, setError, control, formState: { errors }, } = useForm(); useEffect(() => { if (data?.result && getValues('username') === '') { setValue('username', data.result); } }); const queryClient = useQueryClient(); const registerUsernameMutation = useMutation({ mutationFn: async (data: RegisterUsernamePayload) => { const { username, ...customFields } = data; return Promise.all([setBasicInfo({ data: { username }, customFields })]); }, onSuccess: () => { dispatchToastMessage({ type: 'success', message: t('Username_has_been_updated') }); queryClient.invalidateQueries({ queryKey: ['users.info'] }); // Reload to redo any onLogin request that may have failed without an username location.reload(); }, onError: (error: any, { username }) => { if ([error.error, error.errorType].includes('error-blocked-username')) { return setError('username', { type: 'error-blocked-username', message: t('error-blocked-username', { field: username }) }); } if ([error.errorType].includes('error-field-unavailable')) { return setError('username', { type: 'error-field-unavailable', message: t('error-field-unavailable', { field: username }) }); } if ([error.errorType].includes('')) { return setError('username', { type: 'username-invalid', message: t('Username_invalid', { username }) }); } dispatchToastMessage({ type: 'error', message: error }); }, }); return ( : <>} >
registerUsernameMutation.mutate(data))}> {t('Username_title')} {t('Username_description')} {!isLoading && ( {t('Username')} {errors.username && ( )} )} {isLoading && t('Loading_suggestion')}
); }; export default RegisterUsername;