Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
7268 stevensc 1
import React, { useState } from 'react'
2
import { axios } from '../../utils'
3
import { Button, Modal } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
5
import { connect } from 'react-redux'
6
import { addNotification } from '../../redux/notification/notification.actions'
7
 
8
import Spinner from '../UI/Spinner'
9
import LoaderContainer from '../UI/LoaderContainer'
10
import FormErrorFeedback from '../UI/FormErrorFeedback'
11
 
12
const ApplyModal = ({
13
  onHide = function () {},
14
  jobId,
15
  show,
16
  userProfiles,
17
  onApplied,
18
  addNotification = function () {}, // redux destructuring
19
}) => {
20
  const [loading, setLoading] = useState(false)
21
  const { register, handleSubmit, errors, getValues, setError } = useForm()
22
 
23
  const handleOnSubmit = async (data) => {
24
    setLoading(true)
25
    const formData = new FormData()
26
    Object.entries(data).map(([key, value]) => formData.append(key, value))
27
 
28
    await axios.post(`/job/apply-job/${jobId}`, formData).then(({ data }) => {
29
      if (data.success) {
30
        onApplied()
31
        onHide()
32
      } else {
33
        if (typeof data.data === 'object') {
34
          Object.entries(data.data).map(([key, value]) => {
35
            if (key in getValues()) {
36
              setError(key, { type: 'manual', message: value[0] })
37
            }
38
          })
39
        } else if (typeof data.data === 'string') {
40
          addNotification({
41
            style: 'danger',
42
            msg: data.data,
43
          })
44
        } else {
45
          addNotification({
46
            style: 'danger',
47
            msg: 'Ha ocurrido un error, por favor intente más tarde',
48
          })
49
        }
50
      }
51
    })
52
    setLoading(false)
53
  }
54
 
55
  return (
56
    <Modal
57
      show={show}
58
      onHide={onHide}
59
      aria-labelledby="contained-modal-title-vcenter"
60
      centered
61
    >
62
      <Modal.Header closeButton>
63
        <Modal.Title id="contained-modal-title-vcenter">
64
          Perfil de Applicación
65
        </Modal.Title>
66
      </Modal.Header>
67
      <form onSubmit={handleSubmit(handleOnSubmit)}>
68
        <Modal.Body>
69
          <div className="form-groupp">
70
            <select
71
              name="user_profile_id"
72
              id="user_profile_id"
73
              defaultValue=""
74
              ref={register({
75
                required: 'Por favor seleccione un perfil',
76
              })}
77
            >
78
              <option value="">Perfil de applicación</option>
7314 stevensc 79
              {userProfiles &&
80
                Object.entries(userProfiles).map(([key, value]) => (
81
                  <option value={key} key={key}>
82
                    {value}
83
                  </option>
84
                ))}
7268 stevensc 85
            </select>
86
            {errors?.user_profile_id?.message && (
87
              <FormErrorFeedback>
88
                {errors.user_profile_id.message}
89
              </FormErrorFeedback>
90
            )}
91
          </div>
92
        </Modal.Body>
93
        <Modal.Footer>
94
          <Button type="submit">Aplicar</Button>
95
          <Button onClick={onHide}>Cancelar</Button>
96
        </Modal.Footer>
97
      </form>
98
      {loading && (
99
        <LoaderContainer>
100
          <Spinner />
101
        </LoaderContainer>
102
      )}
103
    </Modal>
104
  )
105
}
106
 
107
const mapDispatchToProps = {
108
  addNotification: (notification) => addNotification(notification),
109
}
110
 
111
export default connect(null, mapDispatchToProps)(ApplyModal)