| 7221 |
stevensc |
1 |
import React, { useEffect, useState } from 'react'
|
|
|
2 |
import { useForm } from 'react-hook-form'
|
|
|
3 |
import { CKEditor } from 'ckeditor4-react'
|
|
|
4 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
5 |
import { Button, Form, Modal } from 'react-bootstrap'
|
|
|
6 |
import { CKEDITOR_OPTIONS, axios } from '../../utils'
|
|
|
7 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
8 |
|
|
|
9 |
import Spinner from '../UI/Spinner'
|
|
|
10 |
import FormErrorFeedback from '../UI/FormErrorFeedback'
|
|
|
11 |
|
|
|
12 |
const AnswerModal = ({
|
|
|
13 |
show = false,
|
|
|
14 |
currentAnswer = '',
|
|
|
15 |
url = '',
|
|
|
16 |
onClose = () => null,
|
|
|
17 |
onComplete = () => null,
|
|
|
18 |
}) => {
|
|
|
19 |
const [loading, setLoading] = useState(false)
|
|
|
20 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
21 |
const dispatch = useDispatch()
|
|
|
22 |
|
|
|
23 |
const { register, handleSubmit, setValue, errors } = useForm()
|
|
|
24 |
|
|
|
25 |
const onSubmit = handleSubmit(({ description }) => {
|
|
|
26 |
setLoading(true)
|
|
|
27 |
const formData = new FormData()
|
|
|
28 |
formData.append('description', description)
|
|
|
29 |
|
|
|
30 |
axios
|
|
|
31 |
.post(url, formData)
|
|
|
32 |
.then((response) => {
|
|
|
33 |
const { data, success } = response.data
|
|
|
34 |
|
|
|
35 |
if (!success) {
|
|
|
36 |
const errorMessage =
|
|
|
37 |
typeof data === 'string'
|
|
|
38 |
? data
|
|
|
39 |
: 'Error interno. Por favor, inténtelo de nuevo más tarde.'
|
|
|
40 |
|
|
|
41 |
dispatch(addNotification({ style: 'danger', msg: errorMessage }))
|
|
|
42 |
return
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
onComplete(data)
|
|
|
46 |
onClose()
|
|
|
47 |
})
|
|
|
48 |
.finally(() => setLoading(false))
|
|
|
49 |
})
|
|
|
50 |
|
|
|
51 |
useEffect(() => {
|
|
|
52 |
register('description', { required: true })
|
|
|
53 |
}, [])
|
|
|
54 |
|
|
|
55 |
useEffect(() => {
|
|
|
56 |
setValue('description', currentAnswer)
|
|
|
57 |
}, [currentAnswer])
|
|
|
58 |
|
|
|
59 |
return (
|
|
|
60 |
<Modal show={show}>
|
|
|
61 |
<Modal.Header className="pb-0">
|
|
|
62 |
<Modal.Title>
|
|
|
63 |
{currentAnswer
|
|
|
64 |
? labels.my_coach_answer_edit
|
|
|
65 |
: labels.my_coach_answer_add}
|
|
|
66 |
</Modal.Title>
|
|
|
67 |
</Modal.Header>
|
|
|
68 |
<Modal.Body>
|
|
|
69 |
{loading ? (
|
|
|
70 |
<Spinner />
|
|
|
71 |
) : (
|
|
|
72 |
<Form onSubmit={onSubmit}>
|
|
|
73 |
<CKEditor
|
|
|
74 |
onChange={(e) => setValue('description', e.editor.getData())}
|
|
|
75 |
onInstanceReady={(e) => e.editor.setData(currentAnswer)}
|
|
|
76 |
config={CKEDITOR_OPTIONS}
|
|
|
77 |
/>
|
|
|
78 |
{errors.description && (
|
|
|
79 |
<FormErrorFeedback>{labels.error_field_empty}</FormErrorFeedback>
|
|
|
80 |
)}
|
|
|
81 |
|
|
|
82 |
<Button className="mt-3 mr-2" variant="primary" type="submit">
|
|
|
83 |
{labels.accept}
|
|
|
84 |
</Button>
|
|
|
85 |
<Button className="btn-secondary mt-3" onClick={onClose}>
|
|
|
86 |
{labels.cancel}
|
|
|
87 |
</Button>
|
|
|
88 |
</Form>
|
|
|
89 |
)}
|
|
|
90 |
</Modal.Body>
|
|
|
91 |
</Modal>
|
|
|
92 |
)
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
export default AnswerModal
|