2020-04-08 21:12:58 +05:00
|
|
|
import dequal from 'dequal';
|
2020-04-09 18:27:28 +05:00
|
|
|
import pickBy from 'lodash/pickBy';
|
|
|
|
import React, { useCallback, useMemo, useRef } from 'react';
|
2020-04-08 21:12:58 +05:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { Button, Form, Input } from 'semantic-ui-react';
|
|
|
|
|
|
|
|
import { useForm } from '../../../hooks';
|
|
|
|
|
2020-05-29 19:31:19 +05:00
|
|
|
import styles from './EditInformation.module.scss';
|
2020-04-08 21:12:58 +05:00
|
|
|
|
|
|
|
const EditInformation = React.memo(({ defaultData, onUpdate }) => {
|
|
|
|
const [t] = useTranslation();
|
|
|
|
|
2020-04-09 18:27:28 +05:00
|
|
|
const [data, handleFieldChange] = useForm(() => ({
|
2020-04-08 21:12:58 +05:00
|
|
|
name: '',
|
2020-04-09 18:27:28 +05:00
|
|
|
phone: '',
|
|
|
|
organization: '',
|
|
|
|
...pickBy(defaultData),
|
|
|
|
}));
|
2020-04-08 21:12:58 +05:00
|
|
|
|
2020-04-09 18:27:28 +05:00
|
|
|
const cleanData = useMemo(
|
|
|
|
() => ({
|
2020-04-08 21:12:58 +05:00
|
|
|
...data,
|
|
|
|
name: data.name.trim(),
|
2020-04-09 18:27:28 +05:00
|
|
|
phone: data.phone.trim() || null,
|
|
|
|
organization: data.organization.trim() || null,
|
|
|
|
}),
|
|
|
|
[data],
|
|
|
|
);
|
2020-04-08 21:12:58 +05:00
|
|
|
|
2020-04-09 18:27:28 +05:00
|
|
|
const nameField = useRef(null);
|
|
|
|
|
|
|
|
const handleSubmit = useCallback(() => {
|
2020-04-08 21:12:58 +05:00
|
|
|
if (!cleanData.name) {
|
|
|
|
nameField.current.select();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
onUpdate(cleanData);
|
2020-04-09 18:27:28 +05:00
|
|
|
}, [onUpdate, cleanData]);
|
2020-04-08 21:12:58 +05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<div className={styles.text}>{t('common.name')}</div>
|
|
|
|
<Input
|
|
|
|
fluid
|
|
|
|
ref={nameField}
|
|
|
|
name="name"
|
|
|
|
value={data.name}
|
|
|
|
className={styles.field}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
2020-04-09 18:27:28 +05:00
|
|
|
<div className={styles.text}>{t('common.phone')}</div>
|
|
|
|
<Input
|
|
|
|
fluid
|
|
|
|
name="phone"
|
|
|
|
value={data.phone}
|
|
|
|
className={styles.field}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
<div className={styles.text}>{t('common.organization')}</div>
|
|
|
|
<Input
|
|
|
|
fluid
|
|
|
|
name="organization"
|
|
|
|
value={data.organization}
|
|
|
|
className={styles.field}
|
|
|
|
onChange={handleFieldChange}
|
|
|
|
/>
|
|
|
|
<Button positive disabled={dequal(cleanData, defaultData)} content={t('action.save')} />
|
2020-04-08 21:12:58 +05:00
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
EditInformation.propTypes = {
|
|
|
|
defaultData: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
|
|
|
|
onUpdate: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EditInformation;
|