Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6775 | 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/FormErrorFeedback'
import { useLocation } from 'react-router-dom'

const OverviewModal = ({
  isOpen = false,
  overview = '',
  id = '',
  closeModal = function () {},
  onComplete = function () {},
}) => {
  const [loading, setLoading] = useState(false)
  const dispatch = useDispatch()
  const { pathname } = useLocation()

  const { register, errors, handleSubmit, setValue } = useForm()

  useEffect(() => {
    register('description', { required: 'Este campo es requerido' })
  }, [])

  const onSubmitHandler = async ({ 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 }) => {
        if (!data.success) {
          typeof data.data === 'string'
            ? dispatch(addNotification({ msg: data.data, style: 'danger' }))
            : Object.entries(data.data).map(([key, value]) =>
                value.map((err) =>
                  dispatch(
                    addNotification({ style: 'danger', msg: `${key}: ${err}` })
                  )
                )
              )
          return
        }

        onComplete(data.data.description)
        closeModal()
      })
      .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 />}
          <CKEditor
            onChange={(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