Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
12040 stevensc 1
import axios from 'axios'
2
import React, { useEffect, useState } from 'react'
12096 stevensc 3
import parse from 'html-react-parser'
12040 stevensc 4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
12096 stevensc 6
import { useHistory } from 'react-router-dom'
12040 stevensc 7
import { addNotification } from '../../../redux/notification/notification.actions'
8
import DescriptionInput from '../../../shared/DescriptionInput'
12012 stevensc 9
 
12081 stevensc 10
const levelOptions = {
12139 stevensc 11
	'0': 'Cero',
12081 stevensc 12
	'1': 'Uno',
13
	'2': 'Dos',
14
	'3': 'Tres',
15
	'4': 'Cuatro',
16
	'5': 'Cinco'
17
}
18
 
12040 stevensc 19
const EditView = ({ actionLink }) => {
20
 
12096 stevensc 21
	// Hooks
22
	const history = useHistory()
23
	const { register, setValue, watch, reset } = useForm()
24
	const dispatch = useDispatch()
25
 
26
	// State
12047 stevensc 27
	const [currentJobDescription, setCurretJobDescription] = useState('')
28
	const [jobDescriptionOptions, setJobDescriptionOptions] = useState([])
12081 stevensc 29
	const [competencyTypes, setCompetencyTypes] = useState([])
30
	const [competenciesSelected, setCompetenciesSelected] = useState([])
12047 stevensc 31
	const [status, setStatus] = useState('a')
12040 stevensc 32
 
12096 stevensc 33
	const onSubmit = () => {
12101 stevensc 34
 
35
		const submitData = new FormData()
36
		submitData.append('name', watch('name'))
37
		submitData.append('description', watch('description'))
38
		submitData.append('status', status)
39
		submitData.append('job_description_id', currentJobDescription)
12104 stevensc 40
		submitData.append('content', '[]')
12101 stevensc 41
 
42
		axios.post(actionLink, submitData)
12096 stevensc 43
			.then(({ data }) => {
44
				if (!data.success) {
14616 stevensc 45
					typeof data.data === 'string'
46
						?
47
						dispatch(addNotification({
48
							style: 'danger',
49
							msg: data.data
50
						}))
51
						: Object.entries(data.data).map(([key, value]) =>
52
							value.map(err =>
53
								dispatch(addNotification({
54
									style: 'danger',
55
									msg: `${key}: ${err}`
56
								}))
57
							)
58
						)
59
					return
12096 stevensc 60
				}
61
 
62
				dispatch(addNotification({
63
					style: 'success',
64
					msg: data.data
65
				}))
66
			})
67
	}
68
 
69
	const submitAndClose = () => {
70
		onSubmit()
71
		reset()
72
		history.goBack()
73
	}
74
 
12040 stevensc 75
	useEffect(() => {
76
		register('description')
77
	}, [])
78
 
79
	useEffect(() => {
80
		axios.get(actionLink)
81
			.then(({ data }) => {
82
				if (!data.success) {
83
					return dispatch(addNotification({
84
						style: 'danger',
85
						msg: 'Ha ocurrido un error'
86
					}))
87
				}
88
 
89
				setValue('name', data.data.name)
90
				setValue('description', data.data.description)
91
				setCurretJobDescription(data.data.job_description_id)
92
				setStatus(data.data.status)
93
			})
94
	}, [actionLink])
95
 
96
	useEffect(() => {
97
		axios.get(`/settings/jobs-description/edit/${currentJobDescription}`)
98
			.then(({ data }) => {
99
				if (!data.success) {
100
					return dispatch(addNotification({
101
						style: 'danger',
102
						msg: 'Ha ocurrido un error'
103
					}))
104
				}
105
 
12047 stevensc 106
				setJobDescriptionOptions([...data.data.jobs_description, { job_description_id: data.data.uuid, name: data.data.name }])
12081 stevensc 107
				setCompetenciesSelected(data.data.competencies_selected)
108
				setCompetencyTypes(data.data.competency_types)
12040 stevensc 109
			})
110
	}, [currentJobDescription])
111
 
12012 stevensc 112
	return (
12040 stevensc 113
		<section className="content">
114
			<div className="row" style={{ padding: 16 }}>
115
				<div className="col-xs-12 col-md-12">
12096 stevensc 116
					<div className="form-group">
117
						<label>Nombre</label>
118
						<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
119
					</div>
120
					<div className="form-group">
121
						<label>Cargo a evaluar</label>
122
						<select name="job_description_id" className="form-control" onChange={(e) => setCurretJobDescription(e.target.value)}>
123
							{
124
								jobDescriptionOptions.map(({ name, job_description_id }) => (
125
									<option selected={job_description_id === currentJobDescription} key={job_description_id} value={job_description_id}>{name}</option>
126
								))
127
							}
128
						</select>
129
					</div>
130
					<div className="form-group">
131
						<label htmlFor="form-description">Descripción</label>
132
						<DescriptionInput
133
							defaultValue={typeof watch('description') === 'string' ? parse(watch('description')) : ''}
134
							name='description'
135
							onChange={setValue}
136
						/>
137
					</div>
138
					<div className="form-group">
139
						<label htmlFor="form-status">Estatus</label>
140
						<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
141
							<option selected={status === 'i'} value="i">Inactivo</option>
142
							<option selected={status === 'a'} value="a">Activo</option>
143
						</select>
144
					</div>
145
					<br />
146
					<div className="row">
147
						<div className="col-xs-12 col-md-12">
12135 stevensc 148
							<div className="panel-group" id="rows" >
149
								<div className="form-group" id="competencies-to-job" style={{}}>
150
									<div className="row">
151
										<div className="col-xs-12 col-md-12">
152
											<hr />
153
											<h4 style={{ fontSize: 18, fontWeight: 'bold' }}>Competencias asociadas al cargo:</h4>
154
											<br />
155
											<div className="panel-group" id="rows-job-competencies" >
156
												{
157
													competencyTypes.length > 0
158
													&&
159
													competenciesSelected.map((competency) => {
160
														const type = competencyTypes.find((type) => type.competency_type_id === competency.competency_type_id)
12083 stevensc 161
 
12135 stevensc 162
														return (
163
															<div key={competency.competency_id} className="panel panel-default" id={`panel-${competency.competency_id}`}>
164
																<div className="panel-heading">
165
																	<h4 className="panel-title" style={{ fontSize: 18 }}>
166
																		<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`#panel-${competency.competency_id}`} href={`#collapse-${competency.competency_id}`}>
167
																			<span className={`competency-name${competency.competency_id}`}>
168
																				{`${type.name} - ${competency.name}`}
169
																			</span>
170
																		</a>
171
																	</h4>
172
																</div>
173
																<div id={`collapse-${competency.competency_id}`} className="panel-collapse in collapse show">
174
																	<div className="panel-body">
175
																		<div className="table-responsive">
176
																			<table className="table table-bordered">
177
																				<thead>
178
																					<tr>
179
																						<th style={{ width: '80%' }}>Conducta Observable</th>
180
																						<th style={{ width: '20%' }}>Nivel</th>
181
																					</tr>
182
																				</thead>
183
																				<tbody>
184
																					{
185
																						competency.behaviors?.map(({ behavior_id, description, level }) => (
186
																							<tr key={behavior_id}>
187
																								<td className="text-left">
188
																									{description}
189
																								</td>
190
																								<td>
191
																									{levelOptions[level]}
192
																								</td>
193
																							</tr>
194
																						))
195
																					}
196
																				</tbody>
197
																			</table>
198
																		</div>
199
																	</div>
200
																</div>
12081 stevensc 201
															</div>
12135 stevensc 202
														)
203
													})
204
												}
205
											</div>
206
										</div>
207
									</div>
12040 stevensc 208
								</div>
209
							</div>
210
						</div>
12096 stevensc 211
					</div>
12104 stevensc 212
					<div className="d-flex" style={{ gap: '5px' }}>
12096 stevensc 213
						<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
214
						<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
215
						<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
216
					</div>
12040 stevensc 217
				</div>
12081 stevensc 218
			</div >
219
		</section >
12040 stevensc 220
 
12012 stevensc 221
	)
222
}
223
 
224
export default EditView