Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Autoría | Ultima modificación | Ver Log |

/* eslint-disable react/prop-types */
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 FormErrorFeedback from '../../../shared/form-error-feedback/FormErrorFeedback'
import Spinner from '../../../shared/loading-spinner/Spinner'

const OverviewModal = ({
  isOpen = false,
  overview = '',
  userIdEncrypted = '',
  closeModal = function () {},
  setOverview = function () {},
}) => {
  const [loading, setLoading] = useState(false)
  const { register, errors, handleSubmit, setValue, getValues } = useForm()
  const dispatch = useDispatch()

  useEffect(() => {
    const description = getValues('description')
    if (!description) {
      register('description', { required: 'Este campo es requerido' })
    }
  }, [isOpen])

  const onSubmitHandler = async (data) => {
    setLoading(true)
    const formData = new FormData()
    Object.entries(data).map(([key, value]) => formData.append(key, value))

    axios
      .post(`/profile/my-profiles/extended/${userIdEncrypted}`, 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
        }

        setOverview(data.data.description)
        closeModal()
      })
    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