diff --git a/src/components/basic/Table/PageLoadingTable.tsx b/src/components/basic/Table/PageLoadingTable.tsx index 2298e233..6aa4614f 100644 --- a/src/components/basic/Table/PageLoadingTable.tsx +++ b/src/components/basic/Table/PageLoadingTable.tsx @@ -17,9 +17,9 @@ * * SPDX-License-Identifier: Apache-2.0 ********************************************************************************/ -import { Box } from '@mui/material' -import { useState, useEffect } from 'react' -import { LoadMoreButton } from '../Button/LoadMoreButton' +import { Box, CircularProgress } from '@mui/material' +import { useState, useEffect, useCallback } from 'react' +import { Typography } from '../Typography' import { hasMorePages, getMaxRows } from './components/Helper/helper' import { Table, type TableProps } from '.' @@ -52,8 +52,11 @@ export interface PageLoadingTableProps fetchHookRefresh?: number allItems?: Row[] callbackToPage?: (data: PaginResult) => void + allItemsLoadedHint?: string } +const scrollOffset = 350 // Adjust this value for earlier load + export const PageLoadingTable = function ({ loadLabel, fetchHook, @@ -61,6 +64,7 @@ export const PageLoadingTable = function ({ fetchHookRefresh = 0, allItems, callbackToPage, + allItemsLoadedHint = 'All items have been loaded.', ...props }: PageLoadingTableProps) { const [page, setPage] = useState(0) @@ -126,6 +130,25 @@ export const PageLoadingTable = function ({ } }, [isSuccess, isFetching, data, clear, loaded]) + const handleScroll = useCallback(() => { + const scrollableElement = document.documentElement + if ( + scrollableElement.scrollHeight - scrollableElement.scrollTop <= + scrollableElement.clientHeight + scrollOffset + ) { + if (hasMore && !isFetching) { + nextPage() + } + } + }, [hasMore, isFetching]) + + useEffect(() => { + window.addEventListener('scroll', handleScroll) + return () => { + window.removeEventListener('scroll', handleScroll) + } + }, [handleScroll]) + return ( <> ({ rowsCount={items.length} rowsCountMax={maxRows} hideFooter={items.length < (props.rowCount ?? 100)} - loading={loading} + loading={loading && !items.length} error={error} rows={items} reload={refetch} {...props} /> - {items.length > 0 && hasMore ? ( + {/* Display loading spinner while fetching data */} + {items.length > 0 && loading && ( ({ alignItems: 'center', }} > - + - ) : ( - <> + )} + {/* Display message when all items have been loaded */} + {!hasMore && !loading && items.length > 0 && ( + + {allItemsLoadedHint} + )} )