@@ -266,18 +266,54 @@ async function getUser(userId) {
266
266
return user ;
267
267
}
268
268
269
- async function fillData ( identityLogs , next , prev ) {
269
+ let isLoading = false ;
270
+ let nextLink = null ;
271
+
272
+ async function loadMoreLogs ( ) {
273
+ if ( ! nextLink || isLoading ) {
274
+ return ;
275
+ }
276
+ const footer = document . querySelector ( 'footer' ) ;
277
+
278
+ if ( footer ) {
279
+ footer . style . display = 'none' ;
280
+ }
281
+ isLoading = true ;
282
+
283
+ try {
284
+ document . getElementById ( 'loader' ) . style . display = 'block' ;
285
+ const { identityLogs, next } = await getIdentityLogs ( nextLink ) ;
286
+ await fillData ( identityLogs , next ) ;
287
+ } catch ( error ) {
288
+ console . error ( 'Error loading logs:' , error ) ;
289
+ } finally {
290
+ document . getElementById ( 'loader' ) . style . display = 'none' ;
291
+ isLoading = false ;
292
+
293
+ if ( footer ) {
294
+ footer . style . display = 'block' ;
295
+ }
296
+ }
297
+ }
298
+
299
+ const addIntersectionObserver = ( ) => {
300
+ const lastElementContainer = document . getElementById ( 'page_bottom_element' ) ;
301
+ const intersectionObserver = new IntersectionObserver ( async ( entries ) => {
302
+ if ( entries [ 0 ] . isIntersecting && ! isLoading ) {
303
+ await loadMoreLogs ( ) ;
304
+ }
305
+ } ) ;
306
+ intersectionObserver . observe ( lastElementContainer ) ;
307
+ } ;
308
+
309
+ async function fillData ( identityLogs , next ) {
270
310
if ( identityLogs === undefined || identityLogs . length === 0 ) {
271
311
document . getElementById ( 'loader' ) . innerHTML =
272
312
'No Identity Service Logs found !!!' ;
273
313
} else {
274
- const wrapper = createCardComponent ( {
275
- tagName : 'div' ,
276
- className : 'wrapperDiv' ,
277
- } ) ;
278
-
279
- const footerDiv = document . querySelector ( '#footer' ) ;
280
- document . body . insertBefore ( wrapper , footerDiv ) ;
314
+ const wrapper = document . querySelector ( '.wrapperDiv' ) ;
315
+ const loader = document . querySelector ( '#loader' ) ;
316
+ document . body . insertBefore ( wrapper , loader ) ;
281
317
document . getElementById ( 'loader' ) . style . display = 'none' ;
282
318
283
319
for ( const identityLog of identityLogs ) {
@@ -297,53 +333,14 @@ async function fillData(identityLogs, next, prev) {
297
333
) ;
298
334
}
299
335
}
300
-
301
- const buttonContainer = createCardComponent ( {
302
- tagName : 'div' ,
303
- className : 'buttonContainer' ,
304
- parent : wrapper ,
305
- } ) ;
306
-
307
- if ( prev ) {
308
- const prevButton = createCardComponent ( {
309
- tagName : 'button' ,
310
- className : 'navigation-button' ,
311
- parent : buttonContainer ,
312
- innerText : 'Prev' ,
313
- } ) ;
314
-
315
- prevButton . onclick = async ( ) => {
316
- wrapper . remove ( ) ;
317
- document . getElementById ( 'loader' ) . style . display = 'block' ;
318
- const {
319
- identityLogs,
320
- next,
321
- prev : prevLink ,
322
- } = await getIdentityLogs ( prev ) ;
323
- fillData ( identityLogs , next , prevLink ) ;
324
- } ;
325
- }
326
-
327
- if ( next ) {
328
- const nextButton = createCardComponent ( {
329
- tagName : 'button' ,
330
- className : 'navigation-button' ,
331
- parent : buttonContainer ,
332
- innerText : 'Next' ,
333
- } ) ;
334
-
335
- nextButton . onclick = async ( ) => {
336
- wrapper . remove ( ) ;
337
- document . getElementById ( 'loader' ) . style . display = 'block' ;
338
- const {
339
- identityLogs,
340
- next : nextLink ,
341
- prev,
342
- } = await getIdentityLogs ( next ) ;
343
- fillData ( identityLogs , nextLink , prev ) ;
344
- } ;
345
- }
336
+ nextLink = next ;
346
337
}
347
338
}
348
339
349
- export { getIdentityLogs , getIsSuperUser , fillData , getUserCount } ;
340
+ export {
341
+ getIdentityLogs ,
342
+ getIsSuperUser ,
343
+ fillData ,
344
+ getUserCount ,
345
+ addIntersectionObserver ,
346
+ } ;
0 commit comments