Skip to content

Commit dcdf143

Browse files
committed
Refactored code
1 parent 534386e commit dcdf143

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

identity-service-logs/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<h1>Identity Service Logs</h1>
2020
</nav>
2121

22+
<div class="toast">
23+
<div class="toast__message"></div>
24+
</div>
25+
2226
<div class="stats">
2327
<div>Verified Users: <span id="verified" class="stats-value"></span></div>
2428
<div>Blocked Users: <span id="blocked" class="stats-value"></span></div>

identity-service-logs/script.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { SUPER_USER } from './constants.js';
22

33
import {
44
getIdentityLogs,
5-
getSelfUser,
5+
getIsSuperUser,
66
fillData,
77
getUserCount,
88
} from './utils.js';
99

10-
const self_user = await getSelfUser();
10+
const { isSuperUser } = await getIsSuperUser();
1111

12-
if (self_user?.roles[SUPER_USER]) {
12+
if (isSuperUser) {
1313
const {
1414
verifiedUsersCount,
1515
blockedUsersCount,

identity-service-logs/style.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,53 @@ nav h1::before {
172172
transition-delay: 400ms;
173173
}
174174

175+
/*
176+
FOR Message toaster box
177+
*/
178+
179+
.toast {
180+
position: fixed;
181+
top: 20px;
182+
right: 20px;
183+
width: 300px;
184+
}
185+
186+
.toast__message {
187+
padding: 15px;
188+
background-color: #9c3838;
189+
color: white;
190+
border-radius: 5px;
191+
margin-bottom: 10px;
192+
display: none;
193+
}
194+
195+
.toast--show {
196+
display: block;
197+
animation: slideInFromRight 3s forwards;
198+
}
199+
200+
@keyframes slideInFromRight {
201+
0% {
202+
transform: translateX(100%);
203+
opacity: 1;
204+
}
205+
206+
30% {
207+
transform: translateX(0);
208+
opacity: 1;
209+
}
210+
211+
70% {
212+
transform: translateX(0);
213+
opacity: 1;
214+
}
215+
216+
100% {
217+
transform: translateX(0) translateY(-100%);
218+
opacity: 0;
219+
}
220+
}
221+
175222
.dateContainer {
176223
position: relative;
177224
}

identity-service-logs/utils.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,17 @@ async function getUserCount() {
203203
}
204204
}
205205

206-
async function getSelfUser() {
206+
function showToaster(message) {
207+
const toaster = document.querySelector('.toast__message');
208+
toaster.innerText = message;
209+
toaster.classList.add('toast--show');
210+
211+
setTimeout(() => {
212+
toaster.classList.remove('toast--show');
213+
}, 3000);
214+
}
215+
216+
async function getIsSuperUser() {
207217
try {
208218
const res = await fetch(`${API_BASE_URL}/users/self`, {
209219
method: 'GET',
@@ -213,15 +223,27 @@ async function getSelfUser() {
213223
},
214224
});
215225

216-
const self_user = await res.json();
217-
if (self_user?.statusCode !== 200) {
218-
alert('You are not loggedin. Please login!');
219-
window.location.href = '/index.html';
226+
if (res.status === 500) {
227+
showToaster('Something went wrong. Internal Server Error!');
228+
setTimeout(() => {
229+
window.location.href = '/index.html';
230+
}, 3000);
231+
return;
220232
}
221-
return self_user;
233+
234+
if (res.status === 401) {
235+
showToaster('You are not logged-in. Please login!');
236+
setTimeout(() => {
237+
window.location.href = '/index.html';
238+
}, 3000);
239+
return;
240+
}
241+
242+
const self_user = await res.json();
243+
return { isSuperUser: self_user?.roles[SUPER_USER] };
222244
} catch (err) {
223245
console.error('Error in fetching self user ' + err);
224-
return {};
246+
return { error: err, isSuperUser: false };
225247
}
226248
}
227249

@@ -323,4 +345,4 @@ async function fillData(identityLogs, next, prev) {
323345
}
324346
}
325347

326-
export { getIdentityLogs, getSelfUser, fillData, getUserCount };
348+
export { getIdentityLogs, getIsSuperUser, fillData, getUserCount };

0 commit comments

Comments
 (0)