| 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
|
| 6887 |
stevensc |
27 |
|
|
|
28 |
if (!success) {
|
|
|
29 |
const errorMessage =
|
|
|
30 |
typeof data === 'string'
|
|
|
31 |
? data
|
|
|
32 |
: 'Ha ocurrido un error, por favor intente más tarde'
|
|
|
33 |
addNotification({ style: 'success', msg: errorMessage })
|
| 6861 |
stevensc |
34 |
return
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
getProfiles()
|
|
|
38 |
addNotification({ style: 'success', msg: data })
|
|
|
39 |
onHide()
|
|
|
40 |
})
|
|
|
41 |
.catch((err) => {
|
|
|
42 |
console.log(err)
|
|
|
43 |
throw new Error(err)
|
|
|
44 |
})
|
| 6727 |
stevensc |
45 |
}
|
|
|
46 |
|
|
|
47 |
return (
|
|
|
48 |
<Modal show={show} onHide={onHide}>
|
|
|
49 |
<Modal.Header closeButton>
|
|
|
50 |
<Modal.Title id="contained-modal-title-vcenter">
|
|
|
51 |
{labels.new_profile}
|
|
|
52 |
</Modal.Title>
|
|
|
53 |
</Modal.Header>
|
|
|
54 |
<form onSubmit={handleSubmit(onSubmitHandler)}>
|
|
|
55 |
<Modal.Body>
|
|
|
56 |
<input
|
|
|
57 |
type="text"
|
|
|
58 |
name="name"
|
|
|
59 |
placeholder={labels.profile_name}
|
|
|
60 |
ref={register({ required: 'Este campo es requerido' })}
|
|
|
61 |
/>
|
|
|
62 |
{errors.name && (
|
|
|
63 |
<FormErrorFeedback>{errors.name.message}</FormErrorFeedback>
|
|
|
64 |
)}
|
|
|
65 |
</Modal.Body>
|
|
|
66 |
<Modal.Footer>
|
|
|
67 |
<Button type="submit">{labels.create_profile}</Button>
|
|
|
68 |
<Button onClick={onHide} variant="danger">
|
|
|
69 |
{labels.cancel}
|
|
|
70 |
</Button>
|
|
|
71 |
</Modal.Footer>
|
|
|
72 |
</form>
|
|
|
73 |
</Modal>
|
|
|
74 |
)
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
const mapDispatchToProps = {
|
|
|
78 |
addNotification: (notification) => addNotification(notification),
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
export default connect(null, mapDispatchToProps)(AddProfileModal)
|