6753 |
stevensc |
1 |
/* eslint-disable react/prop-types */
|
|
|
2 |
import React, { useEffect, useState } from 'react'
|
|
|
3 |
import { axios } from '../../utils'
|
|
|
4 |
import { useForm } from 'react-hook-form'
|
|
|
5 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
6 |
import { Button, Modal } from 'react-bootstrap'
|
|
|
7 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
8 |
|
|
|
9 |
import Spinner from '../../../shared/loading-spinner/Spinner'
|
|
|
10 |
import TagsInput from '../../../shared/tags-input/TagsInput'
|
|
|
11 |
|
|
|
12 |
const AptitudesModal = ({
|
|
|
13 |
show = false,
|
|
|
14 |
userIdEncrypted = '',
|
|
|
15 |
aptitudesOptions = [],
|
|
|
16 |
userAptitudes = [],
|
|
|
17 |
closeModal = () => {},
|
|
|
18 |
setAptitudes = () => {},
|
|
|
19 |
}) => {
|
|
|
20 |
const [modalLoading, setModalLoading] = useState(false)
|
|
|
21 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
22 |
const dispatch = useDispatch()
|
|
|
23 |
|
|
|
24 |
const { register, handleSubmit, setValue } = useForm()
|
|
|
25 |
|
|
|
26 |
const handleTagsChange = (tags) => {
|
|
|
27 |
setValue('aptitudes', tags)
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
const onSubmitHandler = async ({ aptitudes }) => {
|
|
|
31 |
setModalLoading(true)
|
|
|
32 |
|
|
|
33 |
const formData = new FormData()
|
|
|
34 |
aptitudes.map((aptitude) => formData.append('aptitudes[]', aptitude.value))
|
|
|
35 |
|
|
|
36 |
axios
|
|
|
37 |
.post(`/profile/my-profiles/aptitude/${userIdEncrypted}`, formData)
|
|
|
38 |
.then(({ data: response }) => {
|
|
|
39 |
const { data, success } = response
|
|
|
40 |
if (!success) {
|
|
|
41 |
const errorMessage = data.aptitudes[0]
|
|
|
42 |
dispatch(addNotification({ style: 'danger', msg: errorMessage }))
|
|
|
43 |
return
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
setAptitudes(aptitudes)
|
|
|
47 |
dispatch(
|
|
|
48 |
addNotification({ style: 'success', msg: 'Registro actualizado' })
|
|
|
49 |
)
|
|
|
50 |
|
|
|
51 |
closeModal()
|
|
|
52 |
})
|
|
|
53 |
.catch((error) => {
|
|
|
54 |
dispatch(addNotification({ style: 'danger', msg: error }))
|
|
|
55 |
throw new Error(error)
|
|
|
56 |
})
|
|
|
57 |
.finally(() => setModalLoading(false))
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
useEffect(() => {
|
|
|
61 |
register('aptitudes')
|
|
|
62 |
}, [])
|
|
|
63 |
|
|
|
64 |
useEffect(() => {
|
|
|
65 |
show ? setValue('aptitudes', userAptitudes) : setValue('aptitudes', [''])
|
|
|
66 |
}, [show])
|
|
|
67 |
|
|
|
68 |
return (
|
|
|
69 |
<Modal show={show} onHide={closeModal}>
|
|
|
70 |
<Modal.Header closeButton>
|
|
|
71 |
<Modal.Title>{labels.aptitudes}</Modal.Title>
|
|
|
72 |
</Modal.Header>
|
|
|
73 |
<form onSubmit={handleSubmit(onSubmitHandler)}>
|
|
|
74 |
<Modal.Body>
|
|
|
75 |
<div className="form-group">
|
|
|
76 |
<TagsInput
|
|
|
77 |
suggestions={aptitudesOptions}
|
|
|
78 |
settedTags={userAptitudes}
|
|
|
79 |
onChange={handleTagsChange}
|
|
|
80 |
/>
|
|
|
81 |
</div>
|
|
|
82 |
</Modal.Body>
|
|
|
83 |
<Modal.Footer>
|
|
|
84 |
<Button size="sm" type="submit">
|
|
|
85 |
{labels.accept}
|
|
|
86 |
</Button>
|
|
|
87 |
<Button size="sm" variant="danger" onClick={closeModal}>
|
|
|
88 |
{labels.cancel}
|
|
|
89 |
</Button>
|
|
|
90 |
</Modal.Footer>
|
|
|
91 |
</form>
|
|
|
92 |
{modalLoading && <Spinner />}
|
|
|
93 |
</Modal>
|
|
|
94 |
)
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
export default AptitudesModal
|