Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2092 | Rev 2802 | 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
 
5 stevensc 19
  const { register, handleSubmit, errors } = useForm()
20
 
1229 stevensc 21
  const onSubmitHandler = handleSubmit(async (data) => {
1979 stevensc 22
    const formData = new FormData()
23
    Object.entries(data).map(([key, value]) => formData.append(key, value))
24
 
5 stevensc 25
    axios
1979 stevensc 26
      .post('/profile/my-profiles/add', formData)
5 stevensc 27
      .then(({ data: response }) => {
28
        const { data, success } = response
29
 
30
        if (!success) {
31
          const errorMessage =
32
            typeof data === 'string'
33
              ? data
34
              : 'Ha ocurrido un error, por favor intente más tarde'
35
          addNotification({ style: 'success', msg: errorMessage })
36
          return
37
        }
38
 
39
        getProfiles()
40
        addNotification({ style: 'success', msg: data })
41
        onHide()
42
      })
43
      .catch((err) => {
2092 stevensc 44
        addNotification({ style: 'danger', msg: err.message })
5 stevensc 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
    >
2092 stevensc 56
      <Input
1229 stevensc 57
        name='name'
2093 stevensc 58
        placeholder={labels.create_profile}
2092 stevensc 59
        inputRef={register({ required: 'Este campo es requerido' })}
60
        error={errors.name?.message}
1229 stevensc 61
      />
5 stevensc 62
    </Modal>
63
  )
64
}
65
 
66
const mapDispatchToProps = {
1229 stevensc 67
  addNotification: (notification) => addNotification(notification)
5 stevensc 68
}
69
 
70
export default connect(null, mapDispatchToProps)(AddProfileModal)