2020-04-28 19:46:55 +05:00
|
|
|
import isEmail from 'validator/lib/isEmail';
|
2020-02-03 18:42:31 +05:00
|
|
|
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
|
2019-08-31 04:07:25 +05:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2020-02-03 18:42:31 +05:00
|
|
|
import { Form, Grid, Header, Message } from 'semantic-ui-react';
|
2019-11-15 03:45:59 +05:00
|
|
|
import { useDidUpdate, usePrevious, useToggle } from '../../lib/hooks';
|
2019-08-31 04:07:25 +05:00
|
|
|
import { Input } from '../../lib/custom-ui';
|
|
|
|
|
2019-11-15 03:45:59 +05:00
|
|
|
import { useForm } from '../../hooks';
|
2020-04-03 00:35:25 +05:00
|
|
|
import { isUsername } from '../../utils/validator';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
import styles from './Login.module.scss';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-03-25 00:15:47 +05:00
|
|
|
const createMessage = (error) => {
|
2019-10-18 08:06:34 +05:00
|
|
|
if (!error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (error.message) {
|
2020-04-03 00:35:25 +05:00
|
|
|
case 'Invalid email or username':
|
2019-10-18 08:06:34 +05:00
|
|
|
return {
|
|
|
|
type: 'error',
|
2020-04-03 00:35:25 +05:00
|
|
|
content: 'common.invalidEmailOrUsername',
|
2019-10-18 08:06:34 +05:00
|
|
|
};
|
2020-04-03 00:35:25 +05:00
|
|
|
case 'Invalid password':
|
2019-10-18 08:06:34 +05:00
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
content: 'common.invalidPassword',
|
|
|
|
};
|
|
|
|
case 'Failed to fetch':
|
|
|
|
return {
|
|
|
|
type: 'warning',
|
|
|
|
content: 'common.noInternetConnection',
|
|
|
|
};
|
|
|
|
case 'Network request failed':
|
|
|
|
return {
|
|
|
|
type: 'warning',
|
|
|
|
content: 'common.serverConnectionFailed',
|
|
|
|
};
|
|
|
|
default:
|
|
|
|
return {
|
|
|
|
type: 'warning',
|
|
|
|
content: 'common.unknownError',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
const Login = React.memo(
|
2020-02-03 18:42:31 +05:00
|
|
|
({ defaultData, isSubmitting, error, onAuthenticate, onMessageDismiss }) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
const wasSubmitting = usePrevious(isSubmitting);
|
|
|
|
|
|
|
|
const [data, handleFieldChange, setData] = useForm(() => ({
|
2020-04-03 00:35:25 +05:00
|
|
|
emailOrUsername: '',
|
2019-08-31 04:07:25 +05:00
|
|
|
password: '',
|
|
|
|
...defaultData,
|
|
|
|
}));
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const message = useMemo(() => createMessage(error), [error]);
|
2019-08-31 04:07:25 +05:00
|
|
|
const [focusPasswordFieldState, focusPasswordField] = useToggle();
|
|
|
|
|
2020-04-03 00:35:25 +05:00
|
|
|
const emailOrUsernameField = useRef(null);
|
2019-08-31 04:07:25 +05:00
|
|
|
const passwordField = useRef(null);
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const handleSubmit = useCallback(() => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const cleanData = {
|
|
|
|
...data,
|
2020-04-03 00:35:25 +05:00
|
|
|
emailOrUsername: data.emailOrUsername.trim(),
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
2020-04-03 00:35:25 +05:00
|
|
|
if (!isEmail(cleanData.emailOrUsername) && !isUsername(cleanData.emailOrUsername)) {
|
|
|
|
emailOrUsernameField.current.select();
|
2019-08-31 04:07:25 +05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cleanData.password) {
|
|
|
|
passwordField.current.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onAuthenticate(cleanData);
|
|
|
|
}, [onAuthenticate, data]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-26 12:49:40 +02:00
|
|
|
emailOrUsernameField.current.focus();
|
2019-08-31 04:07:25 +05:00
|
|
|
}, []);
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
useEffect(() => {
|
2019-08-31 04:07:25 +05:00
|
|
|
if (wasSubmitting && !isSubmitting && error) {
|
|
|
|
switch (error.message) {
|
2020-04-03 00:35:25 +05:00
|
|
|
case 'Invalid email or username':
|
|
|
|
emailOrUsernameField.current.select();
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
break;
|
2020-04-03 00:35:25 +05:00
|
|
|
case 'Invalid password':
|
2020-03-25 00:15:47 +05:00
|
|
|
setData((prevData) => ({
|
2019-08-31 04:07:25 +05:00
|
|
|
...prevData,
|
|
|
|
password: '',
|
|
|
|
}));
|
|
|
|
focusPasswordField();
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [isSubmitting, wasSubmitting, error, setData, focusPasswordField]);
|
|
|
|
|
|
|
|
useDidUpdate(() => {
|
|
|
|
passwordField.current.focus();
|
|
|
|
}, [focusPasswordFieldState]);
|
|
|
|
|
|
|
|
return (
|
2020-05-29 19:31:19 +05:00
|
|
|
<div className={classNames(styles.wrapper, styles.fullHeight)}>
|
2019-08-31 04:07:25 +05:00
|
|
|
<Grid verticalAlign="middle" className={styles.fullHeightPaddingFix}>
|
|
|
|
<Grid.Column widescreen={4} largeScreen={5} computer={6} tablet={16} mobile={16}>
|
|
|
|
<Grid verticalAlign="middle" className={styles.fullHeightPaddingFix}>
|
|
|
|
<Grid.Column>
|
|
|
|
<div className={styles.loginWrapper}>
|
|
|
|
<Header
|
|
|
|
as="h1"
|
|
|
|
textAlign="center"
|
|
|
|
content={t('common.logInToPlanka')}
|
|
|
|
className={styles.formTitle}
|
|
|
|
/>
|
|
|
|
<div>
|
2019-10-18 08:06:34 +05:00
|
|
|
{message && (
|
2019-08-31 04:07:25 +05:00
|
|
|
<Message
|
|
|
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
|
|
|
{...{
|
2019-10-18 08:06:34 +05:00
|
|
|
[message.type]: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
}}
|
|
|
|
visible
|
2019-10-18 08:06:34 +05:00
|
|
|
content={t(message.content)}
|
2019-08-31 04:07:25 +05:00
|
|
|
onDismiss={onMessageDismiss}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Form size="large" onSubmit={handleSubmit}>
|
|
|
|
<div className={styles.inputWrapper}>
|
2020-04-03 00:35:25 +05:00
|
|
|
<div className={styles.inputLabel}>{t('common.emailOrUsername')}</div>
|
2019-08-31 04:07:25 +05:00
|
|
|
<Input
|
|
|
|
fluid
|
2020-04-03 00:35:25 +05:00
|
|
|
ref={emailOrUsernameField}
|
|
|
|
name="emailOrUsername"
|
|
|
|
value={data.emailOrUsername}
|
2019-08-31 04:07:25 +05:00
|
|
|
readOnly={isSubmitting}
|
|
|
|
className={styles.input}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className={styles.inputWrapper}>
|
|
|
|
<div className={styles.inputLabel}>{t('common.password')}</div>
|
2019-11-15 03:45:59 +05:00
|
|
|
<Input.Password
|
2019-08-31 04:07:25 +05:00
|
|
|
fluid
|
|
|
|
ref={passwordField}
|
|
|
|
name="password"
|
|
|
|
value={data.password}
|
|
|
|
readOnly={isSubmitting}
|
|
|
|
className={styles.input}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Form.Button
|
|
|
|
primary
|
|
|
|
size="large"
|
|
|
|
icon="right arrow"
|
|
|
|
labelPosition="right"
|
|
|
|
content={t('action.logIn')}
|
|
|
|
floated="right"
|
|
|
|
loading={isSubmitting}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Grid.Column>
|
|
|
|
</Grid>
|
|
|
|
</Grid.Column>
|
|
|
|
<Grid.Column
|
|
|
|
widescreen={12}
|
|
|
|
largeScreen={11}
|
|
|
|
computer={10}
|
|
|
|
only="computer"
|
|
|
|
className={classNames(styles.cover, styles.fullHeight)}
|
|
|
|
>
|
|
|
|
<div className={styles.descriptionWrapperOverlay} />
|
|
|
|
<div className={styles.descriptionWrapper}>
|
|
|
|
<Header inverted as="h1" content="Planka" className={styles.descriptionTitle} />
|
|
|
|
<Header
|
|
|
|
inverted
|
|
|
|
as="h2"
|
2021-04-30 08:26:19 -07:00
|
|
|
content={t('common.projectManagement')}
|
2019-08-31 04:07:25 +05:00
|
|
|
className={styles.descriptionSubtitle}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Grid.Column>
|
|
|
|
</Grid>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Login.propTypes = {
|
|
|
|
defaultData: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
|
|
|
|
isSubmitting: PropTypes.bool.isRequired,
|
|
|
|
error: PropTypes.object, // eslint-disable-line react/forbid-prop-types
|
|
|
|
onAuthenticate: PropTypes.func.isRequired,
|
|
|
|
onMessageDismiss: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
Login.defaultProps = {
|
|
|
|
error: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Login;
|