Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
2521 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'
1456 stevensc 5
 
2521 stevensc 6
import { axios } from '../../utils'
5 stevensc 7
import { addNotification } from '../../redux/notification/notification.actions'
8
 
2521 stevensc 9
import CKEditor from '@app/components/UI/Ckeditor'
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
 
1456 stevensc 57
  useEffect(() => {
58
    register('description', { required: 'Este campo es requerido' })
59
  }, [])
60
 
5 stevensc 61
  return (
1456 stevensc 62
    <Modal
63
      title='Visión general'
64
      show={isOpen}
65
      onClose={closeModal}
66
      onAccept={onSubmit}
67
    >
68
      <CKEditor
2521 stevensc 69
        onChange={(value) => setValue('description', value)}
70
        defaultValue={overview}
1456 stevensc 71
      />
72
      {errors.description && (
73
        <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
74
      )}
5 stevensc 75
    </Modal>
76
  )
77
}
78
 
79
export default OverviewModal