Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

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