Proyectos de Subversion LeadersLinked - Backend

Rev

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