Proyectos de Subversion LeadersLinked - Backend

Rev

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