Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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