Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1971 | Rev 2092 | 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 from 'react'
2
import { useForm } from 'react-hook-form'
3
import { connect, useSelector } from 'react-redux'
4
 
1229 stevensc 5
import { axios } from '../../utils'
6
import { addNotification } from '../../redux/notification/notification.actions'
7
 
1437 stevensc 8
import FormErrorFeedback from '../UI/form/FormErrorFeedback'
1384 stevensc 9
import Modal from 'components/UI/modal/Modal'
5 stevensc 10
 
11
const AddProfileModal = ({
12
  show = '',
13
  onHide = () => null,
14
  getProfiles = () => null,
1229 stevensc 15
  addNotification // redux action
5 stevensc 16
}) => {
17
  const labels = useSelector(({ intl }) => intl.labels)
18
  const { register, handleSubmit, errors } = useForm()
19
 
1229 stevensc 20
  const onSubmitHandler = handleSubmit(async (data) => {
1979 stevensc 21
    const formData = new FormData()
22
    Object.entries(data).map(([key, value]) => formData.append(key, value))
23
 
5 stevensc 24
    axios
1979 stevensc 25
      .post('/profile/my-profiles/add', formData)
5 stevensc 26
      .then(({ data: response }) => {
27
        const { data, success } = response
28
 
29
        if (!success) {
30
          const errorMessage =
31
            typeof data === 'string'
32
              ? data
33
              : 'Ha ocurrido un error, por favor intente más tarde'
34
          addNotification({ style: 'success', msg: errorMessage })
35
          return
36
        }
37
 
38
        getProfiles()
39
        addNotification({ style: 'success', msg: data })
40
        onHide()
41
      })
42
      .catch((err) => {
43
        console.log(err)
44
        throw new Error(err)
45
      })
1229 stevensc 46
  })
5 stevensc 47
 
48
  return (
1229 stevensc 49
    <Modal
50
      title={labels.new_profile}
51
      show={show}
52
      onHide={onHide}
53
      onAccept={onSubmitHandler}
54
      onClose={onHide}
55
      onReject={onHide}
56
    >
57
      <input
58
        type='text'
59
        name='name'
60
        placeholder={labels.profile_name}
61
        ref={register({ required: 'Este campo es requerido' })}
62
      />
63
      {errors.name && (
64
        <FormErrorFeedback>{errors.name.message}</FormErrorFeedback>
65
      )}
5 stevensc 66
    </Modal>
67
  )
68
}
69
 
70
const mapDispatchToProps = {
1229 stevensc 71
  addNotification: (notification) => addNotification(notification)
5 stevensc 72
}
73
 
74
export default connect(null, mapDispatchToProps)(AddProfileModal)