Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 13074 | Rev 13076 | 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)
13075 stevensc 325
		let questionsToOptions = []
13073 stevensc 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
 
13075 stevensc 336
		content.forEach(section => section.questions.map(question => {
337
			if (question.type !== 'open') {
338
				questionsToOptions = [...questionsToOptions, question]
339
			}
340
		}))
12709 stevensc 341
 
13074 stevensc 342
		console.log(questionsToOptions)
13073 stevensc 343
 
12709 stevensc 344
	}
345
 
13071 stevensc 346
	const onSubmit = async () => {
347
 
348
		const isValid = await validate()
349
 
350
		console.log(isValid)
351
	}
352
 
12709 stevensc 353
	const submitAndClose = () => {
354
		onSubmit()
355
		reset()
356
		history.goBack()
357
	}
358
 
359
	useEffect(() => {
13008 stevensc 360
		register('text')
361
		register('description')
362
	}, [])
363
 
364
	useEffect(() => {
12726 stevensc 365
		if (action === 'edit') {
366
			axios.get(actionLink)
367
				.then(({ data }) => {
368
					if (!data.success) {
369
						return dispatch(addNotification({
370
							style: 'danger',
371
							msg: 'Ha ocurrido un error'
372
						}))
373
					}
12709 stevensc 374
 
13008 stevensc 375
					register('text')
376
					register('description')
377
 
378
					setContent(data.data.content)
379
					setStatus(data.data.status)
380
 
12726 stevensc 381
					setValue('name', data.data.name)
382
					setValue('description', data.data.description)
383
					setValue('text', data.data.description)
384
				})
385
		}
12709 stevensc 386
	}, [actionLink])
387
 
388
	return (
389
		<>
390
			<section className="content">
391
				<div className="row" style={{ padding: 16 }}>
392
					<div className="col-xs-12 col-md-12">
393
						<div className="form-group">
394
							<label>Nombre</label>
395
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
396
						</div>
397
						<div className="form-group">
398
							<label htmlFor="form-description">Descripción</label>
12856 stevensc 399
							<DescriptionInput
13071 stevensc 400
								defaultValue={watch('description') && parse(watch('description', ''))}
12709 stevensc 401
								name='description'
402
								onChange={setValue}
12856 stevensc 403
							/>
12709 stevensc 404
						</div>
405
						<div className="form-group">
406
							<label htmlFor="form-description">Texto</label>
12856 stevensc 407
							<DescriptionInput
13071 stevensc 408
								defaultValue={watch('text') && parse(watch('text', ''))}
12709 stevensc 409
								name='text'
410
								onChange={setValue}
12856 stevensc 411
							/>
12709 stevensc 412
						</div>
413
						<div className="form-group">
414
							<label htmlFor="form-status">Estatus</label>
415
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 416
								<option value="A">Activo</option>
417
								<option value="I">Inactivo</option>
12709 stevensc 418
							</select>
419
						</div>
420
						<br />
421
						<div className="row">
422
							<div className="col-xs-12 col-md-12">
423
								<div className="panel-group" id="rows" >
424
									<div className="form-group">
425
										<div className="row">
426
											<div className="col-xs-12 col-md-12">
427
												<hr />
12994 stevensc 428
												<div className="d-flex justify-content-end">
429
													<button className='btn btn-primary' onClick={() => showSectionModal()}>
430
														<i className="fa fa-plus" />
431
														Agregar sección
432
													</button>
433
												</div>
12709 stevensc 434
												<br />
435
												<div className="panel-group" id="rows-job-competencies" >
436
													{
437
														content.length > 0
12836 stevensc 438
														&&
439
														content.map((section) => {
12709 stevensc 440
 
12836 stevensc 441
															return (
442
																<div className="panel panel-default" key={section.slug_section}>
443
																	<div className="panel-heading">
444
																		<h4 className="panel-title">
445
																			<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
446
																				<span className={`section-name${section.slug_section}`}>
447
																					{section.name}
448
																				</span>
449
																			</a>
450
																		</h4>
451
																	</div>
13071 stevensc 452
																	<div id={`collapse-section${section.slug_section}`} className="panel-collapse in collapse show">
12836 stevensc 453
																		<div className="panel-body">
454
																			<div className="table-responsive">
455
																				<table className="table table-bordered">
456
																					<thead>
457
																						<tr>
458
																							<th style={{ width: '10%' }}>Elemento</th>
459
																							<th style={{ width: '50%' }}>Texto</th>
460
																							<th style={{ width: '10%' }}>Tipo</th>
461
																							<th style={{ width: '20%' }}>Acciones</th>
462
																						</tr>
463
																					</thead>
464
																					<tbody>
465
																						<tr className="tr-section">
466
																							<td className="text-left">Sección</td>
467
																							<td className="text-left">{section.name}</td>
468
																							<td />
469
																							<td>
470
																								<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
471
																									<i className="fa fa-edit" />
472
																									Editar Sección
473
																								</button>
12994 stevensc 474
																								<button className="btn btn-default" onClick={() => {
13007 stevensc 475
																									setSectionSelected(section)
476
																									setDeleteType('section')
12993 stevensc 477
																									setShowDeleteModal(true)
478
																								}}>
12836 stevensc 479
																									<i className="fa fa-ban" />
480
																									Borrar Sección
481
																								</button>
13003 stevensc 482
																								<button className="btn btn-default" onClick={() => {
13007 stevensc 483
																									setSectionSelected(section)
13003 stevensc 484
																									showQuestionModal()
485
																								}}>
12836 stevensc 486
																									<i className="fa fa-plus" />
487
																									Agregar  Pregunta
488
																								</button>
489
																							</td>
490
																						</tr>
491
																						{
492
																							section.questions.map((question) => (
13067 stevensc 493
																								<>
494
																									<tr key={question.slug_question} className="tr-question">
495
																										<td className="text-left">Pregunta</td>
496
																										<td className="text-left">
497
																											{parse(question.text)}
498
																										</td>
499
																										<td className="text-capitalize">
500
																											{sectionTypeOptions[question.type]}
501
																										</td>
502
																										<td>
503
																											<button className="btn btn-default" onClick={() => {
504
																												setSectionSelected(section)
505
																												showQuestionModal(question, 'edit')
506
																											}}>
507
																												<i className="fa fa-edit" /> Editar Pregunta
12996 stevensc 508
																											</button>
13067 stevensc 509
																											<button className="btn btn-default" onClick={() => {
13070 stevensc 510
																												setSectionSelected(section)
13067 stevensc 511
																												setQuestionSelected(question)
512
																												setDeleteType('question')
513
																												setShowDeleteModal(true)
514
																											}}>
515
																												<i className="fa fa-ban" /> Borrar Pregunta
516
																											</button>
517
																											{
518
																												question.type !== 'open'
519
																												&&
13068 stevensc 520
																												<button className="btn btn-default" onClick={() => {
521
																													setSectionSelected(section)
522
																													setQuestionSelected(question)
523
																													showOptionModal()
524
																												}}>
13067 stevensc 525
																													<i className="fa fa-plus" /> Agregar opción
526
																												</button>
527
																											}
528
																										</td>
529
																									</tr>
530
																									{
531
																										question.options.map(option => (
532
																											<tr key={option.slug_question} className="tr-option">
13069 stevensc 533
																												<td className="text-left">Opción</td>
13067 stevensc 534
																												<td className="text-left">
535
																													{parse(option.text)}
536
																												</td>
537
																												<td />
538
																												<td>
539
																													<button className="btn btn-default" onClick={() => {
540
																														setSectionSelected(section)
541
																														setQuestionSelected(question)
542
																														showOptionModal(option, 'edit')
543
																													}}>
544
																														<i className="fa fa-edit" /> Editar opción
545
																													</button>
546
																													<button className="btn btn-default" onClick={() => {
547
																														setOptionSelected(option)
13070 stevensc 548
																														setSectionSelected(section)
13067 stevensc 549
																														setQuestionSelected(question)
550
																														setDeleteType('option')
551
																														setShowDeleteModal(true)
552
																													}}>
553
																														<i className="fa fa-ban" /> Borrar opción
554
																													</button>
555
																												</td>
556
																											</tr>
557
																										))
558
																									}
559
																								</>
12836 stevensc 560
																							))
561
																						}
13067 stevensc 562
 
12836 stevensc 563
																					</tbody>
564
																				</table>
565
																			</div>
566
																		</div>
567
																	</div>
568
																</div>
569
															)
570
														})
12709 stevensc 571
													}
572
												</div>
573
											</div>
574
										</div>
575
									</div>
576
								</div>
577
							</div>
578
						</div>
579
						<div className="d-flex" style={{ gap: '5px' }}>
580
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
581
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
582
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
583
						</div>
584
					</div>
585
				</div >
586
			</section >
587
			<SectionModal
588
				show={isShowSection}
12836 stevensc 589
				sectionType={sectionType}
12709 stevensc 590
				section={sectionSelected}
591
				closeModal={closeSectionModal}
592
				onSubmit={sectionType === 'add' ? addSection : editSection}
593
			/>
12996 stevensc 594
			<QuestionModal
595
				show={isShowQuestion}
596
				questionType={questionType}
597
				question={questionSelected}
598
				closeModal={closeQuestionModal}
599
				onSubmit={questionType === 'add' ? addQuestion : editQuestion}
600
			/>
13067 stevensc 601
			<OptionModal
602
				show={isShowOption}
13068 stevensc 603
				optionType={optionType}
604
				option={optionSelected}
13067 stevensc 605
				closeModal={closeOptionModal}
13070 stevensc 606
				onSubmit={optionType === 'add' ? addOption : editOption}
13067 stevensc 607
			/>
12993 stevensc 608
			<DeleteModal
609
				isOpen={showDeleteModal}
610
				closeModal={() => setShowDeleteModal(false)}
13007 stevensc 611
				onComplete={() => deleteHandler(deleteType, deleteSlugsOptions[deleteType])}
12993 stevensc 612
				message="Registro eliminado"
613
			/>
12709 stevensc 614
		</>
615
	)
616
}
617
 
618
export default FormView