Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6770 | Ir a la última revisión | | Comparar con el anterior | 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 dispatch = useDispatch()
21
 
6775 stevensc 22
  const { register, errors, handleSubmit, setValue } = useForm()
23
 
6770 stevensc 24
  useEffect(() => {
25
    register('description', { required: 'Este campo es requerido' })
26
  }, [])
27
 
28
  const onSubmitHandler = async ({ description }) => {
29
    setLoading(true)
30
 
31
    const formData = new FormData()
32
    formData.append('description', description)
33
 
34
    axios
35
      .post(`/profile/my-profiles/extended/${id}`, formData)
36
      .then(({ data }) => {
37
        if (!data.success) {
38
          typeof data.data === 'string'
39
            ? dispatch(addNotification({ msg: data.data, style: 'danger' }))
40
            : Object.entries(data.data).map(([key, value]) =>
41
                value.map((err) =>
42
                  dispatch(
43
                    addNotification({ style: 'danger', msg: `${key}: ${err}` })
44
                  )
45
                )
46
              )
47
          return
48
        }
49
 
50
        onComplete(data.data.description)
51
        closeModal()
52
      })
53
      .finally(() => setLoading(false))
54
  }
55
 
56
  const removeModalTabIndex = () => {
57
    const modal = document.querySelector('.fade.modal.show')
58
    modal.removeAttribute('tabindex')
59
  }
60
 
61
  return (
62
    <Modal show={isOpen} onHide={closeModal}>
63
      <Modal.Header closeButton>
64
        <Modal.Title>Visión general</Modal.Title>
65
      </Modal.Header>
66
      <form onSubmit={handleSubmit(onSubmitHandler)} autoComplete={false}>
67
        <Modal.Body>
68
          {loading && <Spinner />}
69
          <CKEditor
70
            onChange={(e) => setValue('description', e.editor.getData())}
71
            onInstanceReady={(e) => e.editor.setData(overview)}
72
            config={CKEDITOR_OPTIONS}
73
            onDialogShow={removeModalTabIndex}
74
          />
75
          {errors.description && (
76
            <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
77
          )}
78
        </Modal.Body>
79
        <Modal.Footer>
80
          <Button size="sm" type="submit">
81
            Enviar
82
          </Button>
83
          <Button size="sm" variant="danger" onClick={closeModal}>
84
            Cancelar
85
          </Button>
86
        </Modal.Footer>
87
      </form>
88
    </Modal>
89
  )
90
}
91
 
92
export default OverviewModal