@@ -13,6 +13,7 @@ let nextLink = '';
13
13
let isDataLoading = false ;
14
14
let category = CATEGORY . ALL ;
15
15
let activeIndex = - 1 ;
16
+ let debounceTimeout = null ;
16
17
17
18
const tabsData = [
18
19
{ name : 'All' , 'data-type' : CATEGORY . ALL , class : 'active' } ,
@@ -308,29 +309,29 @@ async function populateActivityFeed(query = {}, newLink) {
308
309
309
310
try {
310
311
isDataLoading = true ;
311
- addLoader ( container ) ;
312
+ addLoader ( activityFeedContainer ) ;
312
313
313
314
const activityFeedData = await getActivityFeedData ( combinedQuery , newLink ) ;
314
315
316
+ if ( ! activityFeedData ) return ;
317
+
315
318
activityFeedContainer . innerHTML = '' ;
316
319
317
- if ( activityFeedData ) {
318
- nextLink = activityFeedData . next ;
319
- const allActivityFeedData = activityFeedData . data ;
320
+ nextLink = activityFeedData . next ;
321
+ const allActivityFeedData = activityFeedData . data ;
320
322
321
- if ( currentVersion !== activityFeedPage ) {
322
- return ;
323
- }
323
+ if ( currentVersion !== activityFeedPage ) {
324
+ return ;
325
+ }
324
326
325
- if ( allActivityFeedData . length === 0 ) {
326
- addEmptyPageMessage ( activityFeedContainer ) ;
327
- return ;
328
- }
327
+ if ( allActivityFeedData . length === 0 ) {
328
+ addEmptyPageMessage ( activityFeedContainer ) ;
329
+ return ;
330
+ }
329
331
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 ) ;
334
335
}
335
336
} catch ( error ) {
336
337
showMessage ( activityFeedContainer , error ) ;
@@ -346,38 +347,32 @@ async function getActivityFeedData(query = {}, nextLink) {
346
347
validateQuery ( query ) ;
347
348
let finalUrl =
348
349
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
+
356
351
try {
357
352
const res = await fetch ( finalUrl , {
358
353
credentials : 'include' ,
354
+ method : 'GET' ,
355
+ headers : {
356
+ 'Content-type' : 'application/json' ,
357
+ } ,
359
358
} ) ;
360
-
361
359
const data = await res . json ( ) ;
362
360
if ( res . ok ) {
363
361
return data ;
364
362
} else {
365
363
switch ( res . status ) {
366
364
case 401 :
367
- return showMessage (
368
- activityFeedContainer ,
369
- ERROR_MESSAGE . UNAUTHENTICATED ,
370
- ) ;
365
+ showMessage ( activityFeedContainer , ERROR_MESSAGE . UNAUTHENTICATED ) ;
366
+ return null ;
371
367
case 403 :
372
- return showMessage ( activityFeedContainer , ERROR_MESSAGE . UNAUTHORIZED ) ;
368
+ showMessage ( activityFeedContainer , ERROR_MESSAGE . UNAUTHORIZED ) ;
369
+ return null ;
373
370
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 ;
378
373
case 400 :
379
374
showMessage ( activityFeedContainer , data . message ) ;
380
- return ;
375
+ return null ;
381
376
default :
382
377
break ;
383
378
}
@@ -454,37 +449,49 @@ async function fetchSuggestions() {
454
449
const query = input . value . trim ( ) ;
455
450
const suggestionBox = document . getElementById ( 'suggestion-box' ) ;
456
451
457
- if ( ! query ) {
452
+ if ( query === '' ) {
458
453
suggestionBox . style . display = 'none' ;
459
454
return ;
460
455
}
461
456
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
+ }
470
463
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
+ }
477
488
} 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 ) ;
481
490
}
482
- } else {
483
- console . error ( 'Error fetching suggestions :' , response . statusText ) ;
491
+ } catch ( error ) {
492
+ console . error ( 'Error:' , error ) ;
484
493
}
485
- } catch ( error ) {
486
- console . error ( 'Error:' , error ) ;
487
- }
494
+ } , 1500 ) ;
488
495
}
489
496
490
497
function renderSuggestions ( users ) {
0 commit comments