Proyectos de Subversion LeadersLinked - Backend

Rev

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