Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
1975 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
 
32
    axios
1975 stevensc 33
      .post(typesUrl[type], { description })
704 stevensc 34
      .then(({ data: responseData }) => {
35
        const { data, success } = responseData
36
        if (!success) {
37
          const errorMessage =
38
            typeof data === 'string'
39
              ? data
40
              : Object.entries(data).map(
41
                  ([key, value]) => `${key}: ${value}`
42
                )[0]
43
          throw new Error(errorMessage)
5 stevensc 44
        }
45
 
717 stevensc 46
        onComplete(data.description || data)
5 stevensc 47
        closeModal()
48
      })
704 stevensc 49
      .catch((err) => {
50
        dispatch(addNotification({ style: 'danger', msg: err.message }))
51
      })
1456 stevensc 52
  })
5 stevensc 53
 
54
  const removeModalTabIndex = () => {
55
    const modal = document.querySelector('.fade.modal.show')
56
    modal.removeAttribute('tabindex')
57
  }
58
 
1456 stevensc 59
  useEffect(() => {
60
    register('description', { required: 'Este campo es requerido' })
61
  }, [])
62
 
5 stevensc 63
  return (
1456 stevensc 64
    <Modal
65
      title='Visión general'
66
      show={isOpen}
67
      onClose={closeModal}
68
      onAccept={onSubmit}
69
    >
70
      <CKEditor
71
        onChange={(e) => setValue('description', e.editor.getData())}
72
        onInstanceReady={(e) => e.editor.setData(overview)}
73
        config={CKEDITOR_OPTIONS}
74
        onDialogShow={removeModalTabIndex}
75
      />
76
      {errors.description && (
77
        <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
78
      )}
5 stevensc 79
    </Modal>
80
  )
81
}
82
 
83
export default OverviewModal