Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5489 stevensc 1
/* eslint-disable array-callback-return */
2
/* eslint-disable react/prop-types */
3
import React, { useEffect, useState } from 'react'
4
import { axios } from '../../../utils'
5
import { useForm } from 'react-hook-form'
6
import { useDispatch } from 'react-redux'
7
import { Button, Modal } from 'react-bootstrap'
8
import { addNotification } from '../../../redux/notification/notification.actions'
9
 
10
import Spinner from '../../../shared/loading-spinner/Spinner'
11
import TagsInput from '../../../shared/tags-input/TagsInput'
12
import FormErrorFeedback from '../../../shared/form-error-feedback/FormErrorFeedback'
13
 
14
const SkillsModal = ({
15
  setSkills,
16
  userIdEncrypted,
17
  closeModal,
18
  show,
19
  skillsOptions,
20
  userSkillsArray,
21
}) => {
22
  const { register, errors, handleSubmit, setValue, getValues, setError } =
23
    useForm()
24
  const [settedSkillTags, setSettedSkillTags] = useState([])
25
  const [modalLoading, setModalLoading] = useState(false)
26
  const dispatch = useDispatch()
27
 
28
  const onSubmitHandler = async () => {
29
    setModalLoading(true)
30
    const formData = new FormData()
31
    getValues('skills').map((language) => formData.append('skills[]', language))
32
 
33
    await axios
34
      .post(`/profile/my-profiles/skill/${userIdEncrypted}`, formData)
35
      .then(({ data: response }) => {
36
        if (!response.success) {
37
          const responseError = response.data
38
 
39
          if (responseError.constructor.name === 'string') {
40
            dispatch(addNotification({ style: 'danger', msg: responseError }))
41
            return
42
          }
43
 
44
          Object.entries(responseError).map(([key, value]) => {
45
            if (key in getValues()) {
46
              setError(key, {
47
                type: 'manual',
48
                message: Array.isArray(value) ? value[0] : value,
49
              })
50
            }
51
          })
52
        }
53
 
54
        setSkills(settedSkillTags)
55
        dispatch(
56
          addNotification({ style: 'success', msg: 'Registro agregado' })
57
        )
58
 
59
        handleModalOpen()
60
      })
61
      .finally(() => setModalLoading(false))
62
  }
63
 
64
  const handleTagsChange = (tags) => {
65
    if (tags.length) {
66
      let newTags = []
67
      tags.map((tag) => {
68
        newTags = [...newTags, tag.value]
69
      })
70
      setValue('skills', newTags)
71
      setSettedSkillTags(tags)
72
    } else {
73
      setValue('skills', '')
74
      setSettedSkillTags('')
75
    }
76
  }
77
 
78
  const handleModalOpen = () => {
79
    Object.keys(getValues()).map(([key]) => setValue(key, ''))
80
    closeModal()
81
  }
82
 
83
  useEffect(() => {
84
    register('skills', {
85
      required: 'Por favor seleccione al menos una habilidad',
86
    })
87
  }, [])
88
 
89
  return (
90
    <Modal show={show} onHide={handleModalOpen}>
91
      <Modal.Header closeButton>
92
        <Modal.Title>Habilidades</Modal.Title>
93
      </Modal.Header>
94
      <form onSubmit={handleSubmit(onSubmitHandler)}>
95
        <Modal.Body>
96
          <div className="form-group">
97
            <TagsInput
98
              suggestions={skillsOptions}
99
              settedTags={userSkillsArray}
100
              onChange={handleTagsChange}
101
            />
102
            {errors.skills && (
103
              <FormErrorFeedback>{errors.skills.message}</FormErrorFeedback>
104
            )}
105
          </div>
106
        </Modal.Body>
107
        <Modal.Footer>
108
          <Button size="sm" type="submit">
109
            Enviar
110
          </Button>
111
          <Button size="sm" variant="danger" onClick={handleModalOpen}>
112
            Cancelar
113
          </Button>
114
        </Modal.Footer>
115
      </form>
116
      {modalLoading && <Spinner />}
117
    </Modal>
118
  )
119
}
120
 
121
export default SkillsModal