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