Rev 6353 | AutorÃa | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useEffect, useState } from 'react'import { Button, Modal } from 'react-bootstrap'import { axios } from '../../../utils'import { useForm } from 'react-hook-form'import { useDispatch } from 'react-redux'import { addNotification } from '../../../redux/notification/notification.actions'import Spinner from '../../../shared/loading-spinner/Spinner'import TagsInput from '../../../shared/tags-input/TagsInput'const HobbiesModal = ({show = false,userIdEncrypted = '',userHobbiesAndInterests = [],hobbiesAndInterestsOptions = [],closeModal = () => {},setUserHobbiesAndInterests = () => {},}) => {const { register, handleSubmit, setValue } = useForm()const [modalLoading, setModalLoading] = useState(false)const dispatch = useDispatch()const handleTagsChange = (tags) => {setValue('hobbiesAndInterests', tags)}const onSubmitHandler = async ({ hobbiesAndInterests }) => {setModalLoading(true)const formData = new FormData()hobbiesAndInterests.map((language) =>formData.append('hobbies_and_interests[]', language.value))axios.post(`/profile/my-profiles/hobby-and-interest/${userIdEncrypted}`,formData).then(({ data: response }) => {const { data, success } = responseif (!success) {const errorMessage = data.hobbies_and_interests[0]dispatch(addNotification({ style: 'danger', msg: errorMessage }))return}setUserHobbiesAndInterests(hobbiesAndInterests)dispatch(addNotification({ style: 'success', msg: 'Registro actualizado' }))closeModal()}).catch((error) => {dispatch(addNotification({ style: 'danger', msg: error }))throw new Error(error)}).finally(() => setModalLoading(false))}useEffect(() => {register('hobbiesAndInterests')}, [])useEffect(() => {show? setValue('hobbiesAndInterests', userHobbiesAndInterests): setValue('hobbiesAndInterests', [''])}, [show])return (<Modal show={show} onHide={closeModal}><Modal.Header closeButton><Modal.Title>Pasatiempos e intereses</Modal.Title></Modal.Header><form onSubmit={handleSubmit(onSubmitHandler)}><Modal.Body><div className="form-group"><TagsInputsuggestions={hobbiesAndInterestsOptions}settedTags={userHobbiesAndInterests}onChange={handleTagsChange}/></div></Modal.Body><Modal.Footer><Button size="sm" type="submit">Enviar</Button><Button size="sm" variant="danger" onClick={closeModal}>Cancelar</Button></Modal.Footer></form>{modalLoading && <Spinner />}</Modal>)}export default HobbiesModal