| 6768 |
stevensc |
1 |
import React, { useState, useRef, useEffect } from 'react'
|
|
|
2 |
import { axios } from '../../utils'
|
|
|
3 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
4 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
5 |
import IconButton from '@mui/material/IconButton'
|
|
|
6 |
import AddIcon from '@mui/icons-material/Add'
|
|
|
7 |
|
|
|
8 |
import EmptySection from '../UI/EmptySection'
|
|
|
9 |
import ExperienceItem from './ExperienceItem'
|
|
|
10 |
import ExperienceModal from './ExperienceModal'
|
|
|
11 |
|
|
|
12 |
const Experiences = ({ experiences = [], months = [], userId, isEdit }) => {
|
|
|
13 |
const [settedExperiences, setSettedExperiences] = useState([])
|
|
|
14 |
const [isModalOpen, setIsModalOpen] = useState(false)
|
|
|
15 |
const actionUrl = useRef('')
|
|
|
16 |
const actionType = useRef('add')
|
|
|
17 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
18 |
const dispatch = useDispatch()
|
|
|
19 |
|
|
|
20 |
const deleteExperience = (url) => {
|
|
|
21 |
axios
|
|
|
22 |
.post(url)
|
|
|
23 |
.then(({ data: response }) => {
|
|
|
24 |
const { success, data } = response
|
|
|
25 |
if (!success) {
|
|
|
26 |
dispatch(addNotification({ style: 'danger', msg: response.data }))
|
|
|
27 |
return
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
setSettedExperiences(data)
|
|
|
31 |
})
|
|
|
32 |
.catch((err) => {
|
|
|
33 |
dispatch(
|
|
|
34 |
addNotification({ style: 'danger', msg: labels.there_was_an_error })
|
|
|
35 |
)
|
|
|
36 |
throw new Error(err)
|
|
|
37 |
})
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
const addExperience = () => {
|
|
|
41 |
actionUrl.current = `/profile/my-profiles/experience/${userId}/operation/add`
|
|
|
42 |
actionType.current = 'add'
|
|
|
43 |
setIsModalOpen(true)
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
const editExperience = async (url) => {
|
|
|
47 |
actionUrl.current = url
|
|
|
48 |
actionType.current = 'edit'
|
|
|
49 |
setIsModalOpen(true)
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
useEffect(() => {
|
|
|
53 |
setSettedExperiences(experiences)
|
|
|
54 |
}, [experiences])
|
|
|
55 |
|
|
|
56 |
return (
|
|
|
57 |
<>
|
|
|
58 |
<div className="profile-attr">
|
|
|
59 |
<div className="profile-attr-header">
|
|
|
60 |
<h2>{labels.experience}</h2>
|
|
|
61 |
{isEdit && (
|
|
|
62 |
<IconButton onClick={addExperience}>
|
|
|
63 |
<AddIcon />
|
|
|
64 |
</IconButton>
|
|
|
65 |
)}
|
|
|
66 |
</div>
|
|
|
67 |
{settedExperiences.length ? (
|
|
|
68 |
settedExperiences.map((experience) => (
|
|
|
69 |
<ExperienceItem
|
|
|
70 |
key={`${experience.company} - ${experience.title}`}
|
|
|
71 |
experience={experience}
|
|
|
72 |
months={months}
|
|
|
73 |
onDelete={deleteExperience}
|
|
|
74 |
onEdit={editExperience}
|
|
|
75 |
isEdit={isEdit}
|
|
|
76 |
/>
|
|
|
77 |
))
|
|
|
78 |
) : (
|
|
|
79 |
<EmptySection align="left" message={labels.empty} />
|
|
|
80 |
)}
|
|
|
81 |
</div>
|
|
|
82 |
<ExperienceModal
|
|
|
83 |
show={isModalOpen}
|
|
|
84 |
url={actionUrl.current}
|
|
|
85 |
onClose={() => setIsModalOpen(false)}
|
|
|
86 |
onComplete={(value) => setSettedExperiences(value)}
|
|
|
87 |
isEdit={actionType.current === 'edit'}
|
|
|
88 |
/>
|
|
|
89 |
</>
|
|
|
90 |
)
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
export default Experiences
|