Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1437 | Rev 1979 | 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) => {
5 stevensc 21
    axios
1971 stevensc 22
      .post('/profile/my-profiles/add', data)
5 stevensc 23
      .then(({ data: response }) => {
24
        const { data, success } = response
25
 
26
        if (!success) {
27
          const errorMessage =
28
            typeof data === 'string'
29
              ? data
30
              : 'Ha ocurrido un error, por favor intente más tarde'
31
          addNotification({ style: 'success', msg: errorMessage })
32
          return
33
        }
34
 
35
        getProfiles()
36
        addNotification({ style: 'success', msg: data })
37
        onHide()
38
      })
39
      .catch((err) => {
40
        console.log(err)
41
        throw new Error(err)
42
      })
1229 stevensc 43
  })
5 stevensc 44
 
45
  return (
1229 stevensc 46
    <Modal
47
      title={labels.new_profile}
48
      show={show}
49
      onHide={onHide}
50
      onAccept={onSubmitHandler}
51
      onClose={onHide}
52
      onReject={onHide}
53
    >
54
      <input
55
        type='text'
56
        name='name'
57
        placeholder={labels.profile_name}
58
        ref={register({ required: 'Este campo es requerido' })}
59
      />
60
      {errors.name && (
61
        <FormErrorFeedback>{errors.name.message}</FormErrorFeedback>
62
      )}
5 stevensc 63
    </Modal>
64
  )
65
}
66
 
67
const mapDispatchToProps = {
1229 stevensc 68
  addNotification: (notification) => addNotification(notification)
5 stevensc 69
}
70
 
71
export default connect(null, mapDispatchToProps)(AddProfileModal)