Proyectos de Subversion LeadersLinked - Backend

Rev

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

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