Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12999 | Rev 13002 | 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) => {
13001 stevensc 130
		console.log(question)
131
		console.log(sectionSelected)
132
		console.log(content)
12996 stevensc 133
		const uuid = new Date().getTime()
134
 
135
		setContent(prev => prev.map(prevSection => {
136
			if (prevSection.slug_section === sectionSelected.slug_section) {
137
				return prevSection.questions.push({ ...question, slug_question: `question${uuid}` })
138
			}
139
		}))
12709 stevensc 140
	}
141
 
142
	const editQuestion = (question) => {
13001 stevensc 143
 
12709 stevensc 144
		setContent(prev => prev.map(prevSection => {
145
			if (prevSection.slug_section === question.slug_section) {
146
				const questions = prevSection.questions
147
 
148
				return questions.map(prevQuestion => prevQuestion.slug_question === question.slug_question && question)
149
			}
150
		}))
151
	}
152
 
153
	const onSubmit = () => {
154
		const submitData = new FormData()
155
		submitData.append('name', watch('name'))
156
		submitData.append('description', watch('description'))
157
		submitData.append('text', watch('text'))
158
		submitData.append('status', status)
12791 stevensc 159
		submitData.append('content', JSON.stringify(content))
12709 stevensc 160
 
161
		axios.post(actionLink, submitData)
162
			.then(({ data }) => {
163
				if (!data.success) {
164
					return dispatch(addNotification({
165
						style: 'danger',
166
						msg: typeof data.data === 'string'
167
							? data.data
168
							: 'Ha ocurrido un error'
169
					}))
170
				}
171
 
172
				dispatch(addNotification({
173
					style: 'success',
174
					msg: data.data
175
				}))
176
			})
177
	}
178
 
179
	const submitAndClose = () => {
180
		onSubmit()
181
		reset()
182
		history.goBack()
183
	}
184
 
185
	useEffect(() => {
12726 stevensc 186
		if (action === 'edit') {
187
			axios.get(actionLink)
188
				.then(({ data }) => {
189
					if (!data.success) {
190
						return dispatch(addNotification({
191
							style: 'danger',
192
							msg: 'Ha ocurrido un error'
193
						}))
194
					}
12709 stevensc 195
 
12726 stevensc 196
					setValue('name', data.data.name)
197
					setValue('description', data.data.description)
198
					setValue('text', data.data.description)
199
					setContent(data.data.content)
200
					setStatus(data.data.status)
201
				})
202
		}
12709 stevensc 203
	}, [actionLink])
204
 
205
	return (
206
		<>
207
			<section className="content">
208
				<div className="row" style={{ padding: 16 }}>
209
					<div className="col-xs-12 col-md-12">
210
						<div className="form-group">
211
							<label>Nombre</label>
212
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
213
						</div>
214
						<div className="form-group">
215
							<label htmlFor="form-description">Descripción</label>
12856 stevensc 216
							<DescriptionInput
12733 stevensc 217
								defaultValue={watch('description') ? parse(watch('description')) : ''}
12709 stevensc 218
								name='description'
219
								onChange={setValue}
12856 stevensc 220
							/>
12709 stevensc 221
						</div>
222
						<div className="form-group">
223
							<label htmlFor="form-description">Texto</label>
12856 stevensc 224
							<DescriptionInput
12733 stevensc 225
								defaultValue={watch('text') ? parse(watch('text')) : ''}
12709 stevensc 226
								name='text'
227
								onChange={setValue}
12856 stevensc 228
							/>
12709 stevensc 229
						</div>
230
						<div className="form-group">
231
							<label htmlFor="form-status">Estatus</label>
232
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 233
								<option value="A">Activo</option>
234
								<option value="I">Inactivo</option>
12709 stevensc 235
							</select>
236
						</div>
237
						<br />
238
						<div className="row">
239
							<div className="col-xs-12 col-md-12">
240
								<div className="panel-group" id="rows" >
241
									<div className="form-group">
242
										<div className="row">
243
											<div className="col-xs-12 col-md-12">
244
												<hr />
12994 stevensc 245
												<div className="d-flex justify-content-end">
246
													<button className='btn btn-primary' onClick={() => showSectionModal()}>
247
														<i className="fa fa-plus" />
248
														Agregar sección
249
													</button>
250
												</div>
12709 stevensc 251
												<br />
252
												<div className="panel-group" id="rows-job-competencies" >
253
													{
254
														content.length > 0
12836 stevensc 255
														&&
256
														content.map((section) => {
12709 stevensc 257
 
12836 stevensc 258
															return (
259
																<div className="panel panel-default" key={section.slug_section}>
260
																	<div className="panel-heading">
261
																		<h4 className="panel-title">
262
																			<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
263
																				<span className={`section-name${section.slug_section}`}>
264
																					{section.name}
265
																				</span>
266
																			</a>
267
																		</h4>
268
																	</div>
269
																	<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
270
																		<div className="panel-body">
271
																			<div className="table-responsive">
272
																				<table className="table table-bordered">
273
																					<thead>
274
																						<tr>
275
																							<th style={{ width: '10%' }}>Elemento</th>
276
																							<th style={{ width: '50%' }}>Texto</th>
277
																							<th style={{ width: '10%' }}>Tipo</th>
278
																							<th style={{ width: '20%' }}>Acciones</th>
279
																						</tr>
280
																					</thead>
281
																					<tbody>
282
																						<tr className="tr-section">
283
																							<td className="text-left">Sección</td>
284
																							<td className="text-left">{section.name}</td>
285
																							<td />
286
																							<td>
287
																								<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
288
																									<i className="fa fa-edit" />
289
																									Editar Sección
290
																								</button>
12994 stevensc 291
																								<button className="btn btn-default" onClick={() => {
12993 stevensc 292
																									setShowDeleteModal(true)
293
																									setDeleteType('section')
12995 stevensc 294
																									setSectionSelected(section)
12993 stevensc 295
																								}}>
12836 stevensc 296
																									<i className="fa fa-ban" />
297
																									Borrar Sección
298
																								</button>
12999 stevensc 299
																								<button className="btn btn-default" onClick={() => showQuestionModal()}>
12836 stevensc 300
																									<i className="fa fa-plus" />
301
																									Agregar  Pregunta
302
																								</button>
303
																							</td>
304
																						</tr>
305
																						{
306
																							section.questions.map((question) => (
307
																								<tr key={question.slug_question} className="tr-question">
308
																									<td className="text-left">Pregunta</td>
309
																									<td className="text-left">
310
																										{parse(question.text)}
311
																									</td>
312
																									<td className="text-capitalize">
313
																										{sectionTypeOptions[question.type]}
314
																									</td>
315
																									<td>
12996 stevensc 316
																										<button className="btn btn-default btn-edit-question" onClick={() => showQuestionModal(question, 'edit')}>
12836 stevensc 317
																											<i className="fa fa-edit" /> Editar Pregunta
318
																										</button>
12996 stevensc 319
																										<button className="btn btn-default btn-delete-question" onClick={() => {
320
																											setShowDeleteModal(true)
321
																											setDeleteType('question')
322
																											setQuestionSelected(question)
323
																											setSectionSelected(section)
324
																										}}>
12836 stevensc 325
																											<i className="fa fa-ban" /> Borrar Pregunta
326
																										</button>
12996 stevensc 327
																										{
328
																											question.type !== 'open'
329
																											&&
330
																											<button className="btn btn-default btn-delete-question">
331
																												<i className="fa fa-plus" /> Agregar opción
332
																											</button>
333
																										}
12836 stevensc 334
																									</td>
335
																								</tr>
336
																							))
337
																						}
338
																					</tbody>
339
																				</table>
340
																			</div>
341
																		</div>
342
																	</div>
343
																</div>
344
															)
345
														})
12709 stevensc 346
													}
347
												</div>
348
											</div>
349
										</div>
350
									</div>
351
								</div>
352
							</div>
353
						</div>
354
						<div className="d-flex" style={{ gap: '5px' }}>
355
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
356
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
357
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
358
						</div>
359
					</div>
360
				</div >
361
			</section >
362
			<SectionModal
363
				show={isShowSection}
12836 stevensc 364
				sectionType={sectionType}
12709 stevensc 365
				section={sectionSelected}
366
				closeModal={closeSectionModal}
367
				onSubmit={sectionType === 'add' ? addSection : editSection}
368
			/>
12996 stevensc 369
			<QuestionModal
370
				show={isShowQuestion}
371
				questionType={questionType}
372
				question={questionSelected}
373
				closeModal={closeQuestionModal}
374
				onSubmit={questionType === 'add' ? addQuestion : editQuestion}
375
			/>
12993 stevensc 376
			<DeleteModal
377
				isOpen={showDeleteModal}
378
				closeModal={() => setShowDeleteModal(false)}
379
				onComplete={() => deleteHandler(deleteType, sectionSelected.slug_section)}
380
				message="Registro eliminado"
381
			/>
12709 stevensc 382
		</>
383
	)
384
}
385
 
386
export default FormView