Proyectos de Subversion LeadersLinked - Backend

Rev

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