Skip to content

Commit 6a76498

Browse files
authored
Fix activity feed (#944)
* chore: fix activity feed * chore: fix alignment
1 parent c74e1f4 commit 6a76498

File tree

3 files changed

+72
-56
lines changed

3 files changed

+72
-56
lines changed

feed/script.js

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let nextLink = '';
1313
let isDataLoading = false;
1414
let category = CATEGORY.ALL;
1515
let activeIndex = -1;
16+
let debounceTimeout = null;
1617

1718
const tabsData = [
1819
{ name: 'All', 'data-type': CATEGORY.ALL, class: 'active' },
@@ -308,29 +309,29 @@ async function populateActivityFeed(query = {}, newLink) {
308309

309310
try {
310311
isDataLoading = true;
311-
addLoader(container);
312+
addLoader(activityFeedContainer);
312313

313314
const activityFeedData = await getActivityFeedData(combinedQuery, newLink);
314315

316+
if (!activityFeedData) return;
317+
315318
activityFeedContainer.innerHTML = '';
316319

317-
if (activityFeedData) {
318-
nextLink = activityFeedData.next;
319-
const allActivityFeedData = activityFeedData.data;
320+
nextLink = activityFeedData.next;
321+
const allActivityFeedData = activityFeedData.data;
320322

321-
if (currentVersion !== activityFeedPage) {
322-
return;
323-
}
323+
if (currentVersion !== activityFeedPage) {
324+
return;
325+
}
324326

325-
if (allActivityFeedData.length === 0) {
326-
addEmptyPageMessage(activityFeedContainer);
327-
return;
328-
}
327+
if (allActivityFeedData.length === 0) {
328+
addEmptyPageMessage(activityFeedContainer);
329+
return;
330+
}
329331

330-
for (const data of allActivityFeedData) {
331-
const renderedItem = renderActivityItem(data);
332-
activityFeedContainer.appendChild(renderedItem);
333-
}
332+
for (const data of allActivityFeedData) {
333+
const renderedItem = renderActivityItem(data);
334+
activityFeedContainer.appendChild(renderedItem);
334335
}
335336
} catch (error) {
336337
showMessage(activityFeedContainer, error);
@@ -346,38 +347,32 @@ async function getActivityFeedData(query = {}, nextLink) {
346347
validateQuery(query);
347348
let finalUrl =
348349
API_BASE_URL + (nextLink || '/logs' + generateActivityFeedParams(query));
349-
const res = await fetch(finalUrl, {
350-
credentials: 'include',
351-
method: 'GET',
352-
headers: {
353-
'Content-type': 'application/json',
354-
},
355-
});
350+
356351
try {
357352
const res = await fetch(finalUrl, {
358353
credentials: 'include',
354+
method: 'GET',
355+
headers: {
356+
'Content-type': 'application/json',
357+
},
359358
});
360-
361359
const data = await res.json();
362360
if (res.ok) {
363361
return data;
364362
} else {
365363
switch (res.status) {
366364
case 401:
367-
return showMessage(
368-
activityFeedContainer,
369-
ERROR_MESSAGE.UNAUTHENTICATED,
370-
);
365+
showMessage(activityFeedContainer, ERROR_MESSAGE.UNAUTHENTICATED);
366+
return null;
371367
case 403:
372-
return showMessage(activityFeedContainer, ERROR_MESSAGE.UNAUTHORIZED);
368+
showMessage(activityFeedContainer, ERROR_MESSAGE.UNAUTHORIZED);
369+
return null;
373370
case 404:
374-
return showMessage(
375-
activityFeedContainer,
376-
ERROR_MESSAGE.LOGS_NOT_FOUND,
377-
);
371+
showMessage(activityFeedContainer, ERROR_MESSAGE.LOGS_NOT_FOUND);
372+
return null;
378373
case 400:
379374
showMessage(activityFeedContainer, data.message);
380-
return;
375+
return null;
381376
default:
382377
break;
383378
}
@@ -454,37 +449,49 @@ async function fetchSuggestions() {
454449
const query = input.value.trim();
455450
const suggestionBox = document.getElementById('suggestion-box');
456451

457-
if (!query) {
452+
if (query === '') {
458453
suggestionBox.style.display = 'none';
459454
return;
460455
}
461456

462-
try {
463-
const response = await fetch(`${API_BASE_URL}/users?search=${query}`, {
464-
method: 'GET',
465-
credentials: 'include',
466-
headers: {
467-
'Content-Type': 'application/json',
468-
},
469-
});
457+
if (/^\d/.test(query)) {
458+
suggestionBox.innerHTML =
459+
'<div class="suggestion-item">Please enter a valid username</div>';
460+
suggestionBox.style.display = 'block';
461+
return;
462+
}
470463

471-
if (response.ok) {
472-
const data = await response.json();
473-
const users = data.users || [];
474-
if (users.length > 0) {
475-
renderSuggestions(users);
476-
suggestionBox.style.display = 'block';
464+
clearTimeout(debounceTimeout);
465+
466+
debounceTimeout = setTimeout(async () => {
467+
try {
468+
const response = await fetch(`${API_BASE_URL}/users?search=${query}`, {
469+
method: 'GET',
470+
credentials: 'include',
471+
headers: {
472+
'Content-Type': 'application/json',
473+
},
474+
});
475+
476+
if (response.ok) {
477+
const data = await response.json();
478+
const users = data.users || [];
479+
480+
if (users.length > 0) {
481+
renderSuggestions(users);
482+
suggestionBox.style.display = 'block';
483+
} else {
484+
suggestionBox.innerHTML =
485+
'<div class="suggestion-item">No users found</div>';
486+
suggestionBox.style.display = 'block';
487+
}
477488
} else {
478-
suggestionBox.innerHTML =
479-
'<div class="suggestion-item">No users found</div>';
480-
suggestionBox.style.display = 'block';
489+
console.error('Error fetching suggestions:', response.statusText);
481490
}
482-
} else {
483-
console.error('Error fetching suggestions:', response.statusText);
491+
} catch (error) {
492+
console.error('Error:', error);
484493
}
485-
} catch (error) {
486-
console.error('Error:', error);
487-
}
494+
}, 1500);
488495
}
489496

490497
function renderSuggestions(users) {

feed/style.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ body {
235235
display: none;
236236
border-radius: 0.5rem;
237237
margin-top: 0.4rem;
238+
min-height: 3rem;
238239
}
239240

240241
.suggestion-item {
@@ -246,6 +247,7 @@ body {
246247
transition: background-color 0.2s;
247248
border-radius: 0.5rem;
248249
height: 2rem;
250+
margin-top: 0.2rem;
249251
}
250252

251253
.suggestion-item:hover {
@@ -269,6 +271,10 @@ body {
269271
border-radius: 50%;
270272
}
271273

274+
.error-message {
275+
text-align: center;
276+
}
277+
272278
@media (max-width: 480px) {
273279
.filters,
274280
.filter-row {

feed/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ function addEmptyPageMessage(container) {
5353
}
5454

5555
function showMessage(container, errorMsg) {
56+
container.innerHTML = '';
57+
5658
if (errorMsg) {
5759
const errorHeading = createElement({
5860
type: 'h4',
61+
attributes: { class: 'error-message' },
5962
innerText: errorMsg,
6063
});
6164

0 commit comments

Comments
 (0)