store current table page in URL

This commit is contained in:
Sidney Alcantara
2022-06-03 13:50:20 +10:00
parent 34a49c3a93
commit 726f4c12f2

View File

@@ -1,5 +1,5 @@
import { atom } from "jotai";
import { atomWithReducer } from "jotai/utils";
import { atomWithReducer, atomWithHash } from "jotai/utils";
import {
uniqBy,
sortBy,
@@ -68,19 +68,21 @@ export const tableFiltersAtom = atom<TableFilter[]>([]);
/** Orders applied to the local view */
export const tableOrdersAtom = atom<TableOrder[]>([]);
/** Store current page in URL */
export const tablePageHashAtom = atomWithHash("page", 0);
/**
* Set the page for the table query. Stops updating if weve loaded all rows.
*/
export const tablePageAtom = atom(
0,
(get) => get(tablePageHashAtom),
(get, set, update: number | ((p: number) => number)) => {
// If loading more or doesnt have next page, dont request another page
const tableNextPage = get(tableNextPageAtom);
if (tableNextPage.loading || !tableNextPage.available) return;
const currentPage = get(tablePageAtom);
const currentPage = get(tablePageHashAtom);
set(
tablePageAtom,
tablePageHashAtom,
typeof update === "number" ? update : update(currentPage)
);
}