3719 |
stevensc |
1 |
import React, { useEffect } from 'react';
|
|
|
2 |
import { useDispatch, useSelector } from 'react-redux';
|
|
|
3 |
import { useForm } from 'react-hook-form';
|
|
|
4 |
|
|
|
5 |
import { axios } from '@utils/index';
|
|
|
6 |
import { addNotification } from '../../redux/notification/notification.actions';
|
|
|
7 |
|
|
|
8 |
import CKEditor from '@components/common/ckeditor/Ckeditor';
|
|
|
9 |
import Modal from '@components/UI/modal/Modal';
|
|
|
10 |
import FormErrorFeedback from '@components/UI/form/FormErrorFeedback';
|
|
|
11 |
|
|
|
12 |
const AnswerModal = ({
|
|
|
13 |
show = false,
|
|
|
14 |
currentAnswer = '',
|
|
|
15 |
url = '',
|
|
|
16 |
onClose = () => null,
|
|
|
17 |
onComplete = () => null
|
|
|
18 |
}) => {
|
|
|
19 |
const labels = useSelector(({ intl }) => intl.labels);
|
|
|
20 |
const dispatch = useDispatch();
|
|
|
21 |
|
|
|
22 |
const {
|
|
|
23 |
register,
|
|
|
24 |
handleSubmit,
|
|
|
25 |
setValue,
|
|
|
26 |
formState: { errors }
|
|
|
27 |
} = useForm();
|
|
|
28 |
|
|
|
29 |
const onSubmit = handleSubmit(({ description }) => {
|
|
|
30 |
const formData = new FormData();
|
|
|
31 |
formData.append('description', description);
|
|
|
32 |
|
|
|
33 |
axios.post(url, formData).then((response) => {
|
|
|
34 |
const { data, success } = response.data;
|
|
|
35 |
|
|
|
36 |
if (!success) {
|
|
|
37 |
const errorMessage =
|
|
|
38 |
typeof data === 'string'
|
|
|
39 |
? data
|
|
|
40 |
: 'Error interno. Por favor, inténtelo de nuevo más tarde.';
|
|
|
41 |
|
|
|
42 |
dispatch(addNotification({ style: 'danger', msg: errorMessage }));
|
|
|
43 |
return;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
onComplete(data);
|
|
|
47 |
onClose();
|
|
|
48 |
});
|
|
|
49 |
});
|
|
|
50 |
|
|
|
51 |
useEffect(() => {
|
|
|
52 |
register('description', { required: true });
|
|
|
53 |
}, []);
|
|
|
54 |
|
|
|
55 |
useEffect(() => {
|
|
|
56 |
setValue('description', currentAnswer);
|
|
|
57 |
}, [currentAnswer]);
|
|
|
58 |
|
|
|
59 |
return (
|
|
|
60 |
<Modal
|
|
|
61 |
title={currentAnswer ? labels.my_coach_answer_edit : labels.my_coach_answer_add}
|
|
|
62 |
show={show}
|
|
|
63 |
onClose={onClose}
|
|
|
64 |
onAccept={onSubmit}
|
|
|
65 |
>
|
|
|
66 |
<CKEditor defaultValue={currentAnswer} onChange={(value) => setValue('description', value)} />
|
|
|
67 |
{errors.description && <FormErrorFeedback>{labels.error_field_empty}</FormErrorFeedback>}
|
|
|
68 |
</Modal>
|
|
|
69 |
);
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
export default AnswerModal;
|