You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
5.8 KiB
191 lines
5.8 KiB
|
6 years ago
|
import {
|
||
|
|
EmailInput,
|
||
|
|
Field,
|
||
|
|
FieldGroup,
|
||
|
|
Icon,
|
||
|
6 years ago
|
Margins,
|
||
|
6 years ago
|
PasswordInput,
|
||
|
|
TextInput,
|
||
|
|
} from '@rocket.chat/fuselage';
|
||
|
6 years ago
|
import { useAutoFocus, useUniqueId } from '@rocket.chat/fuselage-hooks';
|
||
|
6 years ago
|
import React, { useMemo, useState } from 'react';
|
||
|
|
|
||
|
6 years ago
|
import { useMethod } from '../../../contexts/ServerContext';
|
||
|
|
import { useSessionDispatch } from '../../../contexts/SessionContext';
|
||
|
|
import { useSetting } from '../../../contexts/SettingsContext';
|
||
|
|
import { useToastMessageDispatch } from '../../../contexts/ToastMessagesContext';
|
||
|
|
import { useTranslation } from '../../../contexts/TranslationContext';
|
||
|
|
import { useLoginWithPassword } from '../../../contexts/UserContext';
|
||
|
|
import { useCallbacks } from '../../../hooks/useCallbacks';
|
||
|
|
import { Pager } from '../Pager';
|
||
|
6 years ago
|
import { Step } from '../Step';
|
||
|
|
import { StepHeader } from '../StepHeader';
|
||
|
|
|
||
|
6 years ago
|
function AdminUserInformationStep({ step, title, active }) {
|
||
|
6 years ago
|
const loginWithPassword = useLoginWithPassword();
|
||
|
|
const registerUser = useMethod('registerUser');
|
||
|
|
const defineUsername = useMethod('setUsername');
|
||
|
6 years ago
|
|
||
|
6 years ago
|
const setForceLogin = useSessionDispatch('forceLogin');
|
||
|
|
const callbacks = useCallbacks();
|
||
|
|
const dispatchToastMessage = useToastMessageDispatch();
|
||
|
|
|
||
|
6 years ago
|
const registerAdminUser = async ({ name, username, email, password, onRegistrationEmailSent }) => {
|
||
|
|
await registerUser({ name, username, email, pass: password });
|
||
|
|
callbacks.run('userRegistered');
|
||
|
6 years ago
|
|
||
|
6 years ago
|
try {
|
||
|
|
await loginWithPassword(email, password);
|
||
|
|
} catch (error) {
|
||
|
|
if (error.error === 'error-invalid-email') {
|
||
|
|
onRegistrationEmailSent && onRegistrationEmailSent();
|
||
|
|
return;
|
||
|
|
}
|
||
|
6 years ago
|
dispatchToastMessage({ type: 'error', message: error });
|
||
|
6 years ago
|
throw error;
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
setForceLogin(false);
|
||
|
6 years ago
|
|
||
|
6 years ago
|
await defineUsername(username);
|
||
|
|
callbacks.run('usernameSet');
|
||
|
|
};
|
||
|
6 years ago
|
|
||
|
|
const regexpForUsernameValidation = useSetting('UTF8_Names_Validation');
|
||
|
|
const usernameRegExp = useMemo(() => new RegExp(`^${ regexpForUsernameValidation }$`), [regexpForUsernameValidation]);
|
||
|
|
const emailRegExp = useMemo(() => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]+$/i, []);
|
||
|
|
|
||
|
|
const [name, setName] = useState('');
|
||
|
|
const [username, setUsername] = useState('');
|
||
|
|
const [email, setEmail] = useState('');
|
||
|
|
const [password, setPassword] = useState('');
|
||
|
|
|
||
|
|
const [isNameValid, validateName] = useState(true);
|
||
|
|
const [isUsernameValid, validateUsername] = useState(true);
|
||
|
|
const [isEmailValid, validateEmail] = useState(true);
|
||
|
|
const [isPasswordValid, validatePassword] = useState(true);
|
||
|
|
|
||
|
|
const isContinueEnabled = useMemo(() => name && username && email && password, [name, username, email, password]);
|
||
|
|
|
||
|
|
const [commiting, setCommiting] = useState(false);
|
||
|
|
|
||
|
|
const validate = () => {
|
||
|
|
const isNameValid = !!name;
|
||
|
|
const isUsernameValid = !!username && usernameRegExp.test(username);
|
||
|
|
const isEmailValid = !!email && emailRegExp.test(email);
|
||
|
|
const isPasswordValid = !!password;
|
||
|
|
|
||
|
|
validateName(isNameValid);
|
||
|
|
validateUsername(isUsernameValid);
|
||
|
|
validateEmail(isEmailValid);
|
||
|
|
validatePassword(isPasswordValid);
|
||
|
|
|
||
|
|
return isNameValid && isUsernameValid && isEmailValid && isPasswordValid;
|
||
|
|
};
|
||
|
|
|
||
|
|
const t = useTranslation();
|
||
|
|
|
||
|
6 years ago
|
const autoFocusRef = useAutoFocus(active);
|
||
|
6 years ago
|
|
||
|
|
const handleSubmit = async (event) => {
|
||
|
|
event.preventDefault();
|
||
|
|
|
||
|
|
const canRegisterAdminUser = validate();
|
||
|
|
|
||
|
|
if (!canRegisterAdminUser) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
setCommiting(true);
|
||
|
|
|
||
|
|
try {
|
||
|
|
await registerAdminUser({
|
||
|
|
name,
|
||
|
|
username,
|
||
|
|
email,
|
||
|
|
password,
|
||
|
6 years ago
|
onRegistrationEmailSent: () => {
|
||
|
|
dispatchToastMessage({ type: 'success', message: t('We_have_sent_registration_email') });
|
||
|
|
},
|
||
|
6 years ago
|
});
|
||
|
|
} catch (error) {
|
||
|
|
console.error(error);
|
||
|
|
} finally {
|
||
|
|
setCommiting(false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
6 years ago
|
const nameInputId = useUniqueId();
|
||
|
|
const usernameInputId = useUniqueId();
|
||
|
|
const emailInputId = useUniqueId();
|
||
|
|
const passwordInputId = useUniqueId();
|
||
|
|
|
||
|
6 years ago
|
return <Step active={active} working={commiting} onSubmit={handleSubmit}>
|
||
|
|
<StepHeader number={step} title={title} />
|
||
|
|
|
||
|
6 years ago
|
<Margins blockEnd='x32'>
|
||
|
6 years ago
|
<FieldGroup>
|
||
|
|
<Field>
|
||
|
6 years ago
|
<Field.Label htmlFor={nameInputId} required>{t('Name')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput
|
||
|
|
ref={autoFocusRef}
|
||
|
|
id={nameInputId}
|
||
|
6 years ago
|
addon={<Icon name='user' size='x20' />}
|
||
|
6 years ago
|
placeholder={t('Type_your_name')}
|
||
|
|
value={name}
|
||
|
|
onChange={({ currentTarget: { value } }) => setName(value)}
|
||
|
6 years ago
|
error={!isNameValid ? 'error' : ''}
|
||
|
6 years ago
|
/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
</Field>
|
||
|
|
<Field>
|
||
|
6 years ago
|
<Field.Label htmlFor={usernameInputId} required>{t('Username')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput
|
||
|
|
id={usernameInputId}
|
||
|
6 years ago
|
addon={<Icon name='at' size='x20' />}
|
||
|
6 years ago
|
placeholder={t('Type_your_username')}
|
||
|
|
value={username}
|
||
|
|
onChange={({ currentTarget: { value } }) => setUsername(value)}
|
||
|
6 years ago
|
error={!isUsernameValid ? 'error' : ''}
|
||
|
6 years ago
|
/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
{!isUsernameValid && <Field.Error>{t('Invalid_username')}</Field.Error>}
|
||
|
6 years ago
|
</Field>
|
||
|
|
<Field>
|
||
|
6 years ago
|
<Field.Label htmlFor={emailInputId} required>{t('Organization_Email')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<EmailInput
|
||
|
|
id={emailInputId}
|
||
|
6 years ago
|
addon={<Icon name='mail' size='x20' />}
|
||
|
6 years ago
|
placeholder={t('Type_your_email')}
|
||
|
|
value={email}
|
||
|
|
onChange={({ currentTarget: { value } }) => setEmail(value)}
|
||
|
6 years ago
|
error={!isEmailValid ? 'error' : ''}
|
||
|
6 years ago
|
/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
{!isEmailValid && <Field.Error>{t('Invalid_email')}</Field.Error>}
|
||
|
6 years ago
|
</Field>
|
||
|
|
<Field>
|
||
|
6 years ago
|
<Field.Label htmlFor={passwordInputId} required>{t('Password')}</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<PasswordInput
|
||
|
|
id={passwordInputId}
|
||
|
6 years ago
|
addon={<Icon name='key' size='x20' />}
|
||
|
6 years ago
|
placeholder={t('Type_your_password')}
|
||
|
|
value={password}
|
||
|
|
onChange={({ currentTarget: { value } }) => setPassword(value)}
|
||
|
6 years ago
|
error={!isPasswordValid ? 'error' : ''}
|
||
|
6 years ago
|
/>
|
||
|
|
</Field.Row>
|
||
|
6 years ago
|
</Field>
|
||
|
|
</FieldGroup>
|
||
|
6 years ago
|
</Margins>
|
||
|
6 years ago
|
|
||
|
|
<Pager disabled={commiting} isContinueEnabled={isContinueEnabled} />
|
||
|
|
</Step>;
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
|
export default AdminUserInformationStep;
|