Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15067 | Rev 15069 | 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'
15066 stevensc 6
import { useHistory, useParams } 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
 
15066 stevensc 19
const EditView = ({ actionLink, jobsDescription }) => {
12040 stevensc 20
 
12096 stevensc 21
	// Hooks
22
	const history = useHistory()
23
	const { register, setValue, watch, reset } = useForm()
24
	const dispatch = useDispatch()
15066 stevensc 25
	const { action } = useParams()
12096 stevensc 26
 
27
	// State
12047 stevensc 28
	const [currentJobDescription, setCurretJobDescription] = useState('')
15066 stevensc 29
	const [jobDescriptionOptions, setJobDescriptionOptions] = useState(jobsDescription)
12081 stevensc 30
	const [competencyTypes, setCompetencyTypes] = useState([])
31
	const [competenciesSelected, setCompetenciesSelected] = useState([])
12047 stevensc 32
	const [status, setStatus] = useState('a')
12040 stevensc 33
 
12096 stevensc 34
	const onSubmit = () => {
12101 stevensc 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(() => {
15066 stevensc 80
		if (action === 'edit') {
81
			axios.get(actionLink)
82
				.then(({ data }) => {
83
					if (!data.success) {
84
						return dispatch(addNotification({
85
							style: 'danger',
86
							msg: 'Ha ocurrido un error'
87
						}))
88
					}
12040 stevensc 89
 
15066 stevensc 90
					setValue('name', data.data.name)
91
					setValue('description', data.data.description)
92
					setCurretJobDescription(data.data.job_description_id)
93
					setStatus(data.data.status)
94
				})
95
		}
12040 stevensc 96
	}, [actionLink])
97
 
98
	useEffect(() => {
15067 stevensc 99
		if (currentJobDescription) {
100
			axios.get(`/settings/jobs-description/edit/${currentJobDescription}`)
101
				.then(({ data }) => {
102
					if (!data.success) {
103
						return dispatch(addNotification({
104
							style: 'danger',
105
							msg: 'Ha ocurrido un error'
106
						}))
107
					}
12040 stevensc 108
 
15068 stevensc 109
					setJobDescriptionOptions([...data.data.jobs_description, { uuid: data.data.uuid, name: data.data.name }])
15067 stevensc 110
					setCompetenciesSelected(data.data.competencies_selected)
111
					setCompetencyTypes(data.data.competency_types)
112
				})
113
		}
12040 stevensc 114
	}, [currentJobDescription])
115
 
12012 stevensc 116
	return (
12040 stevensc 117
		<section className="content">
118
			<div className="row" style={{ padding: 16 }}>
119
				<div className="col-xs-12 col-md-12">
12096 stevensc 120
					<div className="form-group">
121
						<label>Nombre</label>
122
						<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
123
					</div>
124
					<div className="form-group">
125
						<label>Cargo a evaluar</label>
126
						<select name="job_description_id" className="form-control" onChange={(e) => setCurretJobDescription(e.target.value)}>
15066 stevensc 127
							<option value=''>Seleccione</option>
12096 stevensc 128
							{
15068 stevensc 129
								jobDescriptionOptions.map(({ name, uuid }) => (
130
									<option key={uuid} value={uuid}>{name}</option>
12096 stevensc 131
								))
132
							}
133
						</select>
134
					</div>
135
					<div className="form-group">
136
						<label htmlFor="form-description">Descripción</label>
137
						<DescriptionInput
138
							defaultValue={typeof watch('description') === 'string' ? parse(watch('description')) : ''}
139
							name='description'
140
							onChange={setValue}
141
						/>
142
					</div>
143
					<div className="form-group">
144
						<label htmlFor="form-status">Estatus</label>
145
						<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
146
							<option selected={status === 'i'} value="i">Inactivo</option>
147
							<option selected={status === 'a'} value="a">Activo</option>
148
						</select>
149
					</div>
150
					<br />
151
					<div className="row">
152
						<div className="col-xs-12 col-md-12">
12135 stevensc 153
							<div className="panel-group" id="rows" >
154
								<div className="form-group" id="competencies-to-job" style={{}}>
155
									<div className="row">
156
										<div className="col-xs-12 col-md-12">
157
											<hr />
158
											<h4 style={{ fontSize: 18, fontWeight: 'bold' }}>Competencias asociadas al cargo:</h4>
159
											<br />
160
											<div className="panel-group" id="rows-job-competencies" >
161
												{
162
													competencyTypes.length > 0
163
													&&
164
													competenciesSelected.map((competency) => {
165
														const type = competencyTypes.find((type) => type.competency_type_id === competency.competency_type_id)
12083 stevensc 166
 
12135 stevensc 167
														return (
168
															<div key={competency.competency_id} className="panel panel-default" id={`panel-${competency.competency_id}`}>
169
																<div className="panel-heading">
170
																	<h4 className="panel-title" style={{ fontSize: 18 }}>
171
																		<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`#panel-${competency.competency_id}`} href={`#collapse-${competency.competency_id}`}>
172
																			<span className={`competency-name${competency.competency_id}`}>
173
																				{`${type.name} - ${competency.name}`}
174
																			</span>
175
																		</a>
176
																	</h4>
177
																</div>
178
																<div id={`collapse-${competency.competency_id}`} className="panel-collapse in collapse show">
179
																	<div className="panel-body">
180
																		<div className="table-responsive">
181
																			<table className="table table-bordered">
182
																				<thead>
183
																					<tr>
184
																						<th style={{ width: '80%' }}>Conducta Observable</th>
185
																						<th style={{ width: '20%' }}>Nivel</th>
186
																					</tr>
187
																				</thead>
188
																				<tbody>
189
																					{
190
																						competency.behaviors?.map(({ behavior_id, description, level }) => (
191
																							<tr key={behavior_id}>
192
																								<td className="text-left">
193
																									{description}
194
																								</td>
195
																								<td>
196
																									{levelOptions[level]}
197
																								</td>
198
																							</tr>
199
																						))
200
																					}
201
																				</tbody>
202
																			</table>
203
																		</div>
204
																	</div>
205
																</div>
12081 stevensc 206
															</div>
12135 stevensc 207
														)
208
													})
209
												}
210
											</div>
211
										</div>
212
									</div>
12040 stevensc 213
								</div>
214
							</div>
215
						</div>
12096 stevensc 216
					</div>
12104 stevensc 217
					<div className="d-flex" style={{ gap: '5px' }}>
12096 stevensc 218
						<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
219
						<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
220
						<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
221
					</div>
12040 stevensc 222
				</div>
12081 stevensc 223
			</div >
224
		</section >
12040 stevensc 225
 
12012 stevensc 226
	)
227
}
228
 
229
export default EditView