Rev 6346 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
/* eslint-disable react/prop-types */
import React, { useEffect, useState } from 'react'
import { axios } from '../../../utils'
import { useForm } from 'react-hook-form'
import { useDispatch } from 'react-redux'
import { Button, Modal } from 'react-bootstrap'
import { addNotification } from '../../../redux/notification/notification.actions'
import Spinner from '../../../shared/loading-spinner/Spinner'
import TagsInput from '../../../shared/tags-input/TagsInput'
import FormErrorFeedback from '../../../shared/form-error-feedback/FormErrorFeedback'
const AptitudesModal = ({
show = false,
userIdEncrypted = '',
aptitudesOptions = [],
userAptitudes = [],
closeModal = () => {},
setAptitudes = () => {},
}) => {
const { register, errors, handleSubmit, setValue, getValues, setError } =
useForm()
const [modalLoading, setModalLoading] = useState(false)
const [settedAptitudeTags, setSettedAptitudeTags] = useState([])
const dispatch = useDispatch()
const handleModalOpen = (event) => {
event && event.preventDefault()
Object.keys(getValues()).map(([key]) => setValue(key, ''))
closeModal()
}
const handleTagsChange = (tags) => {
if (tags.length) {
let newTags = []
tags.map((tag) => {
newTags = [...newTags, tag.value]
})
setValue('aptitudes', newTags)
setSettedAptitudeTags(tags)
} else {
setValue('aptitudes', '')
setSettedAptitudeTags('')
}
}
const onSubmitHandler = async () => {
setModalLoading(true)
const formData = new FormData()
getValues('aptitudes').map((language) => {
formData.append('aptitudes[]', language)
})
await axios
.post(`/profile/my-profiles/aptitude/${userIdEncrypted}`, formData)
.then((response) => {
const resData = response.data
if (resData.success) {
setAptitudes(settedAptitudeTags)
dispatch(
addNotification({ style: 'success', msg: 'Registro actualizado' })
)
handleModalOpen()
} else {
const resError = resData.data
if (resError.constructor.name === 'Object') {
Object.entries(resError).map(([key, value]) => {
if (key in getValues()) {
setError(key, {
type: 'manual',
message: Array.isArray(value) ? value[0] : value,
})
}
})
} else {
dispatch(addNotification({ style: 'danger', msg: resError }))
}
}
})
setModalLoading(false)
}
useEffect(() => {
register('aptitudes', {
required: 'Por favor seleccione al menos una aptitud',
})
}, [])
return (
<Modal show={show} onHide={handleModalOpen}>
<Modal.Header closeButton>
<Modal.Title>Aptitudes</Modal.Title>
</Modal.Header>
<form onSubmit={handleSubmit(onSubmitHandler)}>
<Modal.Body>
<div className="form-group">
<TagsInput
suggestions={aptitudesOptions}
settedTags={userAptitudes}
onChange={handleTagsChange}
/>
{errors.aptitudes && (
<FormErrorFeedback>{errors.aptitudes.message}</FormErrorFeedback>
)}
</div>
</Modal.Body>
<Modal.Footer>
<Button size="sm" type="submit">
Enviar
</Button>
<Button size="sm" variant="danger" onClick={handleModalOpen}>
Cancelar
</Button>
</Modal.Footer>
</form>
{modalLoading && <Spinner />}
</Modal>
)
}
export default AptitudesModal