2019-12-07 00:09:01 +07:00
|
|
|
import React, { memo } from 'react';
|
|
|
|
import styled from '@emotion/styled';
|
2020-03-08 22:45:07 +07:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2019-12-07 00:09:01 +07:00
|
|
|
import useForm from 'react-hook-form/dist/react-hook-form.ie11';
|
|
|
|
|
|
|
|
import TextField from '../../muiComponents/TextField';
|
|
|
|
import Button from '../../muiComponents/Button';
|
|
|
|
import { Theme } from '../../design-tokens/theme';
|
|
|
|
import { LoginError } from '../../utils/login';
|
|
|
|
|
|
|
|
import LoginDialogFormError from './LoginDialogFormError';
|
|
|
|
|
|
|
|
const StyledForm = styled('form')<{ theme?: Theme }>(({ theme }) => ({
|
|
|
|
marginTop: theme.spacing(1),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledButton = styled(Button)<{ theme?: Theme }>(({ theme }) => ({
|
|
|
|
margin: theme.spacing(3, 0, 2),
|
|
|
|
}));
|
|
|
|
|
|
|
|
export interface FormValues {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
onSubmit: (formValues: FormValues) => void;
|
|
|
|
error?: LoginError;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LoginDialogForm = memo(({ onSubmit, error }: Props) => {
|
2020-03-08 22:45:07 +07:00
|
|
|
const { t } = useTranslation();
|
2019-12-07 00:09:01 +07:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
errors,
|
|
|
|
handleSubmit,
|
|
|
|
formState: { isValid },
|
|
|
|
} = useForm<FormValues>({ mode: 'onChange' });
|
|
|
|
|
|
|
|
const onSubmitForm = (formValues: FormValues) => {
|
|
|
|
onSubmit(formValues);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StyledForm noValidate={true} onSubmit={handleSubmit(onSubmitForm)}>
|
|
|
|
<TextField
|
|
|
|
autoComplete="username"
|
|
|
|
error={!!errors.username}
|
|
|
|
fullWidth={true}
|
|
|
|
helperText={errors.username?.message}
|
|
|
|
id="login--dialog-username"
|
|
|
|
inputRef={register({
|
2020-03-08 22:45:07 +07:00
|
|
|
required: { value: true, message: t('form-validation.required-field') },
|
|
|
|
minLength: { value: 2, message: t('form-validation.required-min-length', { length: 2 }) },
|
2019-12-07 00:09:01 +07:00
|
|
|
})}
|
2020-03-08 22:45:07 +07:00
|
|
|
label={t('form.username')}
|
2019-12-07 00:09:01 +07:00
|
|
|
margin="normal"
|
|
|
|
name="username"
|
2020-03-08 22:45:07 +07:00
|
|
|
placeholder={t('form-placeholder.username')}
|
2019-12-07 00:09:01 +07:00
|
|
|
required={true}
|
|
|
|
variant="outlined"
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
autoComplete="current-password"
|
|
|
|
error={!!errors.password}
|
|
|
|
fullWidth={true}
|
|
|
|
helperText={errors.password?.message}
|
|
|
|
id="login--dialog-password"
|
|
|
|
inputRef={register({
|
2020-03-08 22:45:07 +07:00
|
|
|
required: { value: true, message: t('form-validation.required-field') },
|
|
|
|
minLength: { value: 2, message: t('form-validation.required-min-length', { length: 2 }) },
|
2019-12-07 00:09:01 +07:00
|
|
|
})}
|
2020-03-08 22:45:07 +07:00
|
|
|
label={t('form.password')}
|
2019-12-07 00:09:01 +07:00
|
|
|
margin="normal"
|
|
|
|
name="password"
|
2020-03-08 22:45:07 +07:00
|
|
|
placeholder={t('form-placeholder.password')}
|
2019-12-07 00:09:01 +07:00
|
|
|
required={true}
|
|
|
|
type="password"
|
|
|
|
variant="outlined"
|
|
|
|
/>
|
|
|
|
{error && <LoginDialogFormError error={error} />}
|
|
|
|
<StyledButton
|
|
|
|
color="primary"
|
2020-03-08 22:45:07 +07:00
|
|
|
data-testid="login-dialog-form-login-button"
|
2019-12-07 00:09:01 +07:00
|
|
|
disabled={!isValid}
|
|
|
|
fullWidth={true}
|
|
|
|
id="login--dialog-button-submit"
|
|
|
|
size="large"
|
|
|
|
type="submit"
|
|
|
|
variant="contained">
|
2020-03-08 22:45:07 +07:00
|
|
|
{t('button.login')}
|
2019-12-07 00:09:01 +07:00
|
|
|
</StyledButton>
|
|
|
|
</StyledForm>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default LoginDialogForm;
|