Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 5 | Rev 704 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5 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
import { useLocation } from 'react-router-dom'
12
 
13
const OverviewModal = ({
14
  isOpen = false,
15
  overview = '',
16
  id = '',
17
  closeModal = function () {},
703 stevensc 18
  onComplete = function () {}
5 stevensc 19
}) => {
20
  const [loading, setLoading] = useState(false)
703 stevensc 21
  const { pathname } = useLocation()
5 stevensc 22
  const dispatch = useDispatch()
23
 
24
  const { register, errors, handleSubmit, setValue } = useForm()
25
 
26
  useEffect(() => {
27
    register('description', { required: 'Este campo es requerido' })
28
  }, [])
29
 
30
  const onSubmitHandler = async ({ description }) => {
31
    const typesUrl = {
32
      profile: `/profile/my-profiles/extended/${id}`,
703 stevensc 33
      group: `/group/my-groups/extended/${id}`
5 stevensc 34
    }
35
    const type = pathname.split('/')[1]
36
 
37
    setLoading(true)
38
 
39
    const formData = new FormData()
40
    formData.append('description', description)
41
 
42
    axios
43
      .post(typesUrl[type], formData)
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>
703 stevensc 88
          <Button size='sm' type='submit'>
5 stevensc 89
            Enviar
90
          </Button>
703 stevensc 91
          <Button size='sm' variant='danger' onClick={closeModal}>
5 stevensc 92
            Cancelar
93
          </Button>
94
        </Modal.Footer>
95
      </form>
96
    </Modal>
97
  )
98
}
99
 
100
export default OverviewModal