| 5 |
stevensc |
1 |
import React, { useEffect, useState } from 'react'
|
| 1460 |
stevensc |
2 |
import { useDispatch, useSelector } from 'react-redux'
|
| 5 |
stevensc |
3 |
import { useForm } from 'react-hook-form'
|
|
|
4 |
import { CKEditor } from 'ckeditor4-react'
|
| 1460 |
stevensc |
5 |
|
|
|
6 |
import { CKEDITOR_OPTIONS, axios } from 'utils/index'
|
| 5 |
stevensc |
7 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
8 |
|
| 1460 |
stevensc |
9 |
import Modal from 'components/UI/modal/Modal'
|
|
|
10 |
import Spinner from 'components/UI/Spinner'
|
|
|
11 |
import FormErrorFeedback from 'components/UI/form/FormErrorFeedback'
|
| 5 |
stevensc |
12 |
|
|
|
13 |
const AnswerModal = ({
|
|
|
14 |
show = false,
|
|
|
15 |
currentAnswer = '',
|
|
|
16 |
url = '',
|
|
|
17 |
onClose = () => null,
|
| 748 |
stevensc |
18 |
onComplete = () => null
|
| 5 |
stevensc |
19 |
}) => {
|
|
|
20 |
const [loading, setLoading] = useState(false)
|
|
|
21 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
22 |
const dispatch = useDispatch()
|
|
|
23 |
|
|
|
24 |
const { register, handleSubmit, setValue, errors } = useForm()
|
|
|
25 |
|
|
|
26 |
const onSubmit = handleSubmit(({ description }) => {
|
|
|
27 |
setLoading(true)
|
|
|
28 |
const formData = new FormData()
|
|
|
29 |
formData.append('description', description)
|
|
|
30 |
|
|
|
31 |
axios
|
|
|
32 |
.post(url, formData)
|
|
|
33 |
.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 |
.finally(() => setLoading(false))
|
|
|
50 |
})
|
|
|
51 |
|
|
|
52 |
useEffect(() => {
|
|
|
53 |
register('description', { required: true })
|
|
|
54 |
}, [])
|
|
|
55 |
|
|
|
56 |
useEffect(() => {
|
|
|
57 |
setValue('description', currentAnswer)
|
|
|
58 |
}, [currentAnswer])
|
|
|
59 |
|
|
|
60 |
return (
|
| 1460 |
stevensc |
61 |
<Modal
|
|
|
62 |
title={
|
|
|
63 |
currentAnswer ? labels.my_coach_answer_edit : labels.my_coach_answer_add
|
|
|
64 |
}
|
|
|
65 |
show={show}
|
|
|
66 |
onClose={onClose}
|
|
|
67 |
onAccept={onSubmit}
|
|
|
68 |
>
|
|
|
69 |
<CKEditor
|
|
|
70 |
onChange={(e) => setValue('description', e.editor.getData())}
|
|
|
71 |
onInstanceReady={(e) => e.editor.setData(currentAnswer)}
|
|
|
72 |
config={CKEDITOR_OPTIONS}
|
|
|
73 |
/>
|
|
|
74 |
{errors.description && (
|
|
|
75 |
<FormErrorFeedback>{labels.error_field_empty}</FormErrorFeedback>
|
|
|
76 |
)}
|
| 5 |
stevensc |
77 |
|
| 1460 |
stevensc |
78 |
{loading && <Spinner />}
|
| 5 |
stevensc |
79 |
</Modal>
|
|
|
80 |
)
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
export default AnswerModal
|