Proyectos de Subversion LeadersLinked - Backend

Rev

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

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