Proyectos de Subversion LeadersLinked - Backend

Rev

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