Proyectos de Subversion LeadersLinked - Backend

Rev

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