Rev 717 | Rev 1456 | 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, Modal } from 'react-bootstrap'import { useForm } from 'react-hook-form'import { CKEditor } from 'ckeditor4-react'import { useDispatch } from 'react-redux'import { addNotification } from '../../redux/notification/notification.actions'import { axios, CKEDITOR_OPTIONS } from '../../utils'import Spinner from '../UI/Spinner'import FormErrorFeedback from '../UI/form/FormErrorFeedback'import { useLocation } from 'react-router-dom'const OverviewModal = ({isOpen = false,overview = '',id = '',closeModal = function () {},onComplete = function () {}}) => {const [loading, setLoading] = useState(false)const { pathname } = useLocation()const dispatch = useDispatch()const { register, errors, handleSubmit, setValue } = useForm()useEffect(() => {register('description', { required: 'Este campo es requerido' })}, [])const onSubmitHandler = ({ description }) => {const typesUrl = {profile: `/profile/my-profiles/extended/${id}`,group: `/group/my-groups/extended/${id}`}const type = pathname.split('/')[1]setLoading(true)const formData = new FormData()formData.append('description', description)axios.post(typesUrl[type], formData).then(({ data: responseData }) => {const { data, success } = responseDataif (!success) {const errorMessage =typeof data === 'string'? data: Object.entries(data).map(([key, value]) => `${key}: ${value}`)[0]throw new Error(errorMessage)}onComplete(data.description || data)closeModal()}).catch((err) => {dispatch(addNotification({ style: 'danger', msg: err.message }))}).finally(() => setLoading(false))}const removeModalTabIndex = () => {const modal = document.querySelector('.fade.modal.show')modal.removeAttribute('tabindex')}return (<Modal show={isOpen} onHide={closeModal}><Modal.Header closeButton><Modal.Title>Visión general</Modal.Title></Modal.Header><form onSubmit={handleSubmit(onSubmitHandler)} autoComplete={false}><Modal.Body>{loading && <Spinner />}<CKEditoronChange={(e) => setValue('description', e.editor.getData())}onInstanceReady={(e) => e.editor.setData(overview)}config={CKEDITOR_OPTIONS}onDialogShow={removeModalTabIndex}/>{errors.description && (<FormErrorFeedback>{errors.description.message}</FormErrorFeedback>)}</Modal.Body><Modal.Footer><Button size='sm' type='submit'>Enviar</Button><Button size='sm' variant='danger' onClick={closeModal}>Cancelar</Button></Modal.Footer></form></Modal>)}export default OverviewModal