Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2093 | Rev 2861 | 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
 
2092 stevensc 5
import { axios } from '@app/utils'
6
import { addNotification } from '@app/redux/notification/notification.actions'
1229 stevensc 7
 
2092 stevensc 8
import Modal from '../UI/modal/Modal'
9
import Input from '../UI/inputs/Input'
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)
2092 stevensc 18
 
2802 stevensc 19
  const {
20
    register,
21
    handleSubmit,
22
    formState: { errors }
23
  } = useForm()
5 stevensc 24
 
1229 stevensc 25
  const onSubmitHandler = handleSubmit(async (data) => {
1979 stevensc 26
    const formData = new FormData()
27
    Object.entries(data).map(([key, value]) => formData.append(key, value))
28
 
5 stevensc 29
    axios
1979 stevensc 30
      .post('/profile/my-profiles/add', formData)
5 stevensc 31
      .then(({ data: response }) => {
32
        const { data, success } = response
33
 
34
        if (!success) {
35
          const errorMessage =
36
            typeof data === 'string'
37
              ? data
38
              : 'Ha ocurrido un error, por favor intente más tarde'
39
          addNotification({ style: 'success', msg: errorMessage })
40
          return
41
        }
42
 
43
        getProfiles()
44
        addNotification({ style: 'success', msg: data })
45
        onHide()
46
      })
47
      .catch((err) => {
2092 stevensc 48
        addNotification({ style: 'danger', msg: err.message })
5 stevensc 49
      })
1229 stevensc 50
  })
5 stevensc 51
 
52
  return (
1229 stevensc 53
    <Modal
54
      title={labels.new_profile}
55
      show={show}
56
      onHide={onHide}
57
      onAccept={onSubmitHandler}
58
      onClose={onHide}
59
    >
2092 stevensc 60
      <Input
1229 stevensc 61
        name='name'
2093 stevensc 62
        placeholder={labels.create_profile}
2092 stevensc 63
        inputRef={register({ required: 'Este campo es requerido' })}
64
        error={errors.name?.message}
1229 stevensc 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)