Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 14197 | Rev 14201 | 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) {
70
					dispatch(addNotification({
71
						style: 'danger',
72
						msg: data.data
73
					}))
74
				}
75
			})
76
	}
77
 
14184 stevensc 78
	useEffect(() => {
79
		getData({
80
			url: table_link,
81
			params: {
82
				search: search,
83
				length: dataLength,
84
				start: pages.current
85
			}
86
		})
87
	}, [search, dataLength, pages.current])
88
 
89
	useEffect(() => {
90
		if (pages.current > 1) {
91
			setStartItem((dataLength * (pages.current - 1)) + 1)
92
		} else {
93
			setStartItem(1)
94
		}
95
	}, [pages.current])
96
 
97
	useEffect(() => {
98
		if (items) {
99
			if (startItem > 1) {
100
				setLastItem(startItem + (items.length - 1))
101
			} else {
102
				setLastItem(items.length)
103
			}
104
		}
105
	}, [items])
106
 
107
	return (
14197 stevensc 108
		<>
109
			<section className="content">
110
				<div className="container-fluid">
111
					<div className="row">
112
						<div className="col-12">
113
							<Card>
114
								<Card.Header>
115
									<div className="row justify-content-end" style={{ gap: '10px' }}>
14184 stevensc 116
										{
14197 stevensc 117
											permisions.allowAdd
14184 stevensc 118
                                            &&
14197 stevensc 119
                                            <label
120
                                            	onClick={getImport}
121
                                            	className='d-flex align-items-center btn-add'
122
                                            	style={{ cursor: 'pointer' }}
123
                                            >
124
                                            	<i className="fa fa-upload mr-2" />
125
                                                Importar
126
                                            </label>
14184 stevensc 127
										}
14197 stevensc 128
										{
129
											permisions.allowAdd
130
                                            &&
131
                                            <label
132
                                            	onClick={() => setActionLink(add_link)}
133
                                            	className='d-flex align-items-center btn-add'
134
                                            	style={{ cursor: 'pointer' }}
135
                                            >
136
                                            	<i className="fa fa-plus mr-2" />
137
                                                Agregar
138
                                            </label>
139
										}
140
										<label
141
											className='d-flex align-items-center'
142
											onClick={() => getData({
143
												url: table_link,
144
												params: {
145
													search: search,
146
													length: dataLength,
147
													page: pages.current
148
												}
149
											})}
150
											style={{ cursor: 'pointer' }}
151
										>
152
											<i className='fa fa-refresh mr-2' />
153
                                            Actualizar
154
										</label>
155
									</div>
156
									<div className="row justify-content-between align-items-center">
157
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
158
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
159
									</div>
160
								</Card.Header>
161
								<Card.Body>
162
									<div className="table-responsive">
163
										<Table data={items} headers={headers} setData={setItems}>
164
											{
165
												items.length
166
                                                &&
167
                                                items.map((item, index) => (
168
                                                	<tr key={index}>
169
                                                		<td className='text-vertical-middle'>{item.name}</td>
170
                                                		<td className='text-vertical-middle'>
171
                                                			{item.status === 'a'
172
                                                				? 'Activo'
173
                                                				: 'Inactivo'
174
                                                			}
175
                                                		</td>
176
                                                		<td>
177
                                                			<div className="d-flex align-items-center" style={{ gap: '.5rem' }}>
178
                                                				{
179
                                                					permisions.allowEdit
180
                                                                    &&
181
                                                                    <i
182
                                                                    	onClick={() => {
183
                                                                    		setActionLink(item.actions.link_edit)
184
                                                                    		history.push('/settings/jobs-description/edit')
185
                                                                    	}}
186
                                                                    	className='fa fa-pencil'
187
                                                                    	style={{ cursor: 'pointer' }}
188
                                                                    />
189
                                                				}
190
                                                				{
191
                                                					permisions.allowDelete
192
                                                                    &&
193
                                                                    <i
194
                                                                    	className='fa fa-trash'
195
                                                                    	onClick={() => {
196
                                                                    		setActionLink(item.actions.link_delete)
197
                                                                    		setIsOpen(true)
198
                                                                    	}}
199
                                                                    	style={{ cursor: 'pointer' }}
200
                                                                    />
201
                                                				}
202
                                                				{
203
                                                					permisions.allowReport
204
                                                                    &&
205
                                                                    <a
206
                                                                    	href={item.actions.link_report}
207
                                                                    	target='_blank'
208
                                                                    	rel="noreferrer"
209
                                                                    >
210
                                                                    	<i
211
                                                                    		className='fa fa-file'
212
                                                                    		style={{ cursor: 'pointer' }}
213
                                                                    	/>
214
                                                                    </a>
215
                                                				}
216
                                                			</div>
217
                                                		</td>
218
                                                	</tr>
219
                                                ))
220
											}
221
										</Table>
222
									</div>
223
									<div className='row justify-content-between align-items-center'>
224
										<p className='mb-0'>
225
											{`Mostrando registros del ${startItem} al ${lastItem} de un total de ${total} registros`}
226
										</p>
227
										<PaginationComponent
228
											pages={pages.last}
229
											currentActivePage={pages.current}
230
											onChangePage={(page) => setPages({ ...pages, current: page })}
231
										/>
232
									</div>
233
								</Card.Body>
234
							</Card>
235
						</div>
236
					</div >
14184 stevensc 237
				</div >
14197 stevensc 238
			</section >
239
			<DeleteModal
240
				url={actionLink}
241
				isOpen={isOpen}
242
				closeModal={() => setIsOpen(false)}
243
				title="Esta seguro de borrar este registro?"
244
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
245
				message="Registro eliminado"
246
			/>
247
		</>
14184 stevensc 248
	)
249
}
250
 
251
export default TableView