Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 13003 | Rev 13006 | 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'
12709 stevensc 13
 
14
const sectionTypeOptions = {
15
	open: 'Abierto',
16
	simple: 'Simple'
17
}
18
 
19
const INITIAL_SECTION = {
20
	slug_section: '',
21
	name: '',
22
	text: '',
23
	position: 0,
24
	questions: [],
25
	status: 0
26
}
27
 
28
const INITIAL_QUESTION = {
29
	slug_section: '',
30
	slug_question: '',
31
	text: '',
32
	type: '',
33
	position: 0,
34
	maxlength: '0',
35
	multiline: '0',
36
	range: '0',
37
	options: [],
38
	answer: ''
39
}
40
 
41
 
42
const FormView = ({ actionLink }) => {
43
 
44
	// Hooks
45
	const history = useHistory()
12710 stevensc 46
	const { action } = useParams()
12709 stevensc 47
	const dispatch = useDispatch()
48
	const { register, setValue, watch, reset } = useForm()
49
 
50
	// Section modal states
51
	const [isShowSection, setIsShowSectionModal] = useState(false)
52
	const [sectionSelected, setSectionSelected] = useState(INITIAL_SECTION)
53
	const [sectionType, setSectionType] = useState('add')
54
 
55
	// Section modal states
56
	const [isShowQuestion, setIsShowQuestionModal] = useState(false)
57
	const [questionSelected, setQuestionSelected] = useState(INITIAL_QUESTION)
58
	const [questionType, setQuestionType] = useState('add')
59
 
60
	const [content, setContent] = useState([])
61
	const [status, setStatus] = useState('A')
12993 stevensc 62
	const [showDeleteModal, setShowDeleteModal] = useState(false)
63
	const [deleteType, setDeleteType] = useState('section')
12709 stevensc 64
 
65
	const showSectionModal = (section = INITIAL_SECTION, type = 'add') => {
66
		setIsShowSectionModal(true)
67
		setSectionSelected(section)
68
		setSectionType(type)
69
	}
70
 
71
	const closeSectionModal = () => {
72
		setIsShowSectionModal(false)
73
		setSectionSelected(INITIAL_SECTION)
74
	}
75
 
12991 stevensc 76
	const addSection = (name, text) => {
12836 stevensc 77
		const uuid = new Date().getTime()
12991 stevensc 78
		let position = content.length
79
		console.log(name)
80
		console.log(text)
12836 stevensc 81
 
12990 stevensc 82
		setContent(prev => [...prev, {
12836 stevensc 83
			slug_section: `section${uuid}`,
84
			name: name,
85
			text: text,
86
			position: position,
87
			questions: [],
88
			status: 0
12990 stevensc 89
		}])
12709 stevensc 90
	}
91
 
12993 stevensc 92
	const deleteSection = (slug) => {
93
		setContent(current =>
94
			current.filter(currentSection => {
95
				return currentSection.slug_section !== slug
96
			}),
97
		)
98
	}
99
 
100
	const deleteHandler = (type, slug) => {
101
		if (type === 'section') {
102
			return deleteSection(slug)
103
		}
104
	}
105
 
12991 stevensc 106
	const editSection = (name, text, slug) => {
12992 stevensc 107
		setContent(current =>
108
			current.map(currentSection => {
109
				if (currentSection.slug_section === slug) {
110
					return { ...currentSection, name: name, text: text }
111
				}
12991 stevensc 112
 
12992 stevensc 113
				return currentSection
114
			})
115
		)
12709 stevensc 116
	}
117
 
118
	const showQuestionModal = (question = INITIAL_QUESTION, type = 'add') => {
119
		setIsShowQuestionModal(true)
120
		setQuestionType(type)
121
		setQuestionSelected(question)
122
	}
123
 
124
	const closeQuestionModal = () => {
125
		setIsShowQuestionModal(false)
126
		setQuestionSelected(INITIAL_QUESTION)
127
	}
128
 
129
	const addQuestion = (question) => {
12996 stevensc 130
		const uuid = new Date().getTime()
131
 
13004 stevensc 132
		setContent(current =>
133
			current.map(currentSection => {
134
				if (currentSection.slug_section === sectionSelected.slug_section) {
135
					return {
136
						...currentSection,
137
						questions: [
138
							...currentSection.questions,
139
							{
140
								...question,
141
								slug_question: `question${uuid}`,
142
								slug_section: sectionSelected.slug_section,
143
							}
144
						]
145
					}
146
				}
13002 stevensc 147
 
13004 stevensc 148
				return currentSection
149
			})
150
		)
12709 stevensc 151
	}
152
 
153
	const editQuestion = (question) => {
13001 stevensc 154
 
12709 stevensc 155
		setContent(prev => prev.map(prevSection => {
156
			if (prevSection.slug_section === question.slug_section) {
157
				const questions = prevSection.questions
158
 
159
				return questions.map(prevQuestion => prevQuestion.slug_question === question.slug_question && question)
160
			}
161
		}))
162
	}
163
 
164
	const onSubmit = () => {
165
		const submitData = new FormData()
166
		submitData.append('name', watch('name'))
167
		submitData.append('description', watch('description'))
168
		submitData.append('text', watch('text'))
169
		submitData.append('status', status)
12791 stevensc 170
		submitData.append('content', JSON.stringify(content))
12709 stevensc 171
 
172
		axios.post(actionLink, submitData)
173
			.then(({ data }) => {
174
				if (!data.success) {
175
					return dispatch(addNotification({
176
						style: 'danger',
177
						msg: typeof data.data === 'string'
178
							? data.data
179
							: 'Ha ocurrido un error'
180
					}))
181
				}
182
 
183
				dispatch(addNotification({
184
					style: 'success',
185
					msg: data.data
186
				}))
187
			})
188
	}
189
 
190
	const submitAndClose = () => {
191
		onSubmit()
192
		reset()
193
		history.goBack()
194
	}
195
 
196
	useEffect(() => {
12726 stevensc 197
		if (action === 'edit') {
198
			axios.get(actionLink)
199
				.then(({ data }) => {
200
					if (!data.success) {
201
						return dispatch(addNotification({
202
							style: 'danger',
203
							msg: 'Ha ocurrido un error'
204
						}))
205
					}
12709 stevensc 206
 
12726 stevensc 207
					setValue('name', data.data.name)
208
					setValue('description', data.data.description)
209
					setValue('text', data.data.description)
210
					setContent(data.data.content)
211
					setStatus(data.data.status)
212
				})
213
		}
12709 stevensc 214
	}, [actionLink])
215
 
216
	return (
217
		<>
218
			<section className="content">
219
				<div className="row" style={{ padding: 16 }}>
220
					<div className="col-xs-12 col-md-12">
221
						<div className="form-group">
222
							<label>Nombre</label>
223
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
224
						</div>
225
						<div className="form-group">
226
							<label htmlFor="form-description">Descripción</label>
12856 stevensc 227
							<DescriptionInput
12733 stevensc 228
								defaultValue={watch('description') ? parse(watch('description')) : ''}
12709 stevensc 229
								name='description'
230
								onChange={setValue}
12856 stevensc 231
							/>
12709 stevensc 232
						</div>
233
						<div className="form-group">
234
							<label htmlFor="form-description">Texto</label>
12856 stevensc 235
							<DescriptionInput
12733 stevensc 236
								defaultValue={watch('text') ? parse(watch('text')) : ''}
12709 stevensc 237
								name='text'
238
								onChange={setValue}
12856 stevensc 239
							/>
12709 stevensc 240
						</div>
241
						<div className="form-group">
242
							<label htmlFor="form-status">Estatus</label>
243
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 244
								<option value="A">Activo</option>
245
								<option value="I">Inactivo</option>
12709 stevensc 246
							</select>
247
						</div>
248
						<br />
249
						<div className="row">
250
							<div className="col-xs-12 col-md-12">
251
								<div className="panel-group" id="rows" >
252
									<div className="form-group">
253
										<div className="row">
254
											<div className="col-xs-12 col-md-12">
255
												<hr />
12994 stevensc 256
												<div className="d-flex justify-content-end">
257
													<button className='btn btn-primary' onClick={() => showSectionModal()}>
258
														<i className="fa fa-plus" />
259
														Agregar sección
260
													</button>
261
												</div>
12709 stevensc 262
												<br />
263
												<div className="panel-group" id="rows-job-competencies" >
264
													{
265
														content.length > 0
12836 stevensc 266
														&&
267
														content.map((section) => {
12709 stevensc 268
 
12836 stevensc 269
															return (
270
																<div className="panel panel-default" key={section.slug_section}>
271
																	<div className="panel-heading">
272
																		<h4 className="panel-title">
273
																			<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
274
																				<span className={`section-name${section.slug_section}`}>
275
																					{section.name}
276
																				</span>
277
																			</a>
278
																		</h4>
279
																	</div>
280
																	<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
281
																		<div className="panel-body">
282
																			<div className="table-responsive">
283
																				<table className="table table-bordered">
284
																					<thead>
285
																						<tr>
286
																							<th style={{ width: '10%' }}>Elemento</th>
287
																							<th style={{ width: '50%' }}>Texto</th>
288
																							<th style={{ width: '10%' }}>Tipo</th>
289
																							<th style={{ width: '20%' }}>Acciones</th>
290
																						</tr>
291
																					</thead>
292
																					<tbody>
293
																						<tr className="tr-section">
294
																							<td className="text-left">Sección</td>
295
																							<td className="text-left">{section.name}</td>
296
																							<td />
297
																							<td>
298
																								<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
299
																									<i className="fa fa-edit" />
300
																									Editar Sección
301
																								</button>
12994 stevensc 302
																								<button className="btn btn-default" onClick={() => {
12993 stevensc 303
																									setShowDeleteModal(true)
304
																									setDeleteType('section')
12995 stevensc 305
																									setSectionSelected(section)
12993 stevensc 306
																								}}>
12836 stevensc 307
																									<i className="fa fa-ban" />
308
																									Borrar Sección
309
																								</button>
13003 stevensc 310
																								<button className="btn btn-default" onClick={() => {
311
																									showQuestionModal()
312
																									setSectionSelected(section)
313
																								}}>
12836 stevensc 314
																									<i className="fa fa-plus" />
315
																									Agregar  Pregunta
316
																								</button>
317
																							</td>
318
																						</tr>
319
																						{
320
																							section.questions.map((question) => (
321
																								<tr key={question.slug_question} className="tr-question">
322
																									<td className="text-left">Pregunta</td>
323
																									<td className="text-left">
324
																										{parse(question.text)}
325
																									</td>
326
																									<td className="text-capitalize">
327
																										{sectionTypeOptions[question.type]}
328
																									</td>
329
																									<td>
13002 stevensc 330
																										<button className="btn btn-default btn-edit-question" onClick={() => {
331
																											showQuestionModal(question, 'edit')
332
																											setSectionSelected(section)
333
																										}}>
12836 stevensc 334
																											<i className="fa fa-edit" /> Editar Pregunta
335
																										</button>
12996 stevensc 336
																										<button className="btn btn-default btn-delete-question" onClick={() => {
13002 stevensc 337
																											setQuestionSelected(question)
338
																											setDeleteType('question')
12996 stevensc 339
																											setShowDeleteModal(true)
340
																										}}>
12836 stevensc 341
																											<i className="fa fa-ban" /> Borrar Pregunta
342
																										</button>
12996 stevensc 343
																										{
344
																											question.type !== 'open'
345
																											&&
346
																											<button className="btn btn-default btn-delete-question">
347
																												<i className="fa fa-plus" /> Agregar opción
348
																											</button>
349
																										}
12836 stevensc 350
																									</td>
351
																								</tr>
352
																							))
353
																						}
354
																					</tbody>
355
																				</table>
356
																			</div>
357
																		</div>
358
																	</div>
359
																</div>
360
															)
361
														})
12709 stevensc 362
													}
363
												</div>
364
											</div>
365
										</div>
366
									</div>
367
								</div>
368
							</div>
369
						</div>
370
						<div className="d-flex" style={{ gap: '5px' }}>
371
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
372
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
373
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
374
						</div>
375
					</div>
376
				</div >
377
			</section >
378
			<SectionModal
379
				show={isShowSection}
12836 stevensc 380
				sectionType={sectionType}
12709 stevensc 381
				section={sectionSelected}
382
				closeModal={closeSectionModal}
383
				onSubmit={sectionType === 'add' ? addSection : editSection}
384
			/>
12996 stevensc 385
			<QuestionModal
386
				show={isShowQuestion}
387
				questionType={questionType}
388
				question={questionSelected}
389
				closeModal={closeQuestionModal}
390
				onSubmit={questionType === 'add' ? addQuestion : editQuestion}
391
			/>
12993 stevensc 392
			<DeleteModal
393
				isOpen={showDeleteModal}
394
				closeModal={() => setShowDeleteModal(false)}
395
				onComplete={() => deleteHandler(deleteType, sectionSelected.slug_section)}
396
				message="Registro eliminado"
397
			/>
12709 stevensc 398
		</>
399
	)
400
}
401
 
402
export default FormView