Rev 7191 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useEffect, useState } from 'react'
import { Button, Form, Modal } from 'react-bootstrap'
import { useDispatch, useSelector } from 'react-redux'
import Spinner from '../UI/Spinner'
import { CKEditor } from 'ckeditor4-react'
import { CKEDITOR_OPTIONS, axios } from '../../utils'
import FormErrorFeedback from '../UI/FormErrorFeedback'
import { useForm } from 'react-hook-form'
import { addNotification } from '../../redux/notification/notification.actions'
const QuestionModal = ({ show, url, isEdit, onClose, onComplete }) => {
const [loading, setLoading] = useState(false)
const labels = useSelector(({ intl }) => intl.labels)
const dispatch = useDispatch()
const { register, handleSubmit, getValues, setValue, errors } = useForm()
const onSubmit = handleSubmit((data) => {
setLoading(true)
const formData = new FormData()
Object.entries(data).map(([key, value]) => {
if (['attachment', 'image'].includes(key) && value) {
formData.append(key, value[0])
return
}
formData.append(key, value)
})
axios
.post(url, formData)
.then((response) => {
const { data, success } = response.data
if (!success) {
if (typeof data === 'string') {
dispatch(addNotification({ style: 'danger', msg: data }))
return
}
Object.entries(data).map(([key, value]) => {
key in getValues() &&
dispatch(
addNotification({
style: 'danger',
msg: `${key}: ${Array.isArray(value) ? value[0] : value}`,
})
)
})
return
}
onComplete()
onClose()
})
.finally(() => setLoading(false))
})
useEffect(() => {
register('description', { required: true })
}, [])
useEffect(() => {
if (!url || !show || !isEdit) return
axios.get(url).then((response) => {
const { data, success } = response.data
if (!success) {
const errorMessage =
typeof data === 'string' ? data : labels.error_there_was_an_error
dispatch(
addNotification({
style: 'danger',
msg: errorMessage,
})
)
return
}
console.log(data)
setValue('title', data.title)
setValue('category_id', data.category_id)
setValue('description', data.description)
})
}, [url, show, isEdit])
useEffect(() => {
if (!show) {
setValue('category_id', '')
setValue('description', '')
setValue('title', '')
setValue('image', '')
}
}, [show])
return (
<Modal show={show}>
<Modal.Header className="pb-0">
<Modal.Title>
{isEdit ? labels.edit : labels.add} {labels.question}
</Modal.Title>
</Modal.Header>
<Modal.Body>
{loading ? (
<Spinner />
) : (
<Form onSubmit={onSubmit}>
<Form.Group>
<Form.Label>{labels.title}</Form.Label>
<Form.Control
type="text"
name="title"
ref={register({ required: true })}
/>
{errors.title && (
<FormErrorFeedback>
{labels.error_field_empty}
</FormErrorFeedback>
)}
</Form.Group>
<CKEditor
onChange={(e) => setValue('description', e.editor.getData())}
onInstanceReady={(e) =>
e.editor.setData(getValues('description'))
}
config={CKEDITOR_OPTIONS}
/>
{errors.description && (
<FormErrorFeedback>{labels.error_field_empty}</FormErrorFeedback>
)}
<Button className="mt-3 mr-2" variant="primary" type="submit">
{labels.accept}
</Button>
<Button className="btn-secondary mt-3" onClick={onClose}>
{labels.cancel}
</Button>
</Form>
)}
</Modal.Body>
</Modal>
)
}
export default QuestionModal