Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 10114 | Rev 10254 | 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 }) => {
10114 stevensc 96
          const respData = data.data
97
 
9869 stevensc 98
          if (data.success) {
10114 stevensc 99
            setValue("name", respData.name)
100
            setValue("description", respData.description)
101
            setLocationLabel(respData.formatted_address)
102
            setYear(respData.last_date)
103
            respData.status === "a" ? setIsActive(true) : setIsActive(false)
104
 
105
            Object.entries(respData.job_category.job_category_options).map(([value, label]) => {
106
              setJobsCategory(prev => [...prev, { value: value, label: label }])
107
            })
9869 stevensc 108
          }
109
        })
9814 stevensc 110
    }
111
  }, [action])
9812 stevensc 112
 
9469 stevensc 113
  return (
9759 stevensc 114
    <section className="container">
115
      <div className="row">
116
        <div className="col-xs-12 col-md-12">
117
          <form onSubmit={handleSubmit(onSubmit)}>
118
            <div className="form-group">
119
              <label>Nombre</label>
120
              <input
121
                type="text"
122
                name="name"
123
                className="form-control"
9761 stevensc 124
                ref={register({ required: true, maxLength: 120 })}
9759 stevensc 125
              />
9837 stevensc 126
              {errors.name && <p>Este campo es requerido</p>}
9759 stevensc 127
            </div>
9779 stevensc 128
            <div className="form-group">
129
              <label>Cargo a evaluar</label>
10115 stevensc 130
              <select name="job_description_id" className="form-control" ref={register({ required: true })}>
9779 stevensc 131
                {
132
                  jobsDescriptions.map(({ label, value }) => (
133
                    <option value={value}>{label}</option>
134
                  ))
135
                }
136
              </select>
137
            </div>
138
            <div className="form-group">
139
              <label>Categoría de Empleo</label>
10115 stevensc 140
              <select name="job_category_id" className="form-control" ref={register({ required: true })}>
9779 stevensc 141
                {
142
                  jobsCategory.map(({ label, value }) => (
143
                    <option value={value}>{label}</option>
144
                  ))
145
                }
146
              </select>
147
            </div>
148
            <div className="form-group">
149
              <label>Ubicación</label>
9791 stevensc 150
              <SearchLocationInput
9779 stevensc 151
                value={locationLabel}
152
                setValue={setLocationLabel}
153
                googleApiKey={googleApiKey}
154
                updateData={setLocation}
155
              />
9838 stevensc 156
              {inputErrors.location && <p>{inputErrors.location}</p>}
9779 stevensc 157
            </div>
158
            <div className="form-group">
159
              <label>Industria</label>
10115 stevensc 160
              <select name="industry_id" className="form-control" ref={register({ required: true })}>
9779 stevensc 161
                {
162
                  industry.map(({ label, value }) => (
163
                    <option value={value}>{label}</option>
164
                  ))
165
                }
166
              </select>
167
            </div>
168
            <div className="form-group">
169
              <label>Último día de aplicación</label>
9791 stevensc 170
              <Datetime
171
                dateFormat="DD-MM-YYYY"
172
                timeFormat={false}
9873 stevensc 173
                initialValue={action === "edit" ? Date.UTC(year) : Date.now()}
9791 stevensc 174
                onChange={(e) =>
175
                  setYear(new Intl.DateTimeFormat({ year: 'numeric', month: 'numeric', day: 'numeric' }).format(e.toDate()))
176
                }
177
                inputProps={{ className: 'form-control' }}
178
                closeOnSelect
179
              />
9838 stevensc 180
              {inputErrors.year && <p>{inputErrors.year}</p>}
9779 stevensc 181
            </div>
9802 stevensc 182
            <div className="form-group">
183
              <label>Descripción</label>
184
              <DescriptionInput
185
                setValue={setValue}
186
                name="description"
9873 stevensc 187
                defaultValue={action === "edit" ? watch("description") : ''}
9802 stevensc 188
              />
189
            </div>
190
            <div className="form-group">
191
              <label>Estatus</label>
192
              <div
193
                className={`toggle btn btn-block btn-primary ${!isActive && "off"}`}
194
                data-toggle="toggle"
195
                role="button"
196
                style={{ width: '130px' }}
197
                onClick={() => setIsActive(!isActive)}
198
              >
199
                <input
200
                  type="checkbox"
201
                  checked={isActive}
202
                />
203
                <div className="toggle-group">
204
                  <label for="status" className="btn btn-primary toggle-on">Activo</label>
205
                  <label for="status" className="btn btn-light toggle-off">Inactivo</label>
206
                  <span className="toggle-handle btn btn-light"></span>
207
                </div>
208
              </div>
209
            </div>
210
            <div className="form-group">
211
              <button
212
                type="submit"
9832 stevensc 213
                className="btn btn-primary btn-form-save-close mr-2"
9802 stevensc 214
              >
215
                Guardar
216
              </button>
217
              <button
218
                type="button"
219
                className="btn btn-secondary btn-edit-cancel"
9812 stevensc 220
                onClick={() => history.goBack()}
9802 stevensc 221
              >
222
                Cancelar
223
              </button>
224
            </div>
9759 stevensc 225
          </form>
226
        </div>
227
      </div>
228
    </section>
9469 stevensc 229
  )
230
}
231
export default FormView