Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2521 | Rev 3269 | 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
 
2802 stevensc 23
  const {
24
    register,
25
    handleSubmit,
26
    setValue,
27
    formState: { errors }
28
  } = useForm()
5 stevensc 29
 
1456 stevensc 30
  const onSubmit = handleSubmit(({ description }) => {
5 stevensc 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
 
1979 stevensc 37
    const formData = new FormData()
38
    formData.append('description', description)
39
 
5 stevensc 40
    axios
1979 stevensc 41
      .post(typesUrl[type], formData)
704 stevensc 42
      .then(({ data: responseData }) => {
43
        const { data, success } = responseData
44
        if (!success) {
45
          const errorMessage =
46
            typeof data === 'string'
47
              ? data
48
              : Object.entries(data).map(
49
                  ([key, value]) => `${key}: ${value}`
50
                )[0]
51
          throw new Error(errorMessage)
5 stevensc 52
        }
53
 
717 stevensc 54
        onComplete(data.description || data)
5 stevensc 55
        closeModal()
56
      })
704 stevensc 57
      .catch((err) => {
58
        dispatch(addNotification({ style: 'danger', msg: err.message }))
59
      })
1456 stevensc 60
  })
5 stevensc 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
2521 stevensc 74
        onChange={(value) => setValue('description', value)}
75
        defaultValue={overview}
1456 stevensc 76
      />
77
      {errors.description && (
78
        <FormErrorFeedback>{errors.description.message}</FormErrorFeedback>
79
      )}
5 stevensc 80
    </Modal>
81
  )
82
}
83
 
84
export default OverviewModal