Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5473 stevensc 1
/* eslint-disable react/prop-types */
2
import React, { useEffect, useState } from 'react'
3
import { Button, Modal } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
5
import { CKEditor } from 'ckeditor4-react'
6
import { useDispatch } from 'react-redux'
7
import { addNotification } from '../../../redux/notification/notification.actions'
8
import { axios, CKEDITOR_OPTIONS } from '../../../utils'
9
 
10
import FormErrorFeedback from '../../../shared/form-error-feedback/FormErrorFeedback'
11
import Spinner from '../../../shared/loading-spinner/Spinner'
12
 
13
const OverviewModal = ({
14
  isOpen = false,
15
  overview = '',
16
  userIdEncrypted = '',
17
  closeModal = function () {},
18
  setOverview = function () {},
19
}) => {
20
  const [loading, setLoading] = useState(false)
21
  const { register, errors, handleSubmit, setValue, getValues } = useForm()
22
  const dispatch = useDispatch()
23
 
24
  useEffect(() => {
25
    const description = getValues('description')
26
    if (!description) {
27
      register('description', { required: 'Este campo es requerido' })
28
    }
29
  }, [isOpen])
30
 
31
  const onSubmitHandler = async (data) => {
32
    setLoading(true)
33
    const formData = new FormData()
34
    Object.entries(data).map(([key, value]) => formData.append(key, value))
35
 
36
    axios
37
      .post(`/profile/my-profiles/extended/${userIdEncrypted}`, formData)
38
      .then(({ data }) => {
39
        if (!data.success) {
40
          typeof data.data === 'string'
41
            ? dispatch(addNotification({ msg: data.data, style: 'danger' }))
42
            : Object.entries(data.data).map(([key, value]) =>
43
                value.map((err) =>
44
                  dispatch(
45
                    addNotification({ style: 'danger', msg: `${key}: ${err}` })
46
                  )
47
                )
48
              )
49
          return
50
        }
51
 
52
        setOverview(data.data.description)
53
        closeModal()
54
      })
55
    setLoading(false)
56
  }
57
 
58
  const removeModalTabIndex = () => {
59
    const modal = document.querySelector('.fade.modal.show')
60
    modal.removeAttribute('tabindex')
61
  }
62
 
63
  return (
64
    <Modal show={isOpen} onHide={closeModal}>
65
      <Modal.Header closeButton>
66
        <Modal.Title>Visión general</Modal.Title>
67
      </Modal.Header>
68
      <form onSubmit={handleSubmit(onSubmitHandler)} autoComplete={false}>
69
        <Modal.Body>
70
          {loading && <Spinner />}
71
          <CKEditor
72
            onChange={(e) => setValue('description', e.editor.getData())}
73
            onInstanceReady={(e) => e.editor.setData(overview)}
74
            config={CKEDITOR_OPTIONS}
75
            onDialogShow={removeModalTabIndex}
76
          />
77
          {errors.description && (
78
            <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
79
          )}
80
        </Modal.Body>
81
        <Modal.Footer>
82
          <Button size="sm" type="submit">
83
            Enviar
84
          </Button>
85
          <Button size="sm" variant="danger" onClick={closeModal}>
86
            Cancelar
87
          </Button>
88
        </Modal.Footer>
89
      </form>
90
    </Modal>
91
  )
92
}
93
 
94
export default OverviewModal