Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12096 | Rev 12104 | 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)
39
		submitData.append('content', [])
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">
136
							<div className="panel-group" id="rows" />
12040 stevensc 137
						</div>
12096 stevensc 138
					</div>
139
					<div className="form-group" id="competencies-to-job" style={{}}>
12040 stevensc 140
						<div className="row">
141
							<div className="col-xs-12 col-md-12">
12096 stevensc 142
								<hr />
143
								<h4 style={{ fontSize: 18, fontWeight: 'bold' }}>Competencias asociadas al cargo:</h4>
144
								<br />
145
								<div className="panel-group" id="rows-job-competencies" >
146
									{
147
										competencyTypes.length > 0
148
										&&
149
										competenciesSelected.map((competency) => {
150
											const type = competencyTypes.find((type) => type.competency_type_id === competency.competency_type_id)
12083 stevensc 151
 
12096 stevensc 152
											return (
153
												<div key={competency.competency_id} className="panel panel-default" id={`panel-${competency.competency_id}`}>
154
													<div className="panel-heading">
155
														<h4 className="panel-title" style={{ fontSize: 18 }}>
156
															<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`#panel-${competency.competency_id}`} href={`#collapse-${competency.competency_id}`}>
157
																<span className={`competency-name${competency.competency_id}`}>
158
																	{`${type.name} - ${competency.name}`}
159
																</span>
160
															</a>
161
														</h4>
162
													</div>
163
													<div id={`collapse-${competency.competency_id}`} className="panel-collapse in collapse show">
164
														<div className="panel-body">
165
															<div className="table-responsive">
166
																<table className="table table-bordered">
167
																	<thead>
168
																		<tr>
169
																			<th style={{ width: '80%' }}>Conducta Observable</th>
170
																			<th style={{ width: '20%' }}>Nivel</th>
171
																		</tr>
172
																	</thead>
173
																	<tbody>
174
																		{
175
																			competency.behaviors?.map(({ behavior_id, description, level }) => (
176
																				<tr key={behavior_id}>
177
																					<td className="text-left">
178
																						{description}
179
																					</td>
180
																					<td>
181
																						{levelOptions[level]}
182
																					</td>
183
																				</tr>
184
																			))
185
																		}
186
																	</tbody>
187
																</table>
12081 stevensc 188
															</div>
189
														</div>
12040 stevensc 190
													</div>
12096 stevensc 191
												</div>
192
											)
193
										})
194
									}
12040 stevensc 195
								</div>
196
							</div>
197
						</div>
12096 stevensc 198
					</div>
199
					<div className="form-group" style={{ gap: '10px' }}>
200
						<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
201
						<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
202
						<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
203
					</div>
12040 stevensc 204
				</div>
12081 stevensc 205
			</div >
206
		</section >
12040 stevensc 207
 
12012 stevensc 208
	)
209
}
210
 
211
export default EditView