Rev 1979 | Rev 3432 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState, useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { IconButton } from '@mui/material'
import { Edit } from '@mui/icons-material'
import { axios } from '@utils'
import { addNotification } from '@store/notification/notification.actions'
import TagsList from '@components/UI/TagsList'
import HobbiesModal from './HobbiesModal'
import Widget from '@components/UI/Widget'
const HobbiesCard = ({
hobbies: defaultHobbies = [],
uuid = '',
edit = false
}) => {
const [hobbies, setHobbies] = useState([])
const [showModal, setShoModal] = useState(false)
const labels = useSelector(({ intl }) => intl.labels)
const dispatch = useDispatch()
const toggleModal = () => setShoModal(!showModal)
const handleEditHobbies = ({ hobbies }) => {
const formData = new FormData()
hobbies.map((hobbie) =>
formData.append('hobbies_and_interests[]', hobbie.value)
)
axios
.post(`/profile/my-profiles/hobby-and-interest/${uuid}`, formData)
.then(({ data: response }) => {
const { data, success } = response
if (!success) {
const errorMessage =
typeof data === 'string' ? data : data.hobbies_and_interests[0]
throw new Error(errorMessage)
}
setHobbies(hobbies)
toggleModal()
})
.catch((err) => {
dispatch(addNotification({ style: 'danger', msg: err.message }))
})
}
useEffect(() => {
setHobbies(defaultHobbies)
}, [defaultHobbies])
return (
<>
<Widget>
<Widget.Header
title={labels.hobbies_and_interests}
renderAction={
edit && (
<IconButton onClick={toggleModal}>
<Edit />
</IconButton>
)
}
/>
<Widget.Body>
<TagsList tags={hobbies} />
</Widget.Body>
</Widget>
<HobbiesModal
show={showModal}
onClose={toggleModal}
onConfirm={handleEditHobbies}
hobbies={hobbies}
/>
</>
)
}
export default HobbiesCard