Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 14201 | Rev 14843 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
14184 stevensc 1
/* eslint-disable no-mixed-spaces-and-tabs */
2
import axios from 'axios'
3
import React, { useEffect, useState } from 'react'
4
import { Card } from 'react-bootstrap'
5
import { useDispatch } from 'react-redux'
6
import { useHistory } from 'react-router-dom'
7
import { LengthFilter, SearchInput, Table } from '../../recruitment_and_selection/components/TableComponents'
8
import { addNotification } from '../../redux/notification/notification.actions'
14197 stevensc 9
import DeleteModal from '../../shared/DeleteModal'
14184 stevensc 10
import PaginationComponent from '../../shared/PaginationComponent'
11
 
12
const headers = [
13
	{ key: 'name', label: 'Nombre', isSorteable: true },
14
	{ key: 'status', label: 'Activo', isSorteable: false },
15
	{ key: 'actions', label: 'Acciones', isSorteable: false }
16
]
17
 
18
const TableView = ({
14197 stevensc 19
	actionLink = '',
14193 stevensc 20
	import_link = '',
14184 stevensc 21
	add_link = '',
22
	table_link = '',
23
	permisions = '',
24
	setActionLink = function () { }
25
}) => {
26
 
27
	//Hooks
28
	const dispatch = useDispatch()
29
	const history = useHistory()
30
 
31
	//State
14197 stevensc 32
	const [isOpen, setIsOpen] = useState(false)
14184 stevensc 33
	const [items, setItems] = useState([])
34
	const [total, setTotal] = useState(0)
35
	const [search, setSearch] = useState('')
36
	const [dataLength, setDataLength] = useState(10)
37
	const [startItem, setStartItem] = useState(1)
38
	const [lastItem, setLastItem] = useState(10)
39
	const [pages, setPages] = useState({
40
		current: 1,
41
		last: 1
42
	})
43
 
44
	const getData = ({ url = '', params = {} }) => {
14199 stevensc 45
		axios.get(url, { params: { ...params } })
14184 stevensc 46
			.then(({ data }) => {
47
				if (!data.success) {
48
					return dispatch(addNotification({
49
						style: 'danger',
50
						msg: typeof data.data === 'string'
51
							? data.data
52
							: 'Ha ocurrido un error'
53
					}))
54
				}
55
 
56
				setItems(data.data.items)
57
				setTotal(data.data.total)
58
				setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
59
			})
60
			.catch(() => dispatch(addNotification({
61
				style: 'danger',
62
				msg: 'Ha ocurrido un error'
63
			})))
64
	}
65
 
14193 stevensc 66
	const getImport = () => {
14199 stevensc 67
		axios.post(import_link)
14193 stevensc 68
			.then(({ data }) => {
69
				if (!data.success) {
14201 stevensc 70
					return dispatch(addNotification({
14193 stevensc 71
						style: 'danger',
72
						msg: data.data
73
					}))
74
				}
14201 stevensc 75
 
76
				dispatch(addNotification({
77
					style: 'success',
78
					msg: data.data
79
				}))
14193 stevensc 80
			})
81
	}
82
 
14184 stevensc 83
	useEffect(() => {
84
		getData({
85
			url: table_link,
86
			params: {
87
				search: search,
88
				length: dataLength,
89
				start: pages.current
90
			}
91
		})
92
	}, [search, dataLength, pages.current])
93
 
94
	useEffect(() => {
95
		if (pages.current > 1) {
96
			setStartItem((dataLength * (pages.current - 1)) + 1)
97
		} else {
98
			setStartItem(1)
99
		}
100
	}, [pages.current])
101
 
102
	useEffect(() => {
103
		if (items) {
104
			if (startItem > 1) {
105
				setLastItem(startItem + (items.length - 1))
106
			} else {
107
				setLastItem(items.length)
108
			}
109
		}
110
	}, [items])
111
 
112
	return (
14197 stevensc 113
		<>
114
			<section className="content">
115
				<div className="container-fluid">
116
					<div className="row">
117
						<div className="col-12">
118
							<Card>
119
								<Card.Header>
120
									<div className="row justify-content-end" style={{ gap: '10px' }}>
14184 stevensc 121
										{
14197 stevensc 122
											permisions.allowAdd
14253 stevensc 123
											&&
124
											<label
125
												onClick={getImport}
126
												className='d-flex align-items-center btn-add'
127
												style={{ cursor: 'pointer' }}
128
											>
129
												<i className="fa fa-upload mr-2" />
130
												Importar
131
											</label>
14184 stevensc 132
										}
14197 stevensc 133
										{
134
											permisions.allowAdd
14253 stevensc 135
											&&
136
											<label
137
												onClick={() => {
138
													setActionLink(add_link)
139
													history.push('/settings/jobs-description/add')
140
												}}
141
												className='d-flex align-items-center btn-add'
142
												style={{ cursor: 'pointer' }}
143
											>
144
												<i className="fa fa-plus mr-2" />
145
												Agregar
146
											</label>
14197 stevensc 147
										}
148
										<label
149
											className='d-flex align-items-center'
150
											onClick={() => getData({
151
												url: table_link,
152
												params: {
153
													search: search,
154
													length: dataLength,
155
													page: pages.current
156
												}
157
											})}
158
											style={{ cursor: 'pointer' }}
159
										>
160
											<i className='fa fa-refresh mr-2' />
14253 stevensc 161
											Actualizar
14197 stevensc 162
										</label>
163
									</div>
164
									<div className="row justify-content-between align-items-center">
165
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
166
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
167
									</div>
168
								</Card.Header>
169
								<Card.Body>
170
									<div className="table-responsive">
171
										<Table data={items} headers={headers} setData={setItems}>
172
											{
173
												items.length
14253 stevensc 174
												&&
175
												items.map((item, index) => (
176
													<tr key={index}>
177
														<td className='text-vertical-middle'>{item.name}</td>
178
														<td className='text-vertical-middle'>
179
															{item.status === 'a'
180
																? 'Activo'
181
																: 'Inactivo'
182
															}
183
														</td>
184
														<td>
185
															<div className="d-flex align-items-center" style={{ gap: '.5rem' }}>
186
																{
187
																	permisions.allowEdit
188
																	&&
189
																	<i
190
																		onClick={() => {
191
																			setActionLink(item.actions.link_edit)
192
																			history.push('/settings/jobs-description/edit')
193
																		}}
194
																		className='fa fa-pencil'
195
																		style={{ cursor: 'pointer' }}
196
																	/>
197
																}
198
																{
199
																	permisions.allowDelete
200
																	&&
201
																	<i
202
																		className='fa fa-trash'
203
																		onClick={() => {
204
																			setActionLink(item.actions.link_delete)
205
																			setIsOpen(true)
206
																		}}
207
																		style={{ cursor: 'pointer' }}
208
																	/>
209
																}
210
																{
211
																	permisions.allowReport
212
																	&&
213
																	<a
214
																		href={item.actions.link_report}
215
																		target='_blank'
216
																		rel="noreferrer"
217
																	>
218
																		<i
219
																			className='fa fa-file'
220
																			style={{ cursor: 'pointer' }}
221
																		/>
222
																	</a>
223
																}
224
															</div>
225
														</td>
226
													</tr>
227
												))
14197 stevensc 228
											}
229
										</Table>
230
									</div>
231
									<div className='row justify-content-between align-items-center'>
232
										<p className='mb-0'>
233
											{`Mostrando registros del ${startItem} al ${lastItem} de un total de ${total} registros`}
234
										</p>
235
										<PaginationComponent
236
											pages={pages.last}
237
											currentActivePage={pages.current}
238
											onChangePage={(page) => setPages({ ...pages, current: page })}
239
										/>
240
									</div>
241
								</Card.Body>
242
							</Card>
243
						</div>
244
					</div >
14184 stevensc 245
				</div >
14197 stevensc 246
			</section >
247
			<DeleteModal
248
				url={actionLink}
249
				isOpen={isOpen}
250
				closeModal={() => setIsOpen(false)}
251
				title="Esta seguro de borrar este registro?"
252
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
253
				message="Registro eliminado"
254
			/>
255
		</>
14184 stevensc 256
	)
257
}
258
 
259
export default TableView