Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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