Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 13068 | Rev 13070 | 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'
12733 stevensc 4
import parse from 'html-react-parser'
12709 stevensc 5
import { useForm } from 'react-hook-form'
6
import { useDispatch } from 'react-redux'
12710 stevensc 7
import { useHistory, useParams } from 'react-router-dom'
12709 stevensc 8
import { addNotification } from '../../../redux/notification/notification.actions'
9
import DescriptionInput from '../../../shared/DescriptionInput'
10
import SectionModal from '../components/SectionModal'
12993 stevensc 11
import DeleteModal from '../../../shared/DeleteModal'
12996 stevensc 12
import QuestionModal from '../components/QuestionModal'
13068 stevensc 13
import OptionModal from '../components/OptionModal'
12709 stevensc 14
 
15
const sectionTypeOptions = {
16
	open: 'Abierto',
13007 stevensc 17
	simple: 'Simple',
18
	multiple: 'Multiple'
12709 stevensc 19
}
20
 
13067 stevensc 21
const INITIAL_OPTION = {
22
	slug_section: '',
23
	slug_question: '',
24
	slug_option: '',
25
	text: '',
26
	checked: false,
27
	position: 0
28
}
29
 
12709 stevensc 30
const INITIAL_SECTION = {
31
	slug_section: '',
32
	name: '',
33
	text: '',
34
	position: 0,
35
	questions: [],
36
	status: 0
37
}
38
 
39
const INITIAL_QUESTION = {
40
	slug_section: '',
41
	slug_question: '',
42
	text: '',
43
	type: '',
44
	position: 0,
45
	maxlength: '0',
46
	multiline: '0',
47
	range: '0',
48
	options: [],
49
	answer: ''
50
}
51
 
52
const FormView = ({ actionLink }) => {
53
 
54
	// Hooks
55
	const history = useHistory()
12710 stevensc 56
	const { action } = useParams()
12709 stevensc 57
	const dispatch = useDispatch()
58
	const { register, setValue, watch, reset } = useForm()
59
 
60
	// Section modal states
61
	const [isShowSection, setIsShowSectionModal] = useState(false)
62
	const [sectionSelected, setSectionSelected] = useState(INITIAL_SECTION)
63
	const [sectionType, setSectionType] = useState('add')
64
 
13067 stevensc 65
	// Question modal states
12709 stevensc 66
	const [isShowQuestion, setIsShowQuestionModal] = useState(false)
67
	const [questionSelected, setQuestionSelected] = useState(INITIAL_QUESTION)
68
	const [questionType, setQuestionType] = useState('add')
69
 
13067 stevensc 70
	// Option modal states
71
	const [isShowOption, setIsShowOptionModal] = useState(false)
72
	const [optionSelected, setOptionSelected] = useState(INITIAL_OPTION)
73
	const [optionType, setOptionType] = useState('add')
74
 
12709 stevensc 75
	const [content, setContent] = useState([])
76
	const [status, setStatus] = useState('A')
12993 stevensc 77
	const [showDeleteModal, setShowDeleteModal] = useState(false)
78
	const [deleteType, setDeleteType] = useState('section')
13007 stevensc 79
	const deleteSlugsOptions = {
80
		section: sectionSelected.slug_section,
13069 stevensc 81
		question: questionSelected.slug_question,
82
		option: optionSelected.slug_option
13007 stevensc 83
	}
12709 stevensc 84
 
13067 stevensc 85
	// Section methods
12709 stevensc 86
	const showSectionModal = (section = INITIAL_SECTION, type = 'add') => {
87
		setSectionSelected(section)
88
		setSectionType(type)
13007 stevensc 89
		setIsShowSectionModal(true)
12709 stevensc 90
	}
91
 
92
	const closeSectionModal = () => {
93
		setIsShowSectionModal(false)
94
		setSectionSelected(INITIAL_SECTION)
95
	}
96
 
12991 stevensc 97
	const addSection = (name, text) => {
12836 stevensc 98
		const uuid = new Date().getTime()
12991 stevensc 99
		let position = content.length
100
		console.log(name)
101
		console.log(text)
12836 stevensc 102
 
12990 stevensc 103
		setContent(prev => [...prev, {
12836 stevensc 104
			slug_section: `section${uuid}`,
105
			name: name,
106
			text: text,
107
			position: position,
108
			questions: [],
109
			status: 0
12990 stevensc 110
		}])
12709 stevensc 111
	}
112
 
12991 stevensc 113
	const editSection = (name, text, slug) => {
12992 stevensc 114
		setContent(current =>
115
			current.map(currentSection => {
116
				if (currentSection.slug_section === slug) {
117
					return { ...currentSection, name: name, text: text }
118
				}
12991 stevensc 119
 
12992 stevensc 120
				return currentSection
121
			})
122
		)
12709 stevensc 123
	}
124
 
13067 stevensc 125
	const deleteSection = (slug) => {
126
		setContent(current =>
127
			current.filter(currentSection => {
128
				return currentSection.slug_section !== slug
129
			}),
130
		)
131
	}
132
 
133
	// Question methods
12709 stevensc 134
	const showQuestionModal = (question = INITIAL_QUESTION, type = 'add') => {
135
		setIsShowQuestionModal(true)
136
		setQuestionType(type)
137
		setQuestionSelected(question)
138
	}
139
 
140
	const closeQuestionModal = () => {
141
		setIsShowQuestionModal(false)
142
		setQuestionSelected(INITIAL_QUESTION)
143
	}
144
 
145
	const addQuestion = (question) => {
12996 stevensc 146
		const uuid = new Date().getTime()
147
 
13004 stevensc 148
		setContent(current =>
149
			current.map(currentSection => {
150
				if (currentSection.slug_section === sectionSelected.slug_section) {
151
					return {
152
						...currentSection,
153
						questions: [
154
							...currentSection.questions,
155
							{
156
								...question,
157
								slug_question: `question${uuid}`,
158
								slug_section: sectionSelected.slug_section,
159
							}
160
						]
161
					}
162
				}
13002 stevensc 163
 
13004 stevensc 164
				return currentSection
165
			})
166
		)
12709 stevensc 167
	}
168
 
169
	const editQuestion = (question) => {
13006 stevensc 170
		setContent(current =>
171
			current.map(currentSection => {
172
				if (currentSection.slug_section === sectionSelected.slug_section) {
13001 stevensc 173
 
13006 stevensc 174
					const newQuestions = currentSection.questions.map((currentQuestion) => {
175
						if (currentQuestion.slug_question === question.slug_question) {
176
							return question
177
						}
12709 stevensc 178
 
13006 stevensc 179
						return currentQuestion
180
					})
181
 
182
					return { ...currentSection, questions: newQuestions }
183
				}
13007 stevensc 184
 
13006 stevensc 185
				return currentSection
186
			})
187
		)
12709 stevensc 188
	}
189
 
13067 stevensc 190
	const deleteQuestion = (slug) => {
191
		setContent(current =>
192
			current.map(currentSection => {
193
				if (currentSection.slug_section === sectionSelected.slug_section) {
194
					return {
195
						...currentSection,
196
						questions: currentSection.questions.filter((currentQuestion) => currentQuestion.slug_question !== slug)
197
					}
198
				}
199
				return currentSection
200
			}),
201
		)
202
	}
203
 
204
	// Option methods
205
	const showOptionModal = (option = INITIAL_OPTION, type = 'add') => {
206
		setIsShowOptionModal(true)
207
		setOptionType(type)
208
		setOptionSelected(option)
209
	}
210
 
13068 stevensc 211
	const closeOptionModal = () => {
212
		setIsShowOptionModal(false)
213
		setOptionSelected(INITIAL_QUESTION)
214
	}
215
 
13067 stevensc 216
	const addOption = (option) => {
217
		const uuid = new Date().getTime()
218
 
219
		setContent(current =>
220
			current.map(currentSection => {
221
				if (currentSection.slug_section === sectionSelected.slug_section) {
222
 
13069 stevensc 223
					return {
224
						...currentSection,
225
						questions: currentSection.questions.map(currentQuestion => {
226
							if (currentQuestion.slug_question === questionSelected.slug_question) {
227
 
228
								return {
229
									...currentQuestion,
230
									options: [
231
										...currentQuestion.options,
232
										{
233
											...option,
234
											slug_question: questionSelected.slug_question,
235
											slug_section: sectionSelected.slug_section,
236
											slug_option: `option${uuid}`
237
										}
238
									]
239
								}
13067 stevensc 240
							}
241
 
13069 stevensc 242
							return currentQuestion
243
						})
244
					}
13067 stevensc 245
				}
246
				return currentSection
247
			})
248
		)
249
	}
250
 
251
	const deleteOption = (slug) => {
252
		setContent(current =>
253
			current.map(currentSection => {
254
				if (currentSection.slug_section === sectionSelected.slug_section) {
255
 
13069 stevensc 256
					return {
257
						...currentSection,
258
						questions: currentSection.questions.map(question => {
13067 stevensc 259
 
13069 stevensc 260
							if (question.slug_question === questionSelected.slug_question) {
261
								return {
262
									...question,
263
									options: question.options.filter(option => option.slug_option !== slug)
264
								}
13067 stevensc 265
							}
266
 
13069 stevensc 267
							return question
268
						})
269
					}
13067 stevensc 270
				}
271
 
272
				return currentSection
273
			}),
274
		)
275
	}
276
 
277
	// General methods
278
	const deleteHandler = (type, slug) => {
279
		if (type === 'section') {
280
			return deleteSection(slug)
281
		}
282
		if (type === 'question') {
283
			return deleteQuestion(slug)
284
		}
285
		if (type === 'option') {
286
			return deleteOption(slug)
287
		}
288
	}
289
 
12709 stevensc 290
	const onSubmit = () => {
291
		const submitData = new FormData()
292
		submitData.append('name', watch('name'))
293
		submitData.append('description', watch('description'))
294
		submitData.append('text', watch('text'))
295
		submitData.append('status', status)
12791 stevensc 296
		submitData.append('content', JSON.stringify(content))
12709 stevensc 297
 
298
		axios.post(actionLink, submitData)
299
			.then(({ data }) => {
300
				if (!data.success) {
301
					return dispatch(addNotification({
302
						style: 'danger',
303
						msg: typeof data.data === 'string'
304
							? data.data
305
							: 'Ha ocurrido un error'
306
					}))
307
				}
308
 
309
				dispatch(addNotification({
310
					style: 'success',
311
					msg: data.data
312
				}))
313
			})
314
	}
315
 
316
	const submitAndClose = () => {
317
		onSubmit()
318
		reset()
319
		history.goBack()
320
	}
321
 
322
	useEffect(() => {
13008 stevensc 323
		register('text')
324
		register('description')
325
	}, [])
326
 
327
	useEffect(() => {
12726 stevensc 328
		if (action === 'edit') {
329
			axios.get(actionLink)
330
				.then(({ data }) => {
331
					if (!data.success) {
332
						return dispatch(addNotification({
333
							style: 'danger',
334
							msg: 'Ha ocurrido un error'
335
						}))
336
					}
12709 stevensc 337
 
13008 stevensc 338
					register('text')
339
					register('description')
340
 
341
					setContent(data.data.content)
342
					setStatus(data.data.status)
343
 
12726 stevensc 344
					setValue('name', data.data.name)
345
					setValue('description', data.data.description)
346
					setValue('text', data.data.description)
347
				})
348
		}
12709 stevensc 349
	}, [actionLink])
350
 
13008 stevensc 351
 
12709 stevensc 352
	return (
353
		<>
354
			<section className="content">
355
				<div className="row" style={{ padding: 16 }}>
356
					<div className="col-xs-12 col-md-12">
357
						<div className="form-group">
358
							<label>Nombre</label>
359
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
360
						</div>
361
						<div className="form-group">
362
							<label htmlFor="form-description">Descripción</label>
12856 stevensc 363
							<DescriptionInput
12733 stevensc 364
								defaultValue={watch('description') ? parse(watch('description')) : ''}
12709 stevensc 365
								name='description'
366
								onChange={setValue}
12856 stevensc 367
							/>
12709 stevensc 368
						</div>
369
						<div className="form-group">
370
							<label htmlFor="form-description">Texto</label>
12856 stevensc 371
							<DescriptionInput
12733 stevensc 372
								defaultValue={watch('text') ? parse(watch('text')) : ''}
12709 stevensc 373
								name='text'
374
								onChange={setValue}
12856 stevensc 375
							/>
12709 stevensc 376
						</div>
377
						<div className="form-group">
378
							<label htmlFor="form-status">Estatus</label>
379
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 380
								<option value="A">Activo</option>
381
								<option value="I">Inactivo</option>
12709 stevensc 382
							</select>
383
						</div>
384
						<br />
385
						<div className="row">
386
							<div className="col-xs-12 col-md-12">
387
								<div className="panel-group" id="rows" >
388
									<div className="form-group">
389
										<div className="row">
390
											<div className="col-xs-12 col-md-12">
391
												<hr />
12994 stevensc 392
												<div className="d-flex justify-content-end">
393
													<button className='btn btn-primary' onClick={() => showSectionModal()}>
394
														<i className="fa fa-plus" />
395
														Agregar sección
396
													</button>
397
												</div>
12709 stevensc 398
												<br />
399
												<div className="panel-group" id="rows-job-competencies" >
400
													{
401
														content.length > 0
12836 stevensc 402
														&&
403
														content.map((section) => {
12709 stevensc 404
 
12836 stevensc 405
															return (
406
																<div className="panel panel-default" key={section.slug_section}>
407
																	<div className="panel-heading">
408
																		<h4 className="panel-title">
409
																			<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
410
																				<span className={`section-name${section.slug_section}`}>
411
																					{section.name}
412
																				</span>
413
																			</a>
414
																		</h4>
415
																	</div>
416
																	<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
417
																		<div className="panel-body">
418
																			<div className="table-responsive">
419
																				<table className="table table-bordered">
420
																					<thead>
421
																						<tr>
422
																							<th style={{ width: '10%' }}>Elemento</th>
423
																							<th style={{ width: '50%' }}>Texto</th>
424
																							<th style={{ width: '10%' }}>Tipo</th>
425
																							<th style={{ width: '20%' }}>Acciones</th>
426
																						</tr>
427
																					</thead>
428
																					<tbody>
429
																						<tr className="tr-section">
430
																							<td className="text-left">Sección</td>
431
																							<td className="text-left">{section.name}</td>
432
																							<td />
433
																							<td>
434
																								<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
435
																									<i className="fa fa-edit" />
436
																									Editar Sección
437
																								</button>
12994 stevensc 438
																								<button className="btn btn-default" onClick={() => {
13007 stevensc 439
																									setSectionSelected(section)
440
																									setDeleteType('section')
12993 stevensc 441
																									setShowDeleteModal(true)
442
																								}}>
12836 stevensc 443
																									<i className="fa fa-ban" />
444
																									Borrar Sección
445
																								</button>
13003 stevensc 446
																								<button className="btn btn-default" onClick={() => {
13007 stevensc 447
																									setSectionSelected(section)
13003 stevensc 448
																									showQuestionModal()
449
																								}}>
12836 stevensc 450
																									<i className="fa fa-plus" />
451
																									Agregar  Pregunta
452
																								</button>
453
																							</td>
454
																						</tr>
455
																						{
456
																							section.questions.map((question) => (
13067 stevensc 457
																								<>
458
																									<tr key={question.slug_question} className="tr-question">
459
																										<td className="text-left">Pregunta</td>
460
																										<td className="text-left">
461
																											{parse(question.text)}
462
																										</td>
463
																										<td className="text-capitalize">
464
																											{sectionTypeOptions[question.type]}
465
																										</td>
466
																										<td>
467
																											<button className="btn btn-default" onClick={() => {
468
																												setSectionSelected(section)
469
																												showQuestionModal(question, 'edit')
470
																											}}>
471
																												<i className="fa fa-edit" /> Editar Pregunta
12996 stevensc 472
																											</button>
13067 stevensc 473
																											<button className="btn btn-default" onClick={() => {
474
																												setQuestionSelected(question)
475
																												setDeleteType('question')
476
																												setShowDeleteModal(true)
477
																											}}>
478
																												<i className="fa fa-ban" /> Borrar Pregunta
479
																											</button>
480
																											{
481
																												question.type !== 'open'
482
																												&&
13068 stevensc 483
																												<button className="btn btn-default" onClick={() => {
484
																													setSectionSelected(section)
485
																													setQuestionSelected(question)
486
																													showOptionModal()
487
																												}}>
13067 stevensc 488
																													<i className="fa fa-plus" /> Agregar opción
489
																												</button>
490
																											}
491
																										</td>
492
																									</tr>
493
																									{
494
																										question.options.map(option => (
495
																											<tr key={option.slug_question} className="tr-option">
13069 stevensc 496
																												<td className="text-left">Opción</td>
13067 stevensc 497
																												<td className="text-left">
498
																													{parse(option.text)}
499
																												</td>
500
																												<td />
501
																												<td>
502
																													<button className="btn btn-default" onClick={() => {
503
																														setSectionSelected(section)
504
																														setQuestionSelected(question)
505
																														showOptionModal(option, 'edit')
506
																													}}>
507
																														<i className="fa fa-edit" /> Editar opción
508
																													</button>
509
																													<button className="btn btn-default" onClick={() => {
510
																														setOptionSelected(option)
511
																														setQuestionSelected(question)
512
																														setDeleteType('option')
513
																														setShowDeleteModal(true)
514
																													}}>
515
																														<i className="fa fa-ban" /> Borrar opción
516
																													</button>
517
																												</td>
518
																											</tr>
519
																										))
520
																									}
521
																								</>
12836 stevensc 522
																							))
523
																						}
13067 stevensc 524
 
12836 stevensc 525
																					</tbody>
526
																				</table>
527
																			</div>
528
																		</div>
529
																	</div>
530
																</div>
531
															)
532
														})
12709 stevensc 533
													}
534
												</div>
535
											</div>
536
										</div>
537
									</div>
538
								</div>
539
							</div>
540
						</div>
541
						<div className="d-flex" style={{ gap: '5px' }}>
542
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
543
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
544
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
545
						</div>
546
					</div>
547
				</div >
548
			</section >
549
			<SectionModal
550
				show={isShowSection}
12836 stevensc 551
				sectionType={sectionType}
12709 stevensc 552
				section={sectionSelected}
553
				closeModal={closeSectionModal}
554
				onSubmit={sectionType === 'add' ? addSection : editSection}
555
			/>
12996 stevensc 556
			<QuestionModal
557
				show={isShowQuestion}
558
				questionType={questionType}
559
				question={questionSelected}
560
				closeModal={closeQuestionModal}
561
				onSubmit={questionType === 'add' ? addQuestion : editQuestion}
562
			/>
13067 stevensc 563
			<OptionModal
564
				show={isShowOption}
13068 stevensc 565
				optionType={optionType}
566
				option={optionSelected}
13067 stevensc 567
				closeModal={closeOptionModal}
568
				onSubmit={optionType === 'add' ? addOption : editQuestion}
569
			/>
12993 stevensc 570
			<DeleteModal
571
				isOpen={showDeleteModal}
572
				closeModal={() => setShowDeleteModal(false)}
13007 stevensc 573
				onComplete={() => deleteHandler(deleteType, deleteSlugsOptions[deleteType])}
12993 stevensc 574
				message="Registro eliminado"
575
			/>
12709 stevensc 576
		</>
577
	)
578
}
579
 
580
export default FormView