Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6775 | Ir a la última revisión | | Ultima modificación | Ver Log |

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