generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNavigationBar.js
294 lines (259 loc) · 8.11 KB
/
NavigationBar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// src/components/NavigationBar.js
import React, { useState } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import axios from 'axios';
import { AppBar, Tabs, Tab, Tooltip, Menu, MenuItem, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle, Button } from '@mui/material';
import SettingsIcon from '@mui/icons-material/Settings';
import AccountCircle from '@mui/icons-material/AccountCircle';
import HelpIcon from '@mui/icons-material/Help';
import LogoutIcon from '@mui/icons-material/Logout';
import LeaderboardIcon from '@mui/icons-material/Leaderboard';
import IconWIQ from '../favicon2.ico';
const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000';
const NavigationBar = () => {
const navigate = useNavigate();
const location = useLocation();
const { username } = location.state || {};
const { createdAt } = location.state || {};
const { selectedNumQuestions } = location.state || {};
const { selectedTimer } = location.state || {};
const [openDialog, setOpenDialog] = useState(false);
const [anchorElInfoMenu, setAnchorElInfoMenu] = React.useState(null);
const deleteTempQuestions = async () => {
await axios.post(`${apiEndpoint}/deleteTempQuestions`, { username });
};
const showHome = () => {
if (username !== undefined) {
deleteTempQuestions();
navigate("/home", { state: { username, createdAt, selectedNumQuestions, selectedTimer } });
}
};
const showSettings = () => {
if (username !== undefined) {
deleteTempQuestions();
navigate("/settings", { state: { username, createdAt } });
}
};
const showRecord = () => {
if (username !== undefined) {
deleteTempQuestions();
navigate("/record", { state: { username, createdAt } });
}
};
const showRanking = () => {
if (username !== undefined) {
deleteTempQuestions();
navigate("/ranking", { state: { username, createdAt } });
}
};
const showHelp = () => {
if (username !== undefined) {
deleteTempQuestions();
navigate("/help", { state: { username, createdAt } });
}
};
const showAboutUs = () => {
if (username !== undefined) {
deleteTempQuestions();
navigate("/aboutUs", { state: { username, createdAt } });
}
};
const showDBData = async () => {
if (username !== undefined) {
setOpenDialog(true);
}
}
const showApiDoc = () => {
window.location.href = 'http://20.26.114.153:8000/api-doc/'; //NOSONAR
};
const logOut = () => {
localStorage.removeItem('username');
localStorage.removeItem('userProfileUsername');
navigate('/');
};
const openInfoMenu = (event) => {
setAnchorElInfoMenu(event.currentTarget);
};
const closeMenu = () => {
setAnchorElInfoMenu(null);
};
const tabsStyle = {
display: 'flex',
justifyContent: 'space-between',
};
const tabStyle = {
color: 'white',
fontWeight: 'bold',
backgroundColor: '#209cee',
fontSize: 26,
flex: '0 0 auto',
paddingTop: 0,
paddingBottom: 0,
minWidth: 0,
};
const menuStyle = {
padding: 0,
};
const menuItemStyle = {
color: 'white',
fontWeight: 'bold',
backgroundColor: '#209cee',
'&:hover': {
backgroundColor: '#209cee',
},
};
const buttonStyle = {
color: 'white',
backgroundColor: '#209cee',
'&:hover': {
backgroundColor: '#1f89cf',
},
};
const handleCloseDialogDownload = async () => {
setOpenDialog(false);
await axios({
url: `${apiEndpoint}/getAllQuestions`,
method: 'GET',
responseType: 'blob',
}).then((response) => {
const href = URL.createObjectURL(response.data);
const link = document.createElement('a');
link.href = href;
link.setAttribute('download', 'questions.json');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);
}).catch((error) => {
console.error('Error en la solicitud:', error); // Aquí se imprimirá el error en la consola
// Aquí puedes manejar el error de la solicitud de otra manera si lo necesitas
});
await axios({
url: `${apiEndpoint}/getAllUsers`,
method: 'GET',
responseType: 'blob',
}).then((response) => {
const href = URL.createObjectURL(response.data);
const link = document.createElement('a');
link.href = href;
link.setAttribute('download', 'users.json');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);
});
showHome();
}
const cancelDialog = () => {
setOpenDialog(false);
}
return (
<div>
<AppBar position="absolute">
<Tabs aria-label="navigation tabs"
value={false}
variant="fullWidth"
sx={tabsStyle}
>
<Tab aria-label="Logo"
data-testid="logo-tab"
icon={<Tooltip title="Home">
<img src={IconWIQ} alt="Icono" />
</Tooltip>}
sx={tabStyle}
onClick={showHome} />
<Tab label="WIQ"
data-testid="wiq-tab"
sx={tabStyle}
onClick={showHome} />
<div style={{ flex: 5, background: '#209cee' }} />
<Tab aria-label="Settings"
data-testid="settings-tab"
icon={<Tooltip title="Settings">
<SettingsIcon sx={tabStyle} />
</Tooltip>}
sx={tabStyle}
onClick={showSettings} />
<Tab aria-label="Record"
data-testid="record-tab"
icon={<Tooltip title="Personal record">
<AccountCircle sx={tabStyle} />
</Tooltip>}
sx={tabStyle}
onClick={showRecord} />
<Tab aria-label="Ranking"
data-testid="ranking-tab"
icon={<Tooltip title="Ranking">
<LeaderboardIcon sx={tabStyle} />
</Tooltip>}
sx={tabStyle}
onClick={showRanking} />
<Tab aria-label="Info"
data-testid="info-tab"
icon={<Tooltip title="Info">
<HelpIcon sx={tabStyle} />
</Tooltip>}
sx={tabStyle}
onClick={openInfoMenu} />
<Tab aria-label="Log out"
data-testid="logout-tab"
icon={<Tooltip title="Log out">
<LogoutIcon sx={tabStyle} />
</Tooltip>}
sx={tabStyle}
onClick={logOut} />
</Tabs>
</AppBar>
<Menu aria-label="Info menu"
anchorEl={anchorElInfoMenu}
open={Boolean(anchorElInfoMenu)}
onClose={closeMenu}
MenuListProps={{
sx: menuStyle
}}
>
<MenuItem aria-label="Help"
data-testid="help-item"
sx={menuItemStyle}
onClick={showHelp} >
Help
</MenuItem>
<MenuItem aria-label="About us"
data-testid="about-us-item"
sx={menuItemStyle}
onClick={showAboutUs} >
About us
</MenuItem>
<MenuItem aria-label="Get DB data"
data-testid="about-us-item"
sx={menuItemStyle}
onClick={showDBData} >
Get DB data
</MenuItem>
<MenuItem aria-label="API DOC"
data-testid="api-doc-item"
sx={menuItemStyle}
onClick={showApiDoc} >
API DOC
</MenuItem>
</Menu>
<Dialog open={openDialog} aria-labelledby="alert-dialog-title" aria-describedby="alert-dialog-description">
<DialogTitle id="alert-dialog-title">{"Database data"}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
You will download all questions and users in database.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleCloseDialogDownload} sx={buttonStyle} autoFocus>
Download
</Button>
<Button onClick={cancelDialog} sx={buttonStyle}>
Cancel
</Button>
</DialogActions>
</Dialog>
</div>
);
};
export default NavigationBar;