Proyectos de Subversion LeadersLinked - Backend

Rev

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