Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
6753 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios } from '../../utils'
3
import { useForm } from 'react-hook-form'
4
import { useDispatch, useSelector } from 'react-redux'
5
import { Button, Modal } from 'react-bootstrap'
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'
6807 stevensc 10
import useFetchHelper from '../../hooks/useFetchHelper'
6753 stevensc 11
 
12
const AptitudesModal = ({
13
  show = false,
6807 stevensc 14
  userId = '',
6753 stevensc 15
  userAptitudes = [],
6807 stevensc 16
  onClose = () => {},
17
  onComplete = () => {},
6753 stevensc 18
}) => {
19
  const [modalLoading, setModalLoading] = useState(false)
6807 stevensc 20
  const { data: aptitudes } = useFetchHelper('aptitudes')
6753 stevensc 21
  const labels = useSelector(({ intl }) => intl.labels)
22
  const dispatch = useDispatch()
23
 
24
  const { register, handleSubmit, setValue } = useForm()
25
 
26
  const handleTagsChange = (tags) => {
27
    setValue('aptitudes', tags)
28
  }
29
 
30
  const onSubmitHandler = async ({ aptitudes }) => {
31
    setModalLoading(true)
32
 
33
    const formData = new FormData()
34
    aptitudes.map((aptitude) => formData.append('aptitudes[]', aptitude.value))
35
 
36
    axios
6807 stevensc 37
      .post(`/profile/my-profiles/aptitude/${userId}`, formData)
6753 stevensc 38
      .then(({ data: response }) => {
39
        const { data, success } = response
40
        if (!success) {
41
          const errorMessage = data.aptitudes[0]
42
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
43
          return
44
        }
45
 
6807 stevensc 46
        onComplete(aptitudes)
6753 stevensc 47
        dispatch(
48
          addNotification({ style: 'success', msg: 'Registro actualizado' })
49
        )
50
 
6807 stevensc 51
        onClose()
6753 stevensc 52
      })
53
      .catch((error) => {
54
        dispatch(addNotification({ style: 'danger', msg: error }))
55
        throw new Error(error)
56
      })
57
      .finally(() => setModalLoading(false))
58
  }
59
 
60
  useEffect(() => {
61
    register('aptitudes')
62
  }, [])
63
 
64
  useEffect(() => {
65
    show ? setValue('aptitudes', userAptitudes) : setValue('aptitudes', [''])
66
  }, [show])
67
 
68
  return (
6807 stevensc 69
    <Modal show={show} onHide={onClose}>
6753 stevensc 70
      <Modal.Header closeButton>
71
        <Modal.Title>{labels.aptitudes}</Modal.Title>
72
      </Modal.Header>
73
      <form onSubmit={handleSubmit(onSubmitHandler)}>
74
        <Modal.Body>
75
          <div className="form-group">
76
            <TagsInput
6807 stevensc 77
              suggestions={aptitudes}
6753 stevensc 78
              settedTags={userAptitudes}
79
              onChange={handleTagsChange}
80
            />
81
          </div>
82
        </Modal.Body>
83
        <Modal.Footer>
84
          <Button size="sm" type="submit">
85
            {labels.accept}
86
          </Button>
6807 stevensc 87
          <Button size="sm" variant="danger" onClick={onClose}>
6753 stevensc 88
            {labels.cancel}
89
          </Button>
90
        </Modal.Footer>
91
      </form>
92
      {modalLoading && <Spinner />}
93
    </Modal>
94
  )
95
}
96
 
97
export default AptitudesModal