2019-08-31 04:07:25 +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 { useTranslation } from 'react-i18next';
|
|
|
|
import { Button, Form, Message } from 'semantic-ui-react';
|
2019-11-15 03:45:59 +05:00
|
|
|
import { usePrevious } from '../../lib/hooks';
|
2019-08-31 04:07:25 +05:00
|
|
|
import { withPopup } from '../../lib/popup';
|
|
|
|
import { Input, Popup } from '../../lib/custom-ui';
|
|
|
|
|
2019-11-15 03:45:59 +05:00
|
|
|
import { useForm } from '../../hooks';
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
import styles from './AddUserPopup.module.css';
|
|
|
|
|
2020-03-25 00:15:47 +05:00
|
|
|
const createMessage = (error) => {
|
2019-10-18 08:06:34 +05:00
|
|
|
if (!error) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.message === 'User is already exist') {
|
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
content: 'common.userIsAlreadyExist',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'warning',
|
|
|
|
content: 'common.unknownError',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
const AddUserPopup = React.memo(
|
2020-02-03 18:42:31 +05:00
|
|
|
({ defaultData, isSubmitting, error, onCreate, onMessageDismiss, onClose }) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
const wasSubmitting = usePrevious(isSubmitting);
|
|
|
|
|
|
|
|
const [data, handleFieldChange] = useForm(() => ({
|
|
|
|
email: '',
|
|
|
|
password: '',
|
|
|
|
name: '',
|
|
|
|
...defaultData,
|
|
|
|
}));
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const message = useMemo(() => createMessage(error), [error]);
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
const emailField = useRef(null);
|
|
|
|
const passwordField = useRef(null);
|
|
|
|
const nameField = useRef(null);
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
const handleSubmit = useCallback(() => {
|
2019-08-31 04:07:25 +05:00
|
|
|
const cleanData = {
|
|
|
|
...data,
|
|
|
|
email: data.email.trim(),
|
|
|
|
name: data.name.trim(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!isEmail(cleanData.email)) {
|
|
|
|
emailField.current.select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cleanData.password) {
|
|
|
|
passwordField.current.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cleanData.name) {
|
|
|
|
nameField.current.select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onCreate(cleanData);
|
|
|
|
}, [onCreate, data]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
emailField.current.select();
|
|
|
|
}, []);
|
|
|
|
|
2019-10-18 08:06:34 +05:00
|
|
|
useEffect(() => {
|
2019-08-31 04:07:25 +05:00
|
|
|
if (wasSubmitting && !isSubmitting) {
|
|
|
|
if (!error) {
|
|
|
|
onClose();
|
2019-10-18 08:06:34 +05:00
|
|
|
} else if (error.message === 'User is already exist') {
|
2019-08-31 04:07:25 +05:00
|
|
|
emailField.current.select();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [isSubmitting, wasSubmitting, error, onClose]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Popup.Header>
|
|
|
|
{t('common.addUser', {
|
|
|
|
context: 'title',
|
|
|
|
})}
|
|
|
|
</Popup.Header>
|
|
|
|
<Popup.Content>
|
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 onSubmit={handleSubmit}>
|
|
|
|
<div className={styles.text}>{t('common.email')}</div>
|
|
|
|
<Input
|
|
|
|
fluid
|
|
|
|
ref={emailField}
|
|
|
|
name="email"
|
|
|
|
value={data.email}
|
|
|
|
readOnly={isSubmitting}
|
|
|
|
className={styles.field}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
<div className={styles.text}>{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.field}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
<div className={styles.text}>{t('common.name')}</div>
|
|
|
|
<Input
|
|
|
|
fluid
|
|
|
|
ref={nameField}
|
|
|
|
name="name"
|
|
|
|
value={data.name}
|
|
|
|
readOnly={isSubmitting}
|
|
|
|
className={styles.field}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
positive
|
|
|
|
content={t('action.addUser')}
|
|
|
|
loading={isSubmitting}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
</Popup.Content>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
AddUserPopup.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
|
|
|
|
onCreate: PropTypes.func.isRequired,
|
|
|
|
onMessageDismiss: PropTypes.func.isRequired,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
AddUserPopup.defaultProps = {
|
|
|
|
error: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withPopup(AddUserPopup);
|