5489 |
stevensc |
1 |
/* eslint-disable react/prop-types */
|
|
|
2 |
import React from 'react'
|
|
|
3 |
import { Button, Modal } from 'react-bootstrap'
|
|
|
4 |
import { useForm } from 'react-hook-form'
|
|
|
5 |
import { connect } from 'react-redux'
|
|
|
6 |
import { axios } from '../../../../utils'
|
|
|
7 |
import { addNotification } from '../../../../redux/notification/notification.actions'
|
|
|
8 |
import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'
|
|
|
9 |
|
|
|
10 |
const AddProfileModal = ({
|
|
|
11 |
show = '',
|
|
|
12 |
onHide = () => null,
|
|
|
13 |
getProfiles = () => null,
|
|
|
14 |
addNotification // redux action
|
|
|
15 |
}) => {
|
|
|
16 |
// React hook form
|
|
|
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 |
|
|
|
23 |
await axios.post('/profile/my-profiles/add', formData)
|
|
|
24 |
.then(({ data }) => {
|
|
|
25 |
if (data.success) {
|
|
|
26 |
return addNotification({ style: 'success', msg: data.data })
|
|
|
27 |
}
|
|
|
28 |
})
|
|
|
29 |
onHide()
|
|
|
30 |
getProfiles()
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
return (
|
|
|
34 |
<Modal show={show} onHide={onHide}>
|
|
|
35 |
<Modal.Header closeButton>
|
|
|
36 |
<Modal.Title id="contained-modal-title-vcenter">
|
|
|
37 |
{LABELS.NEW_PROFILE}
|
|
|
38 |
</Modal.Title>
|
|
|
39 |
</Modal.Header>
|
|
|
40 |
<form onSubmit={handleSubmit(onSubmitHandler)}>
|
|
|
41 |
<Modal.Body>
|
|
|
42 |
<input
|
|
|
43 |
type="text"
|
|
|
44 |
name="name"
|
|
|
45 |
placeholder={LABELS.PROFILE_MAME}
|
|
|
46 |
ref={register({ required: 'Este campo es requerido' })}
|
|
|
47 |
/>
|
|
|
48 |
{errors.name && <FormErrorFeedback>{errors.name.message}</FormErrorFeedback>}
|
|
|
49 |
</Modal.Body>
|
|
|
50 |
<Modal.Footer>
|
|
|
51 |
<Button type="submit">
|
|
|
52 |
{LABELS.CREATE_PROFILE}
|
|
|
53 |
</Button>
|
|
|
54 |
<Button onClick={onHide} variant="danger">
|
|
|
55 |
{LABELS.CANCEL}
|
|
|
56 |
</Button>
|
|
|
57 |
</Modal.Footer>
|
|
|
58 |
</form>
|
|
|
59 |
</Modal>
|
|
|
60 |
)
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
const mapDispatchToProps = {
|
|
|
64 |
addNotification: (notification) => addNotification(notification)
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
export default connect(null, mapDispatchToProps)(AddProfileModal)
|