Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6727 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6727 stevensc 1
import React from 'react'
2
import { axios } from '../../utils'
3
import { useForm } from 'react-hook-form'
4
import { Button, Modal } from 'react-bootstrap'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
import { connect, useSelector } from 'react-redux'
6861 stevensc 7
 
6727 stevensc 8
import FormErrorFeedback from '../UI/FormErrorFeedback'
9
 
10
const AddProfileModal = ({
11
  show = '',
12
  onHide = () => null,
13
  getProfiles = () => null,
14
  addNotification, // redux action
15
}) => {
16
  const labels = useSelector(({ intl }) => intl.labels)
17
  const { register, handleSubmit, errors } = useForm()
18
 
19
  const onSubmitHandler = async (data) => {
20
    const formData = new FormData()
21
    Object.entries(data).map(([key, value]) => formData.append(key, value))
22
 
6861 stevensc 23
    axios
24
      .post('/profile/my-profiles/add', formData)
25
      .then(({ data: response }) => {
26
        const { data, success } = response
27
        if (success) {
28
          return
29
        }
30
 
31
        getProfiles()
32
        addNotification({ style: 'success', msg: data })
33
        onHide()
34
      })
35
      .catch((err) => {
36
        console.log(err)
37
        throw new Error(err)
38
      })
6727 stevensc 39
  }
40
 
41
  return (
42
    <Modal show={show} onHide={onHide}>
43
      <Modal.Header closeButton>
44
        <Modal.Title id="contained-modal-title-vcenter">
45
          {labels.new_profile}
46
        </Modal.Title>
47
      </Modal.Header>
48
      <form onSubmit={handleSubmit(onSubmitHandler)}>
49
        <Modal.Body>
50
          <input
51
            type="text"
52
            name="name"
53
            placeholder={labels.profile_name}
54
            ref={register({ required: 'Este campo es requerido' })}
55
          />
56
          {errors.name && (
57
            <FormErrorFeedback>{errors.name.message}</FormErrorFeedback>
58
          )}
59
        </Modal.Body>
60
        <Modal.Footer>
61
          <Button type="submit">{labels.create_profile}</Button>
62
          <Button onClick={onHide} variant="danger">
63
            {labels.cancel}
64
          </Button>
65
        </Modal.Footer>
66
      </form>
67
    </Modal>
68
  )
69
}
70
 
71
const mapDispatchToProps = {
72
  addNotification: (notification) => addNotification(notification),
73
}
74
 
75
export default connect(null, mapDispatchToProps)(AddProfileModal)