Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15069 | Rev 15071 | 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
 
12096 stevensc 36
	const onSubmit = () => {
12101 stevensc 37
		const submitData = new FormData()
38
		submitData.append('name', watch('name'))
39
		submitData.append('description', watch('description'))
40
		submitData.append('status', status)
41
		submitData.append('job_description_id', currentJobDescription)
12104 stevensc 42
		submitData.append('content', '[]')
12101 stevensc 43
 
44
		axios.post(actionLink, submitData)
12096 stevensc 45
			.then(({ data }) => {
46
				if (!data.success) {
14616 stevensc 47
					typeof data.data === 'string'
48
						?
49
						dispatch(addNotification({
50
							style: 'danger',
51
							msg: data.data
52
						}))
53
						: Object.entries(data.data).map(([key, value]) =>
54
							value.map(err =>
55
								dispatch(addNotification({
56
									style: 'danger',
57
									msg: `${key}: ${err}`
58
								}))
59
							)
60
						)
61
					return
12096 stevensc 62
				}
63
 
64
				dispatch(addNotification({
65
					style: 'success',
66
					msg: data.data
67
				}))
68
			})
69
	}
70
 
71
	const submitAndClose = () => {
72
		onSubmit()
73
		reset()
74
		history.goBack()
75
	}
76
 
12040 stevensc 77
	useEffect(() => {
78
		register('description')
79
	}, [])
80
 
81
	useEffect(() => {
15066 stevensc 82
		if (action === 'edit') {
83
			axios.get(actionLink)
84
				.then(({ data }) => {
85
					if (!data.success) {
86
						return dispatch(addNotification({
87
							style: 'danger',
88
							msg: 'Ha ocurrido un error'
89
						}))
90
					}
12040 stevensc 91
 
15066 stevensc 92
					setValue('name', data.data.name)
93
					setValue('description', data.data.description)
15069 stevensc 94
					setStatus(data.data.status)
15066 stevensc 95
					setCurretJobDescription(data.data.job_description_id)
15069 stevensc 96
					setValue('jobs_description', data.data.job_description_id)
15066 stevensc 97
				})
98
		}
12040 stevensc 99
	}, [actionLink])
100
 
101
	useEffect(() => {
15070 stevensc 102
		axios.get(`/settings/jobs-description/edit/${currentJobDescription}`)
103
			.then(({ data }) => {
104
				if (!data.success) {
105
					return dispatch(addNotification({
106
						style: 'danger',
107
						msg: 'Ha ocurrido un error'
108
					}))
109
				}
110
				setJobDescriptionOptions(data.data.jobs_description?.map(description => ({ uuid: description.job_description_id, name: description.name })))
111
				setCompetenciesSelected(data.data.competencies_selected)
112
				setCompetencyTypes(data.data.competency_types)
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 }) => (
15070 stevensc 130
									<option selected={uuid === currentJobDescription} 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>
15070 stevensc 137
						<CKEditor
138
							onChange={(e) => setValue(name, e.editor.getData())}
139
							initData={typeof watch('description') === 'string' ? parse(watch('description')) : ''}
140
							config={config}
12096 stevensc 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