Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 15083 | Rev 15098 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
12289 stevensc 1
/* eslint-disable no-mixed-spaces-and-tabs */
2
import axios from 'axios'
3
import React, { useState, useEffect } from 'react'
4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
6
import { useHistory, useParams } from 'react-router-dom'
7
import { addNotification } from '../../../redux/notification/notification.actions'
15083 stevensc 8
import parse from 'html-react-parser'
12289 stevensc 9
 
10
const EvaluationView = ({ actionLink, setActionLink }) => {
11
 
12
	// Hooks
13
	const dispatch = useDispatch()
12291 stevensc 14
	const history = useHistory()
12289 stevensc 15
	const { action } = useParams()
16
	const { setValue, register, watch, handleSubmit } = useForm()
17
 
18
	//States
19
	const [jobDescription, setJobDescription] = useState({})
20
 
21
	const onSubmit = () => {
22
 
23
		const content = []
24
		jobDescription.competencies.forEach(competency => competency.behaviors.forEach(behavior => {
25
			content.push({
12301 stevensc 26
				competencyUuid: behavior.competency_uuid,
27
				behaviorUuid: behavior.uuid,
12289 stevensc 28
				comment: watch(`${behavior.competency_uuid}-${behavior.uuid}-comment`),
29
				evaluation: watch(`select-${behavior.competency_uuid}-${behavior.uuid}`)
30
			})
31
		}))
32
 
33
		const submitData = new FormData()
34
		submitData.append('content', JSON.stringify(content))
35
		submitData.append('points', watch('points'))
36
		submitData.append('comment', watch('comment'))
37
 
38
		axios.post(actionLink, submitData)
39
			.then(({ data }) => {
40
				if (!data.success) {
14843 stevensc 41
					typeof data.data === 'string'
42
						?
43
						dispatch(addNotification({
44
							style: 'danger',
45
							msg: data.data
46
						}))
47
						: Object.entries(data.data).map(([key, value]) =>
48
							value.map(err =>
49
								dispatch(addNotification({
50
									style: 'danger',
51
									msg: `${key}: ${err}`
52
								}))
53
							)
54
						)
55
					return
12289 stevensc 56
				}
57
 
58
				history.goBack()
59
				setActionLink('')
60
				dispatch(addNotification({
61
					style: 'success',
62
					msg: `Registro ${action === 'edit' ? 'actualizado' : 'añadido'}`
63
				}))
64
			})
65
	}
66
 
67
	useEffect(() => {
68
		axios.get(actionLink)
69
			.then(({ data }) => {
70
				const resData = data.data
71
 
72
				if (!data.success) {
73
					return dispatch(addNotification({
74
						style: 'danger',
75
						msg: 'Ha ocurrido un error'
76
					}))
77
				}
78
 
79
				setJobDescription(resData.job_description)
80
 
81
				if (action === 'both') {
82
					setValue('comment', resData.both.comment)
83
					setValue('points', resData.both.points)
84
				}
85
 
86
				if (action === 'self') {
87
					setValue('comment', resData.self.comment)
88
					setValue('points', resData.self.points)
89
				}
90
 
91
				if (action === 'superviser') {
92
					setValue('comment', resData.superviser.comment)
93
					setValue('points', resData.superviser.points)
94
				}
95
 
96
				resData.jobDescription.competencies.behaviors.forEach((obj) => {
97
					setValue(`select-${obj.competency_uuid}-${obj.uuid}`, obj.evaluation)
98
					setValue(`${obj.competency_uuid}-${obj.uuid}-comment`, obj.comment)
99
				})
100
			})
101
	}, [action])
102
 
103
	return (
104
		<section className="content">
105
			<div className="container-fluid">
106
				<div className="row">
107
					<div className="col-12">
108
						<form onSubmit={handleSubmit(onSubmit)}>
109
							<div className='card'>
110
								<div className="card-header">
111
									<ul className="nav nav-tabs" id="myTab" role="tablist">
112
										<li className="nav-item" role="presentation">
113
											<button className="nav-link active" id="home-tab" data-toggle="tab" data-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">General</button>
114
										</li>
115
										<li className="nav-item" role="presentation">
116
											<button className="nav-link" id="profile-tab" data-toggle="tab" data-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Competencias</button>
117
										</li>
118
										<li className="nav-item" role="presentation">
119
											<button className="nav-link" id="contact-tab" data-toggle="tab" data-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Conclusiones</button>
120
										</li>
121
									</ul>
122
								</div>
123
								<div className="card-body">
124
									<div className="tab-content" id="myTabContent">
125
										<div className="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
126
											<div className="card">
127
												<div className="card-body">
15083 stevensc 128
													<h5 className='mb-3 bold'>{jobDescription.name}</h5>
12289 stevensc 129
													<h6>Funciones</h6>
15083 stevensc 130
													<p>{parse(jobDescription.functions)}</p>
12289 stevensc 131
													<h6>Objetivos</h6>
15083 stevensc 132
													<p>{parse(jobDescription.objectives)}</p>
12289 stevensc 133
												</div>
134
											</div>
135
										</div>
136
										<div className="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
137
											{
12292 stevensc 138
												jobDescription.competencies?.length > 0
14843 stevensc 139
												&&
140
												jobDescription.competencies.map((competency) => (
141
													<div className="card" key={competency.competency_uuid}>
142
														<div className="card-header">
143
															<h5>{competency.competency_name} - {competency.competency_type_name}</h5>
144
														</div>
145
														<div className="card-body">
146
															<div className="table-responsive">
147
																{
148
																	competency.behaviors
149
																	&&
150
																	competency.behaviors.map((behavior) => (
151
																		<table key={behavior.uuid} className="table table-hover">
152
																			<thead>
153
																				<tr>
154
																					<th style={{ width: '20%' }}>Conducta Observable</th>
155
																					<th style={{ width: '60%' }}>Comentario</th>
156
																					<th style={{ width: '20%' }}>Evaluación</th>
157
																				</tr>
158
																			</thead>
159
																			<tbody>
160
																				<tr>
161
																					<td style={{ width: '20%' }}>{behavior.description}</td>
162
																					<td style={{ width: '60%' }}>
15083 stevensc 163
																						<input
14843 stevensc 164
																							name={`${behavior.competency_uuid}-${behavior.uuid}-comment`}
165
																							ref={register}
15090 efrain 166
																							ref={register}
15083 stevensc 167
																							className='form-control w-100' />
14843 stevensc 168
																					</td>
169
																					<td style={{ width: '20%' }}>
15083 stevensc 170
																						<input
171
																							type="number"
172
																							className='form-control'
173
																							name={`select-${behavior.competency_uuid}-${behavior.uuid}`}
174
																							ref={register} />
14843 stevensc 175
																					</td>
176
																				</tr>
177
																			</tbody>
178
																		</table>
179
																	))
180
																}
181
															</div>
182
														</div>
183
													</div>
184
												))
12289 stevensc 185
											}
186
										</div>
187
										<div className="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
188
											<div className="form-group">
189
												<label>Comentario</label>
15079 efrain 190
												<textarea type="text" name="comment" className="form-control" rows="5" cols="50" ref={register} />
12289 stevensc 191
											</div>
192
											<div className="form-group">
193
												<label>Evaluación</label>
15083 stevensc 194
												<input
195
													type="number"
196
													className='form-control'
197
													name='points'
198
													ref={register} />
12289 stevensc 199
											</div>
200
										</div>
201
									</div>
202
									<div className="form-group">
203
										<button type="submit" className="btn btn-primary btn-form-save-close mr-2">
15079 efrain 204
											Guardar
12289 stevensc 205
										</button>
206
										<button
207
											type="button"
208
											className="btn btn-secondary btn-edit-cancel"
209
											onClick={() => history.goBack()}
210
										>
14843 stevensc 211
											Cancelar
12289 stevensc 212
										</button>
213
									</div>
214
								</div>
215
							</div>
216
						</form>
217
					</div>
218
				</div>
219
			</div>
220
		</section >
221
	)
222
}
223
export default EvaluationView