Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6353 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5489 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { Button, Modal } from 'react-bootstrap'
3
import { axios } from '../../../utils'
4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
6
import { addNotification } from '../../../redux/notification/notification.actions'
7
 
8
import Spinner from '../../../shared/loading-spinner/Spinner'
9
import TagsInput from '../../../shared/tags-input/TagsInput'
10
 
11
const HobbiesModal = ({
12
  show = false,
13
  userIdEncrypted = '',
14
  userHobbiesAndInterests = [],
15
  hobbiesAndInterestsOptions = [],
16
  closeModal = () => {},
17
  setUserHobbiesAndInterests = () => {},
18
}) => {
6354 stevensc 19
  const { register, handleSubmit, setValue } = useForm()
5489 stevensc 20
  const [modalLoading, setModalLoading] = useState(false)
21
  const dispatch = useDispatch()
22
 
23
  const handleTagsChange = (tags) => {
6352 stevensc 24
    setValue('hobbiesAndInterests', tags)
5489 stevensc 25
  }
26
 
6352 stevensc 27
  const onSubmitHandler = async ({ hobbiesAndInterests }) => {
5489 stevensc 28
    setModalLoading(true)
29
    const formData = new FormData()
6352 stevensc 30
 
31
    hobbiesAndInterests.map((language) =>
6353 stevensc 32
      formData.append('hobbies_and_interests[]', language.value)
6352 stevensc 33
    )
34
 
35
    axios
5489 stevensc 36
      .post(
37
        `/profile/my-profiles/hobby-and-interest/${userIdEncrypted}`,
38
        formData
39
      )
6352 stevensc 40
      .then(({ data: response }) => {
41
        const { data, success } = response
42
        if (!success) {
6353 stevensc 43
          const errorMessage = data.hobbies_and_interests[0]
6352 stevensc 44
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
45
          return
5489 stevensc 46
        }
6352 stevensc 47
 
48
        setUserHobbiesAndInterests(hobbiesAndInterests)
49
        dispatch(
50
          addNotification({ style: 'success', msg: 'Registro actualizado' })
51
        )
52
        closeModal()
5489 stevensc 53
      })
6352 stevensc 54
      .catch((error) => {
55
        dispatch(addNotification({ style: 'danger', msg: error }))
56
        throw new Error(error)
57
      })
58
      .finally(() => setModalLoading(false))
5489 stevensc 59
  }
60
 
61
  useEffect(() => {
6352 stevensc 62
    register('hobbiesAndInterests')
5489 stevensc 63
  }, [])
64
 
6352 stevensc 65
  useEffect(() => {
66
    show
67
      ? setValue('hobbiesAndInterests', userHobbiesAndInterests)
68
      : setValue('hobbiesAndInterests', [''])
69
  }, [show])
70
 
5489 stevensc 71
  return (
6352 stevensc 72
    <Modal show={show} onHide={closeModal}>
5489 stevensc 73
      <Modal.Header closeButton>
74
        <Modal.Title>Pasatiempos e intereses</Modal.Title>
75
      </Modal.Header>
76
      <form onSubmit={handleSubmit(onSubmitHandler)}>
77
        <Modal.Body>
78
          <div className="form-group">
79
            <TagsInput
80
              suggestions={hobbiesAndInterestsOptions}
81
              settedTags={userHobbiesAndInterests}
82
              onChange={handleTagsChange}
83
            />
84
          </div>
85
        </Modal.Body>
86
        <Modal.Footer>
87
          <Button size="sm" type="submit">
88
            Enviar
89
          </Button>
6352 stevensc 90
          <Button size="sm" variant="danger" onClick={closeModal}>
5489 stevensc 91
            Cancelar
92
          </Button>
93
        </Modal.Footer>
94
      </form>
95
      {modalLoading && <Spinner />}
96
    </Modal>
97
  )
98
}
99
 
100
export default HobbiesModal