diff --git a/src/table/pagedTable/pagedTable.tsx b/src/table/pagedTable/pagedTable.tsx index 29249c090..77dc9917a 100644 --- a/src/table/pagedTable/pagedTable.tsx +++ b/src/table/pagedTable/pagedTable.tsx @@ -28,6 +28,7 @@ export interface IPagedTableProps extends IDefaultPagedTableImpl, IPagedTa // pass renderProps like: export interface IPagedTableHook { reloadData: () => void + reloadFirstPage?: () => void isLoading: boolean renderProps: IDefaultPagedTableImpl totalRows: number diff --git a/src/table/pagedTable/smoothPagedTable.tsx b/src/table/pagedTable/smoothPagedTable.tsx index 0ed967d6d..6c3e2501a 100644 --- a/src/table/pagedTable/smoothPagedTable.tsx +++ b/src/table/pagedTable/smoothPagedTable.tsx @@ -89,24 +89,26 @@ export function useSmoothPagedTable(props: IUsePagedTableProps) { const [isLoading, setIsLoading] = React.useState(false) // load data - const loadAPI = React.useCallback(async () => { + const loadAPI = React.useCallback(async (pageNumber?: number) => { // fetch data from paginated API try { + const pageToLoad = pageNumber || page + // used to refresh the current page to show the new values if (lastPage && page + 1 > lastPage) return setIsLoading(true) // TablePagination is zero-indexed, API is not - const res = await apiCall({ page: page + 1, pageSize, queryParams: queryParams }) - + const res = await apiCall({ page: pageToLoad + 1, pageSize, queryParams: queryParams }) setRows(prev => ({ ...prev, - [page]: res.rows, + [pageToLoad]: res.rows, })) setLastPage(res.last_page) - + // if pageNumber is provided, we are refreshing this page, so the current page + // has to be set to the page that we are refreshing + if (pageNumber) setPage(pageNumber) // set newPage as current page // someone maybe scrolling like crazy so let's always remember last page - - if (res.page) setPage(prevPage => Math.max(prevPage, res.page)) + else if (res.page) setPage(prevPage => Math.max(prevPage, res.page)) else setPage(0) } catch (err) { setError(err) @@ -115,6 +117,10 @@ export function useSmoothPagedTable(props: IUsePagedTableProps) { } }, [setLastPage, lastPage, apiCall, page, pageSize, queryParams, setIsLoading, setError]) + const reloadFirstPage = React.useCallback(async() => { + loadAPI(0) + }, [setLastPage, apiCall, setIsLoading, setRows, setPage]) + // load on component mount React.useEffect(() => { if (autoLoad && !error) loadAPI() @@ -133,6 +139,7 @@ export function useSmoothPagedTable(props: IUsePagedTableProps) { () => ({ isLoading, reloadData: loadAPI, + reloadFirstPage, page, renderProps: { rows: Object.values(rows).reduce((acc, val) => acc.concat(val), []), @@ -145,6 +152,6 @@ export function useSmoothPagedTable(props: IUsePagedTableProps) { pagedDataContext, }, }), - [isLoading, loadAPI, rows, page, setPage, pageSize, setPageSize, handleDidScrollToEnd, pagedDataContext] + [isLoading, loadAPI, reloadFirstPage, rows, page, setPage, pageSize, setPageSize, handleDidScrollToEnd, pagedDataContext] ) }