Proyectos de Subversion LeadersLinked - Backend

Rev

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