Proyectos de Subversion LeadersLinked - Backend

Rev

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