Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6349 | Rev 6351 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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