Rev 5481 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
/* eslint-disable react/prop-types */
import React, { useState, useRef, useEffect } from 'react'
import { axios } from '../../../utils'
import { useSelector } from 'react-redux'
import AddIcon from '@mui/icons-material/Add'
import IconButton from '@mui/material/IconButton'
import Spinner from '../../../shared/loading-spinner/Spinner'
import EducationModal from './EducationModal'
import EducationItem from './EducationItem'
import EmptySection from '../../../shared/empty-section/EmptySection'
const Educations = ({ educations, degreesOptions, userId, isEdit }) => {
const [settedEducations, setSettedEducations] = useState(educations)
const [settedDescription, setSettedDescription] = useState('')
const [isModalOpen, setIsModalOpen] = useState(false)
const [loading, setLoading] = useState(false)
const labels = useSelector((state) => state.labels)
const postUrl = useRef('')
const addEducation = () => {
postUrl.current = `/profile/my-profiles/education/${userId}/operation/add`
setSettedDescription('')
setIsModalOpen(true)
}
const handleDelete = async (delete_link) => {
setLoading(true)
axios
.post(delete_link)
.then(({ data: response }) => {
if (response.success) setSettedEducations(response.data)
})
.finally(() => setLoading(false))
}
const handleEdit = async (edit_link) => {
postUrl.current = edit_link
const currentDescription = settedEducations.find(
(education) => education.link_edit === edit_link
)
setSettedDescription(currentDescription.description)
setIsModalOpen(true)
}
useEffect(() => {
setSettedEducations(educations)
}, [educations])
return (
<>
<div className="profile-attr">
<div className="profile-attr-header">
<h2>Educación</h2>
{isEdit && (
<IconButton onClick={addEducation}>
<AddIcon />
</IconButton>
)}
</div>
{loading && <Spinner />}
{settedEducations.length ? (
settedEducations.map((education) => (
<EducationItem
key={education.link_edit}
education={education}
onDelete={handleDelete}
onEdit={handleEdit}
isEdit={isEdit}
/>
))
) : (
<EmptySection align="left" message={labels.EMPTY} />
)}
</div>
<EducationModal
degreesOptions={degreesOptions}
closeModal={() => setIsModalOpen(false)}
postUrl={postUrl.current}
setEducations={(newEducations) => setSettedEducations(newEducations)}
settedDescription={settedDescription}
show={isModalOpen}
/>
</>
)
}
export default Educations