Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 13067 | Rev 13069 | 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
 
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,
82
		question: questionSelected.slug_question
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
 
223
					currentSection.questions.map(currentQuestion => {
224
						if (currentQuestion.slug_question === questionSelected.slug_question) {
225
							return {
226
								...currentQuestion,
227
								options: [
228
									...currentQuestion.options,
229
									{
230
										...option,
231
										slug_question: questionSelected.slug_question,
232
										slug_section: sectionSelected.slug_section,
233
										slug_option: `option${uuid}`
234
									}
235
								]
236
							}
237
						}
238
 
239
						return currentQuestion
240
					})
241
				}
242
 
243
				return currentSection
244
			})
245
		)
246
	}
247
 
248
	const deleteOption = (slug) => {
249
		setContent(current =>
250
			current.map(currentSection => {
251
				if (currentSection.slug_section === sectionSelected.slug_section) {
252
 
253
					currentSection.questions.map(question => {
254
 
255
						if (question.slug_question === questionSelected.slug_question) {
256
							return {
257
								...question,
258
								options: question.options.filter(option => option.slug_option !== slug)
259
							}
260
						}
261
 
262
						return question
263
					})
264
				}
265
 
266
				return currentSection
267
			}),
268
		)
269
	}
270
 
271
	// General methods
272
	const deleteHandler = (type, slug) => {
273
		if (type === 'section') {
274
			return deleteSection(slug)
275
		}
276
		if (type === 'question') {
277
			return deleteQuestion(slug)
278
		}
279
		if (type === 'option') {
280
			return deleteOption(slug)
281
		}
282
	}
283
 
12709 stevensc 284
	const onSubmit = () => {
285
		const submitData = new FormData()
286
		submitData.append('name', watch('name'))
287
		submitData.append('description', watch('description'))
288
		submitData.append('text', watch('text'))
289
		submitData.append('status', status)
12791 stevensc 290
		submitData.append('content', JSON.stringify(content))
12709 stevensc 291
 
292
		axios.post(actionLink, submitData)
293
			.then(({ data }) => {
294
				if (!data.success) {
295
					return dispatch(addNotification({
296
						style: 'danger',
297
						msg: typeof data.data === 'string'
298
							? data.data
299
							: 'Ha ocurrido un error'
300
					}))
301
				}
302
 
303
				dispatch(addNotification({
304
					style: 'success',
305
					msg: data.data
306
				}))
307
			})
308
	}
309
 
310
	const submitAndClose = () => {
311
		onSubmit()
312
		reset()
313
		history.goBack()
314
	}
315
 
316
	useEffect(() => {
13008 stevensc 317
		register('text')
318
		register('description')
319
	}, [])
320
 
321
	useEffect(() => {
12726 stevensc 322
		if (action === 'edit') {
323
			axios.get(actionLink)
324
				.then(({ data }) => {
325
					if (!data.success) {
326
						return dispatch(addNotification({
327
							style: 'danger',
328
							msg: 'Ha ocurrido un error'
329
						}))
330
					}
12709 stevensc 331
 
13008 stevensc 332
					register('text')
333
					register('description')
334
 
335
					setContent(data.data.content)
336
					setStatus(data.data.status)
337
 
12726 stevensc 338
					setValue('name', data.data.name)
339
					setValue('description', data.data.description)
340
					setValue('text', data.data.description)
341
				})
342
		}
12709 stevensc 343
	}, [actionLink])
344
 
13008 stevensc 345
 
12709 stevensc 346
	return (
347
		<>
348
			<section className="content">
349
				<div className="row" style={{ padding: 16 }}>
350
					<div className="col-xs-12 col-md-12">
351
						<div className="form-group">
352
							<label>Nombre</label>
353
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
354
						</div>
355
						<div className="form-group">
356
							<label htmlFor="form-description">Descripción</label>
12856 stevensc 357
							<DescriptionInput
12733 stevensc 358
								defaultValue={watch('description') ? parse(watch('description')) : ''}
12709 stevensc 359
								name='description'
360
								onChange={setValue}
12856 stevensc 361
							/>
12709 stevensc 362
						</div>
363
						<div className="form-group">
364
							<label htmlFor="form-description">Texto</label>
12856 stevensc 365
							<DescriptionInput
12733 stevensc 366
								defaultValue={watch('text') ? parse(watch('text')) : ''}
12709 stevensc 367
								name='text'
368
								onChange={setValue}
12856 stevensc 369
							/>
12709 stevensc 370
						</div>
371
						<div className="form-group">
372
							<label htmlFor="form-status">Estatus</label>
373
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 374
								<option value="A">Activo</option>
375
								<option value="I">Inactivo</option>
12709 stevensc 376
							</select>
377
						</div>
378
						<br />
379
						<div className="row">
380
							<div className="col-xs-12 col-md-12">
381
								<div className="panel-group" id="rows" >
382
									<div className="form-group">
383
										<div className="row">
384
											<div className="col-xs-12 col-md-12">
385
												<hr />
12994 stevensc 386
												<div className="d-flex justify-content-end">
387
													<button className='btn btn-primary' onClick={() => showSectionModal()}>
388
														<i className="fa fa-plus" />
389
														Agregar sección
390
													</button>
391
												</div>
12709 stevensc 392
												<br />
393
												<div className="panel-group" id="rows-job-competencies" >
394
													{
395
														content.length > 0
12836 stevensc 396
														&&
397
														content.map((section) => {
12709 stevensc 398
 
12836 stevensc 399
															return (
400
																<div className="panel panel-default" key={section.slug_section}>
401
																	<div className="panel-heading">
402
																		<h4 className="panel-title">
403
																			<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
404
																				<span className={`section-name${section.slug_section}`}>
405
																					{section.name}
406
																				</span>
407
																			</a>
408
																		</h4>
409
																	</div>
410
																	<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
411
																		<div className="panel-body">
412
																			<div className="table-responsive">
413
																				<table className="table table-bordered">
414
																					<thead>
415
																						<tr>
416
																							<th style={{ width: '10%' }}>Elemento</th>
417
																							<th style={{ width: '50%' }}>Texto</th>
418
																							<th style={{ width: '10%' }}>Tipo</th>
419
																							<th style={{ width: '20%' }}>Acciones</th>
420
																						</tr>
421
																					</thead>
422
																					<tbody>
423
																						<tr className="tr-section">
424
																							<td className="text-left">Sección</td>
425
																							<td className="text-left">{section.name}</td>
426
																							<td />
427
																							<td>
428
																								<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
429
																									<i className="fa fa-edit" />
430
																									Editar Sección
431
																								</button>
12994 stevensc 432
																								<button className="btn btn-default" onClick={() => {
13007 stevensc 433
																									setSectionSelected(section)
434
																									setDeleteType('section')
12993 stevensc 435
																									setShowDeleteModal(true)
436
																								}}>
12836 stevensc 437
																									<i className="fa fa-ban" />
438
																									Borrar Sección
439
																								</button>
13003 stevensc 440
																								<button className="btn btn-default" onClick={() => {
13007 stevensc 441
																									setSectionSelected(section)
13003 stevensc 442
																									showQuestionModal()
443
																								}}>
12836 stevensc 444
																									<i className="fa fa-plus" />
445
																									Agregar  Pregunta
446
																								</button>
447
																							</td>
448
																						</tr>
449
																						{
450
																							section.questions.map((question) => (
13067 stevensc 451
																								<>
452
																									<tr key={question.slug_question} className="tr-question">
453
																										<td className="text-left">Pregunta</td>
454
																										<td className="text-left">
455
																											{parse(question.text)}
456
																										</td>
457
																										<td className="text-capitalize">
458
																											{sectionTypeOptions[question.type]}
459
																										</td>
460
																										<td>
461
																											<button className="btn btn-default" onClick={() => {
462
																												setSectionSelected(section)
463
																												showQuestionModal(question, 'edit')
464
																											}}>
465
																												<i className="fa fa-edit" /> Editar Pregunta
12996 stevensc 466
																											</button>
13067 stevensc 467
																											<button className="btn btn-default" onClick={() => {
468
																												setQuestionSelected(question)
469
																												setDeleteType('question')
470
																												setShowDeleteModal(true)
471
																											}}>
472
																												<i className="fa fa-ban" /> Borrar Pregunta
473
																											</button>
474
																											{
475
																												question.type !== 'open'
476
																												&&
13068 stevensc 477
																												<button className="btn btn-default" onClick={() => {
478
																													setSectionSelected(section)
479
																													setQuestionSelected(question)
480
																													showOptionModal()
481
																												}}>
13067 stevensc 482
																													<i className="fa fa-plus" /> Agregar opción
483
																												</button>
484
																											}
485
																										</td>
486
																									</tr>
487
																									{
488
																										question.options.map(option => (
489
																											<tr key={option.slug_question} className="tr-option">
490
																												<td className="text-left">Pregunta</td>
491
																												<td className="text-left">
492
																													{parse(option.text)}
493
																												</td>
494
																												<td />
495
																												<td>
496
																													<button className="btn btn-default" onClick={() => {
497
																														setSectionSelected(section)
498
																														setQuestionSelected(question)
499
																														showOptionModal(option, 'edit')
500
																													}}>
501
																														<i className="fa fa-edit" /> Editar opción
502
																													</button>
503
																													<button className="btn btn-default" onClick={() => {
504
																														setOptionSelected(option)
505
																														setQuestionSelected(question)
506
																														setDeleteType('option')
507
																														setShowDeleteModal(true)
508
																													}}>
509
																														<i className="fa fa-ban" /> Borrar opción
510
																													</button>
511
																												</td>
512
																											</tr>
513
																										))
514
																									}
515
																								</>
12836 stevensc 516
																							))
517
																						}
13067 stevensc 518
 
12836 stevensc 519
																					</tbody>
520
																				</table>
521
																			</div>
522
																		</div>
523
																	</div>
524
																</div>
525
															)
526
														})
12709 stevensc 527
													}
528
												</div>
529
											</div>
530
										</div>
531
									</div>
532
								</div>
533
							</div>
534
						</div>
535
						<div className="d-flex" style={{ gap: '5px' }}>
536
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
537
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
538
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
539
						</div>
540
					</div>
541
				</div >
542
			</section >
543
			<SectionModal
544
				show={isShowSection}
12836 stevensc 545
				sectionType={sectionType}
12709 stevensc 546
				section={sectionSelected}
547
				closeModal={closeSectionModal}
548
				onSubmit={sectionType === 'add' ? addSection : editSection}
549
			/>
12996 stevensc 550
			<QuestionModal
551
				show={isShowQuestion}
552
				questionType={questionType}
553
				question={questionSelected}
554
				closeModal={closeQuestionModal}
555
				onSubmit={questionType === 'add' ? addQuestion : editQuestion}
556
			/>
13067 stevensc 557
			<OptionModal
558
				show={isShowOption}
13068 stevensc 559
				optionType={optionType}
560
				option={optionSelected}
13067 stevensc 561
				closeModal={closeOptionModal}
562
				onSubmit={optionType === 'add' ? addOption : editQuestion}
563
			/>
12993 stevensc 564
			<DeleteModal
565
				isOpen={showDeleteModal}
566
				closeModal={() => setShowDeleteModal(false)}
13007 stevensc 567
				onComplete={() => deleteHandler(deleteType, deleteSlugsOptions[deleteType])}
12993 stevensc 568
				message="Registro eliminado"
569
			/>
12709 stevensc 570
		</>
571
	)
572
}
573
 
574
export default FormView