Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
12709 stevensc 1
/* eslint-disable no-mixed-spaces-and-tabs */
2
import React, { useState, useEffect } from 'react'
3
import axios from 'axios'
4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
12710 stevensc 6
import { useHistory, useParams } from 'react-router-dom'
12709 stevensc 7
import { addNotification } from '../../../redux/notification/notification.actions'
8
import DescriptionInput from '../../../shared/DescriptionInput'
9
import SectionModal from '../components/SectionModal'
10
 
11
const sectionTypeOptions = {
12
	open: 'Abierto',
13
	simple: 'Simple'
14
}
15
 
16
const INITIAL_SECTION = {
17
	slug_section: '',
18
	name: '',
19
	text: '',
20
	position: 0,
21
	questions: [],
22
	status: 0
23
}
24
 
25
const INITIAL_QUESTION = {
26
	slug_section: '',
27
	slug_question: '',
28
	text: '',
29
	type: '',
30
	position: 0,
31
	maxlength: '0',
32
	multiline: '0',
33
	range: '0',
34
	options: [],
35
	answer: ''
36
}
37
 
38
 
39
const FormView = ({ actionLink }) => {
40
 
41
	// Hooks
42
	const history = useHistory()
12710 stevensc 43
	const { action } = useParams()
12709 stevensc 44
	const dispatch = useDispatch()
45
	const { register, setValue, watch, reset } = useForm()
46
 
47
	// Section modal states
48
	const [isShowSection, setIsShowSectionModal] = useState(false)
49
	const [sectionSelected, setSectionSelected] = useState(INITIAL_SECTION)
50
	const [sectionType, setSectionType] = useState('add')
51
 
52
	// Section modal states
53
	const [isShowQuestion, setIsShowQuestionModal] = useState(false)
54
	const [questionSelected, setQuestionSelected] = useState(INITIAL_QUESTION)
55
	const [questionType, setQuestionType] = useState('add')
56
 
57
	const [content, setContent] = useState([])
58
	const [status, setStatus] = useState('A')
59
 
60
	const showSectionModal = (section = INITIAL_SECTION, type = 'add') => {
61
		setIsShowSectionModal(true)
62
		setSectionSelected(section)
63
		setSectionType(type)
64
	}
65
 
66
	const closeSectionModal = () => {
67
		setIsShowSectionModal(false)
68
		setSectionSelected(INITIAL_SECTION)
69
	}
70
 
71
	const addSection = (section) => {
72
		setContent(prev => [...prev, section])
73
	}
74
 
75
	const editSection = (section) => {
76
		setContent(prev => prev.map(prevSection => prevSection.slug_section === section.slug_section && section))
77
	}
78
 
79
	const showQuestionModal = (question = INITIAL_QUESTION, type = 'add') => {
80
		setIsShowQuestionModal(true)
81
		setQuestionType(type)
82
		setQuestionSelected(question)
83
	}
84
 
85
	const closeQuestionModal = () => {
86
		setIsShowQuestionModal(false)
87
		setQuestionSelected(INITIAL_QUESTION)
88
	}
89
 
90
	const addQuestion = (question) => {
91
		setContent(prev => prev.map(prevSection => prevSection.slug_section === question.slug_section && prev.questions.push(question)))
92
	}
93
 
94
	const editQuestion = (question) => {
95
		setContent(prev => prev.map(prevSection => {
96
			if (prevSection.slug_section === question.slug_section) {
97
				const questions = prevSection.questions
98
 
99
				return questions.map(prevQuestion => prevQuestion.slug_question === question.slug_question && question)
100
			}
101
		}))
102
	}
103
 
104
	const onSubmit = () => {
105
		const submitData = new FormData()
106
		submitData.append('name', watch('name'))
107
		submitData.append('description', watch('description'))
108
		submitData.append('text', watch('text'))
109
		submitData.append('status', status)
110
		submitData.append('content', content)
111
 
112
		axios.post(actionLink, submitData)
113
			.then(({ data }) => {
114
				if (!data.success) {
115
					return dispatch(addNotification({
116
						style: 'danger',
117
						msg: typeof data.data === 'string'
118
							? data.data
119
							: 'Ha ocurrido un error'
120
					}))
121
				}
122
 
123
				dispatch(addNotification({
124
					style: 'success',
125
					msg: data.data
126
				}))
127
			})
128
	}
129
 
130
	const submitAndClose = () => {
131
		onSubmit()
132
		reset()
133
		history.goBack()
134
	}
135
 
136
	useEffect(() => {
137
		register('description')
138
		register('text')
139
	}, [])
140
 
141
	useEffect(() => {
12726 stevensc 142
		if (action === 'edit') {
143
			axios.get(actionLink)
144
				.then(({ data }) => {
145
					if (!data.success) {
146
						return dispatch(addNotification({
147
							style: 'danger',
148
							msg: 'Ha ocurrido un error'
149
						}))
150
					}
12709 stevensc 151
 
12726 stevensc 152
					setValue('name', data.data.name)
153
					setValue('description', data.data.description)
154
					setValue('text', data.data.description)
155
					setContent(data.data.content)
156
					setStatus(data.data.status)
157
				})
158
		}
12709 stevensc 159
	}, [actionLink])
160
 
161
	return (
162
		<>
163
			<section className="content">
164
				<div className="row" style={{ padding: 16 }}>
165
					<div className="col-xs-12 col-md-12">
166
						<div className="form-group">
167
							<label>Nombre</label>
168
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
169
						</div>
170
						<div className="form-group">
171
							<label htmlFor="form-description">Descripción</label>
172
							<DescriptionInput
173
								name='description'
174
								onChange={setValue}
175
							/>
176
						</div>
177
						<div className="form-group">
178
							<label htmlFor="form-description">Texto</label>
179
							<DescriptionInput
180
								name='text'
181
								onChange={setValue}
182
							/>
183
						</div>
184
						<div className="form-group">
185
							<label htmlFor="form-status">Estatus</label>
186
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 187
								<option value="A">Activo</option>
188
								<option value="I">Inactivo</option>
12709 stevensc 189
							</select>
190
						</div>
191
						<br />
192
						<div className="row">
193
							<div className="col-xs-12 col-md-12">
194
								<div className="panel-group" id="rows" >
195
									<div className="form-group">
196
										<div className="row">
197
											<div className="col-xs-12 col-md-12">
198
												<hr />
199
												<h4 style={{ fontSize: 18, fontWeight: 'bold' }}>Competencias asociadas al cargo:</h4>
200
												<button className='btn btn-primary' onClick={showSectionModal}>
201
                                                    Agregar sección
202
												</button>
203
												<br />
204
												<div className="panel-group" id="rows-job-competencies" >
205
													{
206
														content.length > 0
207
                                                        &&
208
                                                        content.map((section) => {
209
 
210
                                                        	return (
211
                                                        		<div className="panel panel-default" key={section.slug_section}>
212
                                                        			<div className="panel-heading">
213
                                                        				<h4 className="panel-title">
214
                                                        					<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
215
                                                        						<span className={`section-name${section.slug_section}`}>
216
                                                        							{section.name}
217
                                                        						</span>
218
                                                        					</a>
219
                                                        				</h4>
220
                                                        			</div>
221
                                                        			<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
222
                                                        				<div className="panel-body">
223
                                                        					<div className="table-responsive">
224
                                                        						<table className="table table-bordered">
225
                                                        							<thead>
226
                                                        								<tr>
227
                                                        									<th style={{ width: '10%' }}>Elemento</th>
228
                                                        									<th style={{ width: '50%' }}>Texto</th>
229
                                                        									<th style={{ width: '10%' }}>Tipo</th>
230
                                                        									<th style={{ width: '20%' }}>Acciones</th>
231
                                                        								</tr>
232
                                                        							</thead>
233
                                                        							<tbody>
234
                                                        								<tr className="tr-section">
235
                                                        									<td className="text-left">Sección</td>
236
                                                        									<td className="text-left">{section.name}</td>
237
                                                        									<td />
238
                                                        									<td>
239
                                                        										<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
240
                                                        											<i className="fa fa-edit" />
241
                                                                                                    Editar Sección
242
                                                        										</button>
243
                                                        										<button className="btn btn-default" >
244
                                                        											<i className="fa fa-ban" />
245
                                                                                                    Borrar Sección
246
                                                        										</button>
247
                                                        										<button className="btn btn-default btn-add-question" >
248
                                                        											<i className="fa fa-plus" />
249
                                                                                                    Agregar  Pregunta
250
                                                        										</button>
251
                                                        									</td>
252
                                                        								</tr>
253
                                                        								{
254
                                                        									section.questions.map((question) => (
255
                                                        										<tr key={question.slug_question} className="tr-question">
256
                                                        											<td className="text-left">Pregunta</td>
257
                                                        											<td className="text-left">
12717 stevensc 258
                                                        												{question.text}
12709 stevensc 259
                                                        											</td>
260
                                                        											<td className="text-capitalize">
261
                                                        												{sectionTypeOptions[question.type]}
262
                                                        											</td>
263
                                                        											<td>
264
                                                        												<button className="btn btn-default btn-edit-question">
265
                                                        													<i className="fa fa-edit" /> Editar Pregunta
266
                                                        												</button>
267
                                                        												<button className="btn btn-default btn-delete-question">
268
                                                        													<i className="fa fa-ban" /> Borrar Pregunta
269
                                                        												</button>
270
                                                        											</td>
271
                                                        										</tr>
272
                                                        									))
273
                                                        								}
274
                                                        							</tbody>
275
                                                        						</table>
276
                                                        					</div>
277
                                                        				</div>
278
                                                        			</div>
279
                                                        		</div>
280
                                                        	)
281
                                                        })
282
													}
283
												</div>
284
											</div>
285
										</div>
286
									</div>
287
								</div>
288
							</div>
289
						</div>
290
						<div className="d-flex" style={{ gap: '5px' }}>
291
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
292
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
293
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
294
						</div>
295
					</div>
296
				</div >
297
			</section >
298
			<SectionModal
299
				show={isShowSection}
300
				section={sectionSelected}
301
				closeModal={closeSectionModal}
302
				onSubmit={sectionType === 'add' ? addSection : editSection}
303
			/>
304
		</>
305
	)
306
}
307
 
308
export default FormView