3719 |
stevensc |
1 |
import React, { useEffect } from 'react';
|
|
|
2 |
import { useLocation } from 'react-router-dom';
|
|
|
3 |
import { useDispatch } from 'react-redux';
|
|
|
4 |
import { useForm } from 'react-hook-form';
|
|
|
5 |
|
|
|
6 |
import { axios } from '../../utils';
|
|
|
7 |
import { addNotification } from '../../redux/notification/notification.actions';
|
|
|
8 |
|
|
|
9 |
import CKEditor from '@components/common/ckeditor/Ckeditor';
|
|
|
10 |
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
|
|
|
11 |
import Modal from '@components/UI/modal/Modal';
|
|
|
12 |
|
|
|
13 |
const OverviewModal = ({
|
|
|
14 |
isOpen = false,
|
|
|
15 |
overview = '',
|
|
|
16 |
id = '',
|
|
|
17 |
closeModal = function () {},
|
|
|
18 |
onComplete = function () {}
|
|
|
19 |
}) => {
|
|
|
20 |
const { pathname } = useLocation();
|
|
|
21 |
const dispatch = useDispatch();
|
|
|
22 |
|
|
|
23 |
const {
|
|
|
24 |
register,
|
|
|
25 |
handleSubmit,
|
|
|
26 |
setValue,
|
|
|
27 |
formState: { errors }
|
|
|
28 |
} = useForm();
|
|
|
29 |
|
|
|
30 |
const onSubmit = handleSubmit(({ description }) => {
|
|
|
31 |
const typesUrl = {
|
|
|
32 |
profile: `/profile/my-profiles/extended/${id}`,
|
|
|
33 |
group: `/group/my-groups/extended/${id}`
|
|
|
34 |
};
|
|
|
35 |
const type = pathname.split('/')[1];
|
|
|
36 |
|
|
|
37 |
const formData = new FormData();
|
|
|
38 |
formData.append('description', description);
|
|
|
39 |
|
|
|
40 |
axios
|
|
|
41 |
.post(typesUrl[type], formData)
|
|
|
42 |
.then((response) => {
|
|
|
43 |
const { data, success } = response.data;
|
|
|
44 |
if (!success) {
|
|
|
45 |
const errorMessage =
|
|
|
46 |
typeof data === 'string'
|
|
|
47 |
? data
|
|
|
48 |
: Object.entries(data).map(([key, value]) => `${key}: ${value}`)[0];
|
|
|
49 |
throw new Error(errorMessage);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
onComplete(data.description || data);
|
|
|
53 |
closeModal();
|
|
|
54 |
})
|
|
|
55 |
.catch((err) => {
|
|
|
56 |
dispatch(addNotification({ style: 'danger', msg: err.message }));
|
|
|
57 |
});
|
|
|
58 |
});
|
|
|
59 |
|
|
|
60 |
useEffect(() => {
|
|
|
61 |
register('description', { required: 'Este campo es requerido' });
|
|
|
62 |
}, []);
|
|
|
63 |
|
|
|
64 |
return (
|
|
|
65 |
<Modal title='Visión general' show={isOpen} onClose={closeModal} onAccept={onSubmit}>
|
|
|
66 |
<CKEditor onChange={(value) => setValue('description', value)} defaultValue={overview} />
|
|
|
67 |
{errors.description && <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>}
|
|
|
68 |
</Modal>
|
|
|
69 |
);
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
export default OverviewModal;
|