Proyectos de Subversion LeadersLinked - Backend

Rev

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