Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12733 | Rev 12745 | 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(() => {
12726 stevensc 138
		if (action === 'edit') {
139
			axios.get(actionLink)
140
				.then(({ data }) => {
141
					if (!data.success) {
142
						return dispatch(addNotification({
143
							style: 'danger',
144
							msg: 'Ha ocurrido un error'
145
						}))
146
					}
12709 stevensc 147
 
12726 stevensc 148
					setValue('name', data.data.name)
149
					setValue('description', data.data.description)
150
					setValue('text', data.data.description)
151
					setContent(data.data.content)
152
					setStatus(data.data.status)
153
				})
154
		}
12709 stevensc 155
	}, [actionLink])
156
 
157
	return (
158
		<>
159
			<section className="content">
160
				<div className="row" style={{ padding: 16 }}>
161
					<div className="col-xs-12 col-md-12">
162
						<div className="form-group">
163
							<label>Nombre</label>
164
							<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
165
						</div>
166
						<div className="form-group">
167
							<label htmlFor="form-description">Descripción</label>
12734 stevensc 168
							{/* <DescriptionInput
12733 stevensc 169
								defaultValue={watch('description') ? parse(watch('description')) : ''}
12709 stevensc 170
								name='description'
171
								onChange={setValue}
12734 stevensc 172
							/> */}
12709 stevensc 173
						</div>
174
						<div className="form-group">
175
							<label htmlFor="form-description">Texto</label>
12734 stevensc 176
							{/* <DescriptionInput
12733 stevensc 177
								defaultValue={watch('text') ? parse(watch('text')) : ''}
12709 stevensc 178
								name='text'
179
								onChange={setValue}
12734 stevensc 180
							/> */}
12709 stevensc 181
						</div>
182
						<div className="form-group">
183
							<label htmlFor="form-status">Estatus</label>
184
							<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
12728 stevensc 185
								<option value="A">Activo</option>
186
								<option value="I">Inactivo</option>
12709 stevensc 187
							</select>
188
						</div>
189
						<br />
190
						<div className="row">
191
							<div className="col-xs-12 col-md-12">
192
								<div className="panel-group" id="rows" >
193
									<div className="form-group">
194
										<div className="row">
195
											<div className="col-xs-12 col-md-12">
196
												<hr />
197
												<h4 style={{ fontSize: 18, fontWeight: 'bold' }}>Competencias asociadas al cargo:</h4>
198
												<button className='btn btn-primary' onClick={showSectionModal}>
199
                                                    Agregar sección
200
												</button>
201
												<br />
202
												<div className="panel-group" id="rows-job-competencies" >
203
													{
204
														content.length > 0
205
                                                        &&
206
                                                        content.map((section) => {
207
 
208
                                                        	return (
209
                                                        		<div className="panel panel-default" key={section.slug_section}>
210
                                                        			<div className="panel-heading">
211
                                                        				<h4 className="panel-title">
212
                                                        					<a className="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent={`panel-${section.slug_section}`} href={`#collapse-${section.slug_section}`}>
213
                                                        						<span className={`section-name${section.slug_section}`}>
214
                                                        							{section.name}
215
                                                        						</span>
216
                                                        					</a>
217
                                                        				</h4>
218
                                                        			</div>
219
                                                        			<div id="collapse-section1661528423935" className="panel-collapse in collapse show">
220
                                                        				<div className="panel-body">
221
                                                        					<div className="table-responsive">
222
                                                        						<table className="table table-bordered">
223
                                                        							<thead>
224
                                                        								<tr>
225
                                                        									<th style={{ width: '10%' }}>Elemento</th>
226
                                                        									<th style={{ width: '50%' }}>Texto</th>
227
                                                        									<th style={{ width: '10%' }}>Tipo</th>
228
                                                        									<th style={{ width: '20%' }}>Acciones</th>
229
                                                        								</tr>
230
                                                        							</thead>
231
                                                        							<tbody>
232
                                                        								<tr className="tr-section">
233
                                                        									<td className="text-left">Sección</td>
234
                                                        									<td className="text-left">{section.name}</td>
235
                                                        									<td />
236
                                                        									<td>
237
                                                        										<button className="btn btn-default" onClick={() => showSectionModal(section, 'edit')}>
238
                                                        											<i className="fa fa-edit" />
239
                                                                                                    Editar Sección
240
                                                        										</button>
241
                                                        										<button className="btn btn-default" >
242
                                                        											<i className="fa fa-ban" />
243
                                                                                                    Borrar Sección
244
                                                        										</button>
245
                                                        										<button className="btn btn-default btn-add-question" >
246
                                                        											<i className="fa fa-plus" />
247
                                                                                                    Agregar  Pregunta
248
                                                        										</button>
249
                                                        									</td>
250
                                                        								</tr>
251
                                                        								{
252
                                                        									section.questions.map((question) => (
253
                                                        										<tr key={question.slug_question} className="tr-question">
254
                                                        											<td className="text-left">Pregunta</td>
255
                                                        											<td className="text-left">
12717 stevensc 256
                                                        												{question.text}
12709 stevensc 257
                                                        											</td>
258
                                                        											<td className="text-capitalize">
259
                                                        												{sectionTypeOptions[question.type]}
260
                                                        											</td>
261
                                                        											<td>
262
                                                        												<button className="btn btn-default btn-edit-question">
263
                                                        													<i className="fa fa-edit" /> Editar Pregunta
264
                                                        												</button>
265
                                                        												<button className="btn btn-default btn-delete-question">
266
                                                        													<i className="fa fa-ban" /> Borrar Pregunta
267
                                                        												</button>
268
                                                        											</td>
269
                                                        										</tr>
270
                                                        									))
271
                                                        								}
272
                                                        							</tbody>
273
                                                        						</table>
274
                                                        					</div>
275
                                                        				</div>
276
                                                        			</div>
277
                                                        		</div>
278
                                                        	)
279
                                                        })
280
													}
281
												</div>
282
											</div>
283
										</div>
284
									</div>
285
								</div>
286
							</div>
287
						</div>
288
						<div className="d-flex" style={{ gap: '5px' }}>
289
							<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
290
							<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
291
							<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
292
						</div>
293
					</div>
294
				</div >
295
			</section >
296
			<SectionModal
297
				show={isShowSection}
298
				section={sectionSelected}
299
				closeModal={closeSectionModal}
300
				onSubmit={sectionType === 'add' ? addSection : editSection}
301
			/>
302
		</>
303
	)
304
}
305
 
306
export default FormView