Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6348 | Rev 6350 | 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
import FormErrorFeedback from '../../../shared/form-error-feedback/FormErrorFeedback'
12
 
13
const AptitudesModal = ({
14
  show = false,
15
  userIdEncrypted = '',
16
  aptitudesOptions = [],
17
  userAptitudes = [],
18
  closeModal = () => {},
19
  setAptitudes = () => {},
20
}) => {
6349 stevensc 21
  const { register, errors, handleSubmit, setValue, setError } = useForm()
5489 stevensc 22
  const [modalLoading, setModalLoading] = useState(false)
23
  const dispatch = useDispatch()
24
 
6346 stevensc 25
  const handleModalOpen = () => {
6349 stevensc 26
    setValue('aptitudes', '')
5489 stevensc 27
    closeModal()
28
  }
29
 
30
  const handleTagsChange = (tags) => {
6349 stevensc 31
    setValue('aptitudes', tags)
5489 stevensc 32
  }
33
 
6349 stevensc 34
  const onSubmitHandler = async ({ aptitudes }) => {
5489 stevensc 35
    setModalLoading(true)
6348 stevensc 36
 
6349 stevensc 37
    console.log(aptitudes)
38
 
5489 stevensc 39
    const formData = new FormData()
6349 stevensc 40
    aptitudes.map((aptitude) => formData.append('aptitudes[]', aptitude.value))
6347 stevensc 41
 
6349 stevensc 42
    axios
5489 stevensc 43
      .post(`/profile/my-profiles/aptitude/${userIdEncrypted}`, formData)
6347 stevensc 44
      .then(({ data: response }) => {
45
        const { data, success } = response
6349 stevensc 46
        if (!success) {
6347 stevensc 47
          const resError = data
6349 stevensc 48
          typeof resError === 'string'
49
            ? Object.entries(resError).map(([key, value]) => {
50
                setError('aptitudes', {
5489 stevensc 51
                  type: 'manual',
52
                  message: Array.isArray(value) ? value[0] : value,
53
                })
6349 stevensc 54
              })
55
            : dispatch(addNotification({ style: 'danger', msg: resError }))
56
          return
5489 stevensc 57
        }
6349 stevensc 58
 
59
        setAptitudes(aptitudes)
60
        dispatch(
61
          addNotification({ style: 'success', msg: 'Registro actualizado' })
62
        )
63
 
64
        handleModalOpen()
5489 stevensc 65
      })
66
    setModalLoading(false)
67
  }
68
 
69
  useEffect(() => {
6345 stevensc 70
    register('aptitudes')
5489 stevensc 71
  }, [])
72
 
73
  return (
74
    <Modal show={show} onHide={handleModalOpen}>
75
      <Modal.Header closeButton>
76
        <Modal.Title>Aptitudes</Modal.Title>
77
      </Modal.Header>
78
      <form onSubmit={handleSubmit(onSubmitHandler)}>
79
        <Modal.Body>
80
          <div className="form-group">
81
            <TagsInput
82
              suggestions={aptitudesOptions}
83
              settedTags={userAptitudes}
84
              onChange={handleTagsChange}
85
            />
86
            {errors.aptitudes && (
87
              <FormErrorFeedback>{errors.aptitudes.message}</FormErrorFeedback>
88
            )}
89
          </div>
90
        </Modal.Body>
91
        <Modal.Footer>
92
          <Button size="sm" type="submit">
93
            Enviar
94
          </Button>
95
          <Button size="sm" variant="danger" onClick={handleModalOpen}>
96
            Cancelar
97
          </Button>
98
        </Modal.Footer>
99
      </form>
100
      {modalLoading && <Spinner />}
101
    </Modal>
102
  )
103
}
104
 
105
export default AptitudesModal