Proyectos de Subversion LeadersLinked - Backend

Rev

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