Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
1979 stevensc 1
import React, { useEffect} from 'react'
1456 stevensc 2
import { useLocation } from 'react-router-dom'
3
import { useDispatch } from 'react-redux'
5 stevensc 4
import { useForm } from 'react-hook-form'
5
import { CKEditor } from 'ckeditor4-react'
1456 stevensc 6
 
7
import { axios, CKEDITOR_OPTIONS } from '../../utils'
5 stevensc 8
import { addNotification } from '../../redux/notification/notification.actions'
9
 
1456 stevensc 10
import FormErrorFeedback from 'components/UI/form/FormErrorFeedback'
11
import Modal from 'components/UI/modal/Modal'
5 stevensc 12
 
13
const OverviewModal = ({
14
  isOpen = false,
15
  overview = '',
16
  id = '',
17
  closeModal = function () {},
703 stevensc 18
  onComplete = function () {}
5 stevensc 19
}) => {
703 stevensc 20
  const { pathname } = useLocation()
5 stevensc 21
  const dispatch = useDispatch()
22
 
23
  const { register, errors, handleSubmit, setValue } = useForm()
24
 
1456 stevensc 25
  const onSubmit = handleSubmit(({ description }) => {
5 stevensc 26
    const typesUrl = {
27
      profile: `/profile/my-profiles/extended/${id}`,
703 stevensc 28
      group: `/group/my-groups/extended/${id}`
5 stevensc 29
    }
30
    const type = pathname.split('/')[1]
31
 
1979 stevensc 32
    const formData = new FormData()
33
    formData.append('description', description)
34
 
5 stevensc 35
    axios
1979 stevensc 36
      .post(typesUrl[type], formData)
704 stevensc 37
      .then(({ data: responseData }) => {
38
        const { data, success } = responseData
39
        if (!success) {
40
          const errorMessage =
41
            typeof data === 'string'
42
              ? data
43
              : Object.entries(data).map(
44
                  ([key, value]) => `${key}: ${value}`
45
                )[0]
46
          throw new Error(errorMessage)
5 stevensc 47
        }
48
 
717 stevensc 49
        onComplete(data.description || data)
5 stevensc 50
        closeModal()
51
      })
704 stevensc 52
      .catch((err) => {
53
        dispatch(addNotification({ style: 'danger', msg: err.message }))
54
      })
1456 stevensc 55
  })
5 stevensc 56
 
57
  const removeModalTabIndex = () => {
58
    const modal = document.querySelector('.fade.modal.show')
59
    modal.removeAttribute('tabindex')
60
  }
61
 
1456 stevensc 62
  useEffect(() => {
63
    register('description', { required: 'Este campo es requerido' })
64
  }, [])
65
 
5 stevensc 66
  return (
1456 stevensc 67
    <Modal
68
      title='Visión general'
69
      show={isOpen}
70
      onClose={closeModal}
71
      onAccept={onSubmit}
72
    >
73
      <CKEditor
74
        onChange={(e) => setValue('description', e.editor.getData())}
75
        onInstanceReady={(e) => e.editor.setData(overview)}
76
        config={CKEDITOR_OPTIONS}
77
        onDialogShow={removeModalTabIndex}
78
      />
79
      {errors.description && (
80
        <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
81
      )}
5 stevensc 82
    </Modal>
83
  )
84
}
85
 
86
export default OverviewModal