Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
11024 stevensc 1
/* eslint-disable no-mixed-spaces-and-tabs */
10061 stevensc 2
import React, { useState, useEffect } from 'react'
3
import axios from 'axios'
4
import { Card } from 'react-bootstrap'
5
import { LengthFilter, SearchInput, Table, TablePagination } from '../../components/TableComponents'
6
import { addNotification } from '../../../redux/notification/notification.actions'
7
import { useDispatch } from 'react-redux'
8
import DeleteModal from '../../../shared/DeleteModal'
10095 stevensc 9
import ContentTitle from '../../../shared/ContentTitle'
11046 stevensc 10
import AddModal from '../components/AddModal'
11061 stevensc 11
import EditModal from '../components/EditModal'
9954 stevensc 12
 
10061 stevensc 13
const headers = [
11024 stevensc 14
	{ key: 'first_name', label: 'Nombre', isSorteable: true },
15
	{ key: 'last_name', label: 'Apellido', isSorteable: true },
16
	{ key: 'email', label: 'Correo electrónico', isSorteable: true },
17
	{ key: 'status', label: 'Estatus', isSorteable: false },
18
	{ key: 'actions', label: 'Acciones', isSorteable: false }
10061 stevensc 19
]
9954 stevensc 20
 
11024 stevensc 21
const MainView = ({ email_link, add_link, table_link, permisions, vacancies }) => {
9954 stevensc 22
 
11024 stevensc 23
	const dispatch = useDispatch()
24
	const [showDeleteModal, setShowDeleteModal] = useState(false)
11046 stevensc 25
	const [isShowAddModal, setIsShowAddModal] = useState(false)
11061 stevensc 26
	const [currentEditLink, setCurrentEditLink] = useState('')
27
	const [isShowEditModal, setIsShowEditModal] = useState(false)
15310 stevensc 28
	const [currentVacancy, setCurrentVacancy] = useState((vacancies.length > 0) ? vacancies[0].uuid : '')
11024 stevensc 29
	const [actionLink, setActionLink] = useState('')
12712 stevensc 30
	const [items, setItems] = useState([])
31
	const [total, setTotal] = useState(0)
11024 stevensc 32
	const [search, setSearch] = useState('')
33
	const [dataLength, setDataLength] = useState(10)
12375 stevensc 34
	const [startItem, setStartItem] = useState(1)
35
	const [lastItem, setLastItem] = useState(10)
11024 stevensc 36
	const [pages, setPages] = useState({
37
		current: 1,
38
		last: 1
39
	})
10061 stevensc 40
 
11024 stevensc 41
	const getData = ({ url = '', params = {} }) => {
10061 stevensc 42
 
11024 stevensc 43
		axios.get(`${url}/${currentVacancy}`, { params: { ...params } })
44
			.then(({ data }) => {
45
				if (!data.success) {
46
					dispatch(addNotification({
11051 stevensc 47
						style: 'danger',
11024 stevensc 48
						msg: 'Ha ocurrido un error'
49
					}))
50
				}
10061 stevensc 51
 
12712 stevensc 52
				setItems(data.data.items)
53
				setTotal(data.data.total)
11024 stevensc 54
				setPages({ ...pages, last: Math.ceil(data.data.total / dataLength) })
55
			})
56
			.catch(() => dispatch(addNotification({
11051 stevensc 57
				style: 'danger',
11024 stevensc 58
				msg: 'Ha ocurrido un error'
59
			})))
60
	}
10061 stevensc 61
 
11024 stevensc 62
	useEffect(() => {
63
		getData({
64
			url: table_link,
65
			params: {
66
				search: search,
67
				length: dataLength,
12375 stevensc 68
				start: pages.current
11024 stevensc 69
			}
70
		})
11025 stevensc 71
	}, [search, dataLength, pages.current, currentVacancy])
10061 stevensc 72
 
12375 stevensc 73
	useEffect(() => {
74
		if (pages.current > 1) {
75
			setStartItem((dataLength * (pages.current - 1)) + 1)
76
		} else {
77
			setStartItem(1)
78
		}
79
	}, [pages.current])
80
 
81
	useEffect(() => {
12712 stevensc 82
		if (items) {
12375 stevensc 83
			if (startItem > 1) {
12712 stevensc 84
				setLastItem(startItem + (items.length - 1))
12375 stevensc 85
			} else {
12712 stevensc 86
				setLastItem(items.length)
12375 stevensc 87
			}
88
		}
12712 stevensc 89
	}, [items])
12375 stevensc 90
 
11024 stevensc 91
	return (
92
		<ContentTitle title='Preselección'>
93
			<section className="content">
94
				<div className="container-fluid">
95
					<div className="row">
96
						<div className="col-12">
97
							<Card>
98
								<Card.Header>
99
									<div className="row" style={{ gap: '10px' }}>
100
										<div className="form-group">
101
											<label>Vacantes</label>
102
											<select
103
												className="form-control"
104
												value={currentVacancy}
105
												onChange={(e) => setCurrentVacancy(e.target.value)}
106
												defaultValue={vacancies[0].uuid}
107
											>
108
												{
109
													vacancies.map((vacancy) => (
110
														<option key={vacancy.uuid} value={vacancy.uuid}>{vacancy.name}</option>
111
													))
112
												}
113
											</select>
114
										</div>
115
									</div>
116
									<div className="row justify-content-end" style={{ gap: '10px' }}>
15310 stevensc 117
										{permisions.allowAdd &&
11059 stevensc 118
											<label
119
												className='d-flex align-items-center'
120
												onClick={() => setIsShowAddModal(true)}
121
												style={{ cursor: 'pointer' }}
122
											>
123
												<i className="fa fa-plus mr-2" />
124
												Agregar
125
											</label>
11024 stevensc 126
										}
127
										<label
128
											className='d-flex align-items-center'
129
											onClick={() => getData({
130
												url: table_link,
131
												params: {
132
													search: search,
133
													length: dataLength,
134
													page: pages.current
135
												}
136
											})}
137
											style={{ cursor: 'pointer' }}
138
										>
139
											<i className='fa fa-refresh mr-2' />
11059 stevensc 140
											Actualizar
11024 stevensc 141
										</label>
142
									</div>
143
									<div className="row justify-content-between align-items-center">
144
										<LengthFilter onChange={(e) => setDataLength(e.target.value)} />
145
										<SearchInput onChange={(e) => setSearch(e.target.value)} />
146
									</div>
147
								</Card.Header>
148
								<Card.Body>
11268 stevensc 149
									<div className="table-responsive">
12712 stevensc 150
										<Table data={items} headers={headers} setData={setItems}>
11268 stevensc 151
											{
12712 stevensc 152
												items.map((item, index) => (
11268 stevensc 153
													<tr key={index}>
154
														<td>{item.first_name}</td>
155
														<td>{item.last_name}</td>
156
														<td>{item.email}</td>
157
														<td>
158
															{
159
																item.status === 'a'
160
																	? <i className='fa fa-check' style={{ color: '#5cb85c' }} />
161
																	: <i className='fa fa-close' style={{ color: 'red' }} />
162
															}
163
														</td>
164
														<td className='d-flex' style={{ gap: '10px' }}>
15310 stevensc 165
															{permisions.allowEdit &&
11268 stevensc 166
																<i
167
																	className='fa fa-pencil'
168
																	style={{ cursor: 'pointer' }}
169
																	onClick={() => {
170
																		setCurrentEditLink(item.actions.link_edit)
171
																		setIsShowEditModal(true)
172
																	}}
173
																/>
174
															}
15310 stevensc 175
															{permisions.allowDelete &&
11268 stevensc 176
																<i
177
																	className='fa fa-trash'
178
																	onClick={() => {
179
																		setShowDeleteModal(true)
180
																		setActionLink(item.actions.link_delete)
181
																	}}
182
																	style={{ cursor: 'pointer' }}
183
																/>
184
															}
185
														</td>
186
													</tr>
187
												))
188
											}
189
										</Table>
190
									</div>
11024 stevensc 191
									<div className='row justify-content-between align-items-center'>
192
										<p className='mb-0'>
12712 stevensc 193
											{`Mostrando registros del ${startItem} al ${lastItem} de un total de ${total} registros`}
11024 stevensc 194
										</p>
195
										<TablePagination
12326 stevensc 196
											onDecrement={() => setPages({ ...pages, current: pages.current - 1 })}
197
											onIncrement={() => setPages({ ...pages, current: pages.current + 1 })}
11024 stevensc 198
											totalPages={pages.last}
199
											currentPage={pages.current}
200
										/>
201
									</div>
202
								</Card.Body>
203
							</Card>
204
						</div>
205
					</div >
206
				</div >
207
			</section >
11049 stevensc 208
			<AddModal
11046 stevensc 209
				email_link={email_link}
11059 stevensc 210
				add_link={add_link.replace('UUID_PLACEHOLDER', currentVacancy)}
11046 stevensc 211
				closeModal={() => setIsShowAddModal(false)}
212
				isOpen={isShowAddModal}
15310 stevensc 213
				onComplete={() => getData({ url: table_link })}
11049 stevensc 214
			/>
11061 stevensc 215
			<EditModal
216
				edit_link={currentEditLink}
217
				closeModal={() => setIsShowEditModal(false)}
218
				isOpen={isShowEditModal}
11227 stevensc 219
				resetLink={() => setCurrentEditLink('')}
11061 stevensc 220
			/>
11024 stevensc 221
			<DeleteModal
222
				url={actionLink}
223
				isOpen={showDeleteModal}
224
				closeModal={() => setShowDeleteModal(false)}
15310 stevensc 225
				title="Esta seguro de borrar este candidato?"
12712 stevensc 226
				onComplete={() => setItems(items.filter((item) => item.actions.link_delete !== actionLink))}
15310 stevensc 227
				message="Candidato eliminado"
11024 stevensc 228
			/>
229
		</ContentTitle>
230
	)
9954 stevensc 231
}
232
export default MainView