Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
11960 stevensc 1
/* eslint-disable no-mixed-spaces-and-tabs */
2
import React, { useState, useEffect } from 'react'
3
import { Card } from 'react-bootstrap'
4
import { LengthFilter, SearchInput, Table, TablePagination } from '../../../recruitment_and_selection/components/TableComponents'
5
import { addNotification } from '../../../redux/notification/notification.actions'
6
import DeleteModal from '../../../shared/DeleteModal'
7
import axios from 'axios'
8
import { useDispatch } from 'react-redux'
12012 stevensc 9
import { useHistory, useRouteMatch } from 'react-router-dom'
11960 stevensc 10
 
11
const headers = [
12
	{ key: 'name', label: 'Nombre', isSorteable: true },
13
	{ key: 'job_description', label: 'Descripción', isSorteable: true },
14
	{ key: 'status', label: 'Estatus', isSorteable: false },
15
	{ key: 'actions', label: 'Acciones', isSorteable: false }
16
]
17
 
12012 stevensc 18
const TableView = ({ add_link, table_link, permisions, actionLink, setActionLink }) => {
11960 stevensc 19
 
20
	const dispatch = useDispatch()
12012 stevensc 21
	const history = useHistory()
12014 stevensc 22
	const { path } = useRouteMatch()
11960 stevensc 23
	const [modalToShow, setModalToShow] = useState('')
24
	const [items, setItems] = useState([])
25
	const [total, setTotal] = useState(0)
26
	const [search, setSearch] = useState('')
27
	const [dataLength, setDataLength] = useState(10)
28
	const [pages, setPages] = useState({
29
		current: 1,
30
		last: 1
31
	})
32
 
33
	const getData = ({ url = '', params = {} }) => {
34
 
35
		axios.get(url, { params: { ...params } })
36
			.then(({ data }) => {
37
				if (!data.success) {
38
					return dispatch(addNotification({
39
						style: 'danger',
40
						msg: 'Ha ocurrido un error'
41
					}))
42
				}
43
 
44
				setItems(data.data.items)
45
				setTotal(data.data.total)
46
				setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
47
			})
48
			.catch(() => dispatch(addNotification({
49
				style: 'danger',
50
				msg: 'Ha ocurrido un error'
51
			})))
52
	}
53
 
54
	useEffect(() => {
55
		getData({
56
			url: table_link,
57
			params: {
58
				search: search,
59
				length: dataLength,
60
				page: pages.current
61
			}
62
		})
63
	}, [search, dataLength, pages.current])
64
 
65
	return (
66
		<>
11965 stevensc 67
			<section className="content">
68
				<div className="container-fluid">
69
					<div className="row">
70
						<div className="col-12">
71
							<Card>
72
								<Card.Header>
73
									<div className="row justify-content-end" style={{ gap: '10px' }}>
74
										{
75
											permisions.allowAdd
76
											&&
77
											<label
78
												className='d-flex align-items-center'
79
												onClick={() => {
80
													setActionLink(add_link)
12014 stevensc 81
													history.push(`/${path}/add`)
11965 stevensc 82
												}}
83
												style={{ cursor: 'pointer' }}
84
											>
85
												<i className="fa fa-plus mr-2" />
86
												Agregar
87
											</label>
88
										}
89
										<label
90
											className='d-flex align-items-center'
91
											onClick={() => getData({
92
												url: table_link,
93
												params: {
94
													search: search,
95
													length: dataLength,
96
													page: pages.current
97
												}
98
											})}
99
											style={{ cursor: 'pointer' }}
100
										>
101
											<i className='fa fa-refresh mr-2' />
102
											Actualizar
103
										</label>
104
									</div>
105
									<div className="row justify-content-between align-items-center">
106
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
107
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
108
									</div>
109
								</Card.Header>
110
								<Card.Body>
111
									<div className="table-responsive">
112
										<Table data={items} headers={headers} setData={setItems}>
113
											{
114
												items.length
115
												&&
116
												items.map((item, index) => (
117
													<tr key={index}>
118
														<td className='text-vertical-middle'>{item.name}</td>
119
														<td className='text-vertical-middle'>{item.job_description}</td>
120
														<td className='text-vertical-middle'>
121
															{
122
																item.status === 'a'
123
																	? 'Activo'
124
																	: 'Inactivo'
125
															}
126
														</td>
127
														<td>
128
															<div className="d-flex align-items-center" style={{ gap: '5px' }}>
129
																{
130
																	permisions.allowEdit
131
																	&&
132
																	<i
133
																		className='fa fa-pencil'
134
																		onClick={() => {
135
																			setActionLink(item.actions.link_edit)
12014 stevensc 136
																			history.push(`/${path}/edit`)
11965 stevensc 137
																		}}
138
																		style={{ cursor: 'pointer' }}
139
																	/>
140
																}
141
																{
142
																	permisions.allowDelete
143
																	&&
144
																	<i
145
																		className='fa fa-trash'
146
																		onClick={() => {
147
																			setActionLink(item.actions.link_delete)
148
																			setModalToShow('delete')
149
																		}}
150
																		style={{ cursor: 'pointer' }}
151
																	/>
152
																}
153
																{
154
																	permisions.allowReport
155
																	&&
156
																	<a href={item.actions.link_report} target='_blank' rel="noreferrer" >
157
																		<i className='fa fa-fil' style={{ color: '#333' }} />
158
																	</a>
159
																}
160
															</div>
161
														</td>
162
													</tr>
163
												))
164
											}
165
										</Table>
166
									</div>
167
									<div className='row justify-content-between align-items-center'>
168
										<p className='mb-0'>
169
											{`Mostrando registros del ${(dataLength * pages.current) - (dataLength - 1) || 0} al ${(dataLength * pages.current) - (dataLength - total) || 0} de un total de ${total || 0} registros`}
170
										</p>
171
										<TablePagination
172
											onDecrement={() => setPages(prev => prev.current -= 1)}
173
											onIncrement={() => setPages(prev => prev.current += 1)}
174
											totalPages={pages.last}
175
											currentPage={pages.current}
176
										/>
177
									</div>
178
								</Card.Body>
179
							</Card>
180
						</div>
181
					</div >
182
				</div >
183
			</section >
184
			<DeleteModal
185
				url={actionLink}
186
				isOpen={modalToShow === 'delete'}
187
				closeModal={() => setModalToShow('')}
188
				title="Esta seguro de borrar este formulario?"
189
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
190
				message="Registro borrado"
191
			/>
11960 stevensc 192
		</>
193
	)
194
}
195
 
196
export default TableView