Proyectos de Subversion LeadersLinked - Backend

Rev

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