Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12729 | Rev 12734 | 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
 
72
	const addSection = (section) => {
73
		setContent(prev => [...prev, section])
74
	}
75
 
76
	const editSection = (section) => {
77
		setContent(prev => prev.map(prevSection => prevSection.slug_section === section.slug_section && section))
78
	}
79
 
80
	const showQuestionModal = (question = INITIAL_QUESTION, type = 'add') => {
81
		setIsShowQuestionModal(true)
82
		setQuestionType(type)
83
		setQuestionSelected(question)
84
	}
85
 
86
	const closeQuestionModal = () => {
87
		setIsShowQuestionModal(false)
88
		setQuestionSelected(INITIAL_QUESTION)
89
	}
90
 
91
	const addQuestion = (question) => {
92
		setContent(prev => prev.map(prevSection => prevSection.slug_section === question.slug_section && prev.questions.push(question)))
93
	}
94
 
95
	const editQuestion = (question) => {
96
		setContent(prev => prev.map(prevSection => {
97
			if (prevSection.slug_section === question.slug_section) {
98
				const questions = prevSection.questions
99
 
100
				return questions.map(prevQuestion => prevQuestion.slug_question === question.slug_question && question)
101
			}
102
		}))
103
	}
104
 
105
	const onSubmit = () => {
106
		const submitData = new FormData()
107
		submitData.append('name', watch('name'))
108
		submitData.append('description', watch('description'))
109
		submitData.append('text', watch('text'))
110
		submitData.append('status', status)
111
		submitData.append('content', content)
112
 
113
		axios.post(actionLink, submitData)
114
			.then(({ data }) => {
115
				if (!data.success) {
116
					return dispatch(addNotification({
117
						style: 'danger',
118
						msg: typeof data.data === 'string'
119
							? data.data
120
							: 'Ha ocurrido un error'
121
					}))
122
				}
123
 
124
				dispatch(addNotification({
125
					style: 'success',
126
					msg: data.data
127
				}))
128
			})
129
	}
130
 
131
	const submitAndClose = () => {
132
		onSubmit()
133
		reset()
134
		history.goBack()
135
	}
136
 
137
	useEffect(() => {
138
		register('description')
139
		register('text')
140
	}, [])
141
 
142
	useEffect(() => {
12726 stevensc 143
		if (action === 'edit') {
144
			axios.get(actionLink)
145
				.then(({ data }) => {
146
					if (!data.success) {
147
						return dispatch(addNotification({
148
							style: 'danger',
149
							msg: 'Ha ocurrido un error'
150
						}))
151
					}
12709 stevensc 152
 
12726 stevensc 153
					setValue('name', data.data.name)
154
					setValue('description', data.data.description)
155
					setValue('text', data.data.description)
156
					setContent(data.data.content)
157
					setStatus(data.data.status)
158
				})
159
		}
12709 stevensc 160
	}, [actionLink])
161
 
162
	return (
163
		<>
164
			<section className="content">
165
				<div className="row" style={{ padding: 16 }}>
166
					<div className="col-xs-12 col-md-12">
167
						<div className="form-group">
168
							<label>Nombre</label>
169
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
170
						</div>
171
						<div className="form-group">
172
							<label htmlFor="form-description">Descripción</label>
173
							<DescriptionInput
12733 stevensc 174
								defaultValue={watch('description') ? parse(watch('description')) : ''}
12709 stevensc 175
								name='description'
176
								onChange={setValue}
177
							/>
178
						</div>
179
						<div className="form-group">
180
							<label htmlFor="form-description">Texto</label>
181
							<DescriptionInput
12733 stevensc 182
								defaultValue={watch('text') ? parse(watch('text')) : ''}
12709 stevensc 183
								name='text'
184
								onChange={setValue}
185
							/>
186
						</div>
187
						<div className="form-group">
188
							<label htmlFor="form-status">Estatus</label>
189
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 190
								<option value="A">Activo</option>
191
								<option value="I">Inactivo</option>
12709 stevensc 192
							</select>
193
						</div>
194
						<br />
195
						<div className="row">
196
							<div className="col-xs-12 col-md-12">
197
								<div className="panel-group" id="rows" >
198
									<div className="form-group">
199
										<div className="row">
200
											<div className="col-xs-12 col-md-12">
201
												<hr />
202
												<h4 style={{ fontSize: 18, fontWeight: 'bold' }}>Competencias asociadas al cargo:</h4>
203
												<button className='btn btn-primary' onClick={showSectionModal}>
204
                                                    Agregar sección
205
												</button>
206
												<br />
207
												<div className="panel-group" id="rows-job-competencies" >
208
													{
209
														content.length > 0
210
                                                        &&
211
                                                        content.map((section) => {
212
 
213
                                                        	return (
214
                                                        		<div className="panel panel-default" key={section.slug_section}>
215
                                                        			<div className="panel-heading">
216
                                                        				<h4 className="panel-title">
217
                                                        					<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
218
                                                        						<span className={`section-name${section.slug_section}`}>
219
                                                        							{section.name}
220
                                                        						</span>
221
                                                        					</a>
222
                                                        				</h4>
223
                                                        			</div>
224
                                                        			<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
225
                                                        				<div className="panel-body">
226
                                                        					<div className="table-responsive">
227
                                                        						<table className="table table-bordered">
228
                                                        							<thead>
229
                                                        								<tr>
230
                                                        									<th style={{ width: '10%' }}>Elemento</th>
231
                                                        									<th style={{ width: '50%' }}>Texto</th>
232
                                                        									<th style={{ width: '10%' }}>Tipo</th>
233
                                                        									<th style={{ width: '20%' }}>Acciones</th>
234
                                                        								</tr>
235
                                                        							</thead>
236
                                                        							<tbody>
237
                                                        								<tr className="tr-section">
238
                                                        									<td className="text-left">Sección</td>
239
                                                        									<td className="text-left">{section.name}</td>
240
                                                        									<td />
241
                                                        									<td>
242
                                                        										<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
243
                                                        											<i className="fa fa-edit" />
244
                                                                                                    Editar Sección
245
                                                        										</button>
246
                                                        										<button className="btn btn-default" >
247
                                                        											<i className="fa fa-ban" />
248
                                                                                                    Borrar Sección
249
                                                        										</button>
250
                                                        										<button className="btn btn-default btn-add-question" >
251
                                                        											<i className="fa fa-plus" />
252
                                                                                                    Agregar  Pregunta
253
                                                        										</button>
254
                                                        									</td>
255
                                                        								</tr>
256
                                                        								{
257
                                                        									section.questions.map((question) => (
258
                                                        										<tr key={question.slug_question} className="tr-question">
259
                                                        											<td className="text-left">Pregunta</td>
260
                                                        											<td className="text-left">
12717 stevensc 261
                                                        												{question.text}
12709 stevensc 262
                                                        											</td>
263
                                                        											<td className="text-capitalize">
264
                                                        												{sectionTypeOptions[question.type]}
265
                                                        											</td>
266
                                                        											<td>
267
                                                        												<button className="btn btn-default btn-edit-question">
268
                                                        													<i className="fa fa-edit" /> Editar Pregunta
269
                                                        												</button>
270
                                                        												<button className="btn btn-default btn-delete-question">
271
                                                        													<i className="fa fa-ban" /> Borrar Pregunta
272
                                                        												</button>
273
                                                        											</td>
274
                                                        										</tr>
275
                                                        									))
276
                                                        								}
277
                                                        							</tbody>
278
                                                        						</table>
279
                                                        					</div>
280
                                                        				</div>
281
                                                        			</div>
282
                                                        		</div>
283
                                                        	)
284
                                                        })
285
													}
286
												</div>
287
											</div>
288
										</div>
289
									</div>
290
								</div>
291
							</div>
292
						</div>
293
						<div className="d-flex" style={{ gap: '5px' }}>
294
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
295
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
296
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
297
						</div>
298
					</div>
299
				</div >
300
			</section >
301
			<SectionModal
302
				show={isShowSection}
303
				section={sectionSelected}
304
				closeModal={closeSectionModal}
305
				onSubmit={sectionType === 'add' ? addSection : editSection}
306
			/>
307
		</>
308
	)
309
}
310
 
311
export default FormView