Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
9812 stevensc 1
import React, { useState, useEffect } from 'react'
9759 stevensc 2
import { useForm } from 'react-hook-form'
9782 stevensc 3
import Datetime from 'react-datetime'
9814 stevensc 4
import axios from 'axios'
9791 stevensc 5
import SearchLocationInput from '../../../shared/SearchLocationInput'
9812 stevensc 6
import DescriptionInput from '../../../shared/DescriptionInput'
7
import { useHistory, useParams } from 'react-router-dom'
9782 stevensc 8
import "react-datetime/css/react-datetime.css"
9832 stevensc 9
import { validate } from 'uuid'
9839 stevensc 10
import { addNotification } from '../../../redux/notification/notification.actions'
11
import { useDispatch } from 'react-redux'
9469 stevensc 12
 
9779 stevensc 13
const FormView = ({ actionLink, googleApiKey }) => {
9759 stevensc 14
 
9812 stevensc 15
  const history = useHistory()
9839 stevensc 16
  const { action } = useParams()
17
  const dispatch = useDispatch()
9812 stevensc 18
 
9839 stevensc 19
 
9874 stevensc 20
  const { handleSubmit, register, setValue, errors, watch } = useForm()
9802 stevensc 21
 
9832 stevensc 22
  const [inputErrors, setInputErrors] = useState({})
9779 stevensc 23
  const [location, setLocation] = useState({})
9802 stevensc 24
  const [isActive, setIsActive] = useState(false)
9833 stevensc 25
  const [year, setYear] = useState('')
9779 stevensc 26
  const [locationLabel, setLocationLabel] = useState('')
27
  const [jobsDescriptions, setJobsDescriptions] = useState([
28
    {
29
      value: "8ff86a9a-651c-4dd0-86c1-b9c0716d09e0",
30
      label: "Programador Junior"
31
    }
32
  ])
33
  const [jobsCategory, setJobsCategory] = useState([
34
    {
35
      value: "7bd009b8-bd25-4602-bf85-9496af80afbd",
36
      label: "Finanzas"
37
    }
38
  ])
39
  const [industry, setIndustry] = useState([
40
    {
41
      value: "307c261f-2d27-4b3c-a86a-6f69a596edb8",
42
      label: "Bienes raíces"
43
    }
44
  ])
9759 stevensc 45
 
9779 stevensc 46
 
9759 stevensc 47
  const onSubmit = (data) => {
9838 stevensc 48
 
49
    const formData = new FormData()
9831 stevensc 50
    const submitData = {
51
      ...data,
52
      ...location,
53
      last_date: year,
54
      status: isActive ? "a" : "i"
55
    }
56
 
9837 stevensc 57
    if (!location.formatted_address) {
9838 stevensc 58
      return setInputErrors(prev => ({ ...prev, location: "Es requerida una ubicación" }))
9837 stevensc 59
    }
60
    if (!year) {
9838 stevensc 61
      return setInputErrors(prev => ({ ...prev, year: "Este campo es requerido" }))
9837 stevensc 62
    }
9833 stevensc 63
 
9838 stevensc 64
    Object.entries(submitData).forEach((entries) => {
65
      formData.append(entries[0], entries[1])
66
    })
67
 
68
    axios.post(actionLink, formData)
69
      .then(({ data }) => {
70
        if (!data.success) {
71
          dispatch(addNotification({
72
            style: "error",
73
            msg: "Ha ocurrido un error"
74
          }))
75
          return
76
        }
9842 stevensc 77
        history.goBack()
9838 stevensc 78
        dispatch(addNotification({
79
          style: "success",
80
          msg: `Registro ${action === "edit" ? "actualizado" : "guardado"}`
81
        }))
82
      })
9839 stevensc 83
      .catch((err) => dispatch(addNotification({
84
        style: "error",
85
        msg: "Ha ocurrido un error"
86
      })))
9838 stevensc 87
 
9759 stevensc 88
  }
9802 stevensc 89
 
9831 stevensc 90
  useEffect(() => register("description"), [])
91
 
9812 stevensc 92
  useEffect(() => {
9814 stevensc 93
    if (action === "edit") {
94
      axios.get(actionLink)
9869 stevensc 95
        .then(({ data }) => {
96
          if (data.success) {
97
            setValue("name", data.data.name)
98
            setValue("description", data.data.description)
99
            setLocationLabel(data.data.formatted_address)
100
            setYear(data.data.last_date)
101
            data.data.status === "a" ? setIsActive(true) : setIsActive(false)
102
          }
103
        })
9814 stevensc 104
    }
105
  }, [action])
9812 stevensc 106
 
9469 stevensc 107
  return (
9759 stevensc 108
    <section className="container">
109
      <div className="row">
110
        <div className="col-xs-12 col-md-12">
111
          <form onSubmit={handleSubmit(onSubmit)}>
112
            <div className="form-group">
113
              <label>Nombre</label>
114
              <input
115
                type="text"
116
                name="name"
117
                className="form-control"
9761 stevensc 118
                ref={register({ required: true, maxLength: 120 })}
9759 stevensc 119
              />
9837 stevensc 120
              {errors.name && <p>Este campo es requerido</p>}
9759 stevensc 121
            </div>
9779 stevensc 122
            <div className="form-group">
123
              <label>Cargo a evaluar</label>
9832 stevensc 124
              <select name="job_description_id" className="form-control" ref={register({ required: true })} onChange={(e) => setJobsDescriptions(e.target.value)}>
9779 stevensc 125
                {
126
                  jobsDescriptions.map(({ label, value }) => (
127
                    <option value={value}>{label}</option>
128
                  ))
129
                }
130
              </select>
131
            </div>
132
            <div className="form-group">
133
              <label>Categoría de Empleo</label>
9832 stevensc 134
              <select name="job_category_id" className="form-control" ref={register({ required: true })} onChange={(e) => setJobsCategory(e.target.value)}>
9779 stevensc 135
                {
136
                  jobsCategory.map(({ label, value }) => (
137
                    <option value={value}>{label}</option>
138
                  ))
139
                }
140
              </select>
141
            </div>
142
            <div className="form-group">
143
              <label>Ubicación</label>
9791 stevensc 144
              <SearchLocationInput
9779 stevensc 145
                value={locationLabel}
146
                setValue={setLocationLabel}
147
                googleApiKey={googleApiKey}
148
                updateData={setLocation}
149
              />
9838 stevensc 150
              {inputErrors.location && <p>{inputErrors.location}</p>}
9779 stevensc 151
            </div>
152
            <div className="form-group">
153
              <label>Industria</label>
9832 stevensc 154
              <select name="industry_id" className="form-control" ref={register({ required: true })} onChange={(e) => setIndustry(e.target.value)}>
9779 stevensc 155
                {
156
                  industry.map(({ label, value }) => (
157
                    <option value={value}>{label}</option>
158
                  ))
159
                }
160
              </select>
161
            </div>
162
            <div className="form-group">
163
              <label>Último día de aplicación</label>
9791 stevensc 164
              <Datetime
165
                dateFormat="DD-MM-YYYY"
166
                timeFormat={false}
9873 stevensc 167
                initialValue={action === "edit" ? Date.UTC(year) : Date.now()}
9791 stevensc 168
                onChange={(e) =>
169
                  setYear(new Intl.DateTimeFormat({ year: 'numeric', month: 'numeric', day: 'numeric' }).format(e.toDate()))
170
                }
171
                inputProps={{ className: 'form-control' }}
172
                closeOnSelect
173
              />
9838 stevensc 174
              {inputErrors.year && <p>{inputErrors.year}</p>}
9779 stevensc 175
            </div>
9802 stevensc 176
            <div className="form-group">
177
              <label>Descripción</label>
178
              <DescriptionInput
179
                setValue={setValue}
180
                name="description"
9873 stevensc 181
                defaultValue={action === "edit" ? watch("description") : ''}
9802 stevensc 182
              />
183
            </div>
184
            <div className="form-group">
185
              <label>Estatus</label>
186
              <div
187
                className={`toggle btn btn-block btn-primary ${!isActive && "off"}`}
188
                data-toggle="toggle"
189
                role="button"
190
                style={{ width: '130px' }}
191
                onClick={() => setIsActive(!isActive)}
192
              >
193
                <input
194
                  type="checkbox"
195
                  checked={isActive}
196
                />
197
                <div className="toggle-group">
198
                  <label for="status" className="btn btn-primary toggle-on">Activo</label>
199
                  <label for="status" className="btn btn-light toggle-off">Inactivo</label>
200
                  <span className="toggle-handle btn btn-light"></span>
201
                </div>
202
              </div>
203
            </div>
204
            <div className="form-group">
205
              <button
206
                type="submit"
9832 stevensc 207
                className="btn btn-primary btn-form-save-close mr-2"
9802 stevensc 208
              >
209
                Guardar
210
              </button>
211
              <button
212
                type="button"
213
                className="btn btn-secondary btn-edit-cancel"
9812 stevensc 214
                onClick={() => history.goBack()}
9802 stevensc 215
              >
216
                Cancelar
217
              </button>
218
            </div>
9759 stevensc 219
          </form>
220
        </div>
221
      </div>
222
    </section>
9469 stevensc 223
  )
224
}
225
export default FormView