Proyectos de Subversion LeadersLinked - Backend

Rev

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