make url hash always represent current table state
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { CrudFilter, CrudSort } from "@refinedev/core";
|
import { CrudFilter, CrudSort } from "@refinedev/core";
|
||||||
import { isLocalStorageAvailable } from "./support";
|
import { isLocalStorageAvailable } from "./support";
|
||||||
|
|
||||||
interface Pagination {
|
interface Pagination {
|
||||||
current: number;
|
current: number;
|
||||||
pageSize: number;
|
pageSize: number;
|
||||||
@@ -16,10 +15,10 @@ export interface TableState {
|
|||||||
|
|
||||||
export function useInitialTableState(tableId: string): TableState {
|
export function useInitialTableState(tableId: string): TableState {
|
||||||
const [initialState] = React.useState(() => {
|
const [initialState] = React.useState(() => {
|
||||||
const savedSorters = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-sorters`) : null;
|
const savedSorters = hasHashProperty(`${tableId}-sorters`) ? getHashProperty(`${tableId}-sorters`) : isLocalStorageAvailable ? localStorage.getItem(`${tableId}-sorters`) : null;
|
||||||
const savedFilters = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-filters`) : null;
|
const savedFilters = hasHashProperty(`${tableId}-filters`) ? getHashProperty(`${tableId}-filters`) : isLocalStorageAvailable ? localStorage.getItem(`${tableId}-filters`) : null;
|
||||||
const savedPagination = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-pagination`) : null;
|
const savedPagination = hasHashProperty(`${tableId}-pagination`) ? getHashProperty(`${tableId}-pagination`) : isLocalStorageAvailable ? localStorage.getItem(`${tableId}-pagination`) : null;
|
||||||
const savedShowColumns = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-showColumns`) : null;
|
const savedShowColumns = hasHashProperty(`${tableId}-showColumns`) ? getHashProperty(`${tableId}-showColumns`) : isLocalStorageAvailable ? localStorage.getItem(`${tableId}-showColumns`) : null;
|
||||||
|
|
||||||
const sorters = savedSorters ? JSON.parse(savedSorters) : [{ field: "id", order: "asc" }];
|
const sorters = savedSorters ? JSON.parse(savedSorters) : [{ field: "id", order: "asc" }];
|
||||||
const filters = savedFilters ? JSON.parse(savedFilters) : [];
|
const filters = savedFilters ? JSON.parse(savedFilters) : [];
|
||||||
@@ -35,28 +34,44 @@ export function useStoreInitialState(tableId: string, state: TableState) {
|
|||||||
if (isLocalStorageAvailable) {
|
if (isLocalStorageAvailable) {
|
||||||
localStorage.setItem(`${tableId}-sorters`, JSON.stringify(state.sorters));
|
localStorage.setItem(`${tableId}-sorters`, JSON.stringify(state.sorters));
|
||||||
}
|
}
|
||||||
|
if (JSON.stringify(state.sorters) != JSON.stringify([{ field: "id", order: "asc" }])) {
|
||||||
|
updateURLHash(`${tableId}-sorters`, JSON.stringify(state.sorters));
|
||||||
|
}
|
||||||
}, [tableId, state.sorters]);
|
}, [tableId, state.sorters]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (isLocalStorageAvailable) {
|
if (isLocalStorageAvailable) {
|
||||||
localStorage.setItem(`${tableId}-filters`, JSON.stringify(state.filters));
|
localStorage.setItem(`${tableId}-filters`, JSON.stringify(state.filters));
|
||||||
}
|
}
|
||||||
|
if (JSON.stringify(state.filters) != JSON.stringify([])) {
|
||||||
|
updateURLHash(`${tableId}-filters`, JSON.stringify(state.filters));
|
||||||
|
}
|
||||||
}, [tableId, state.filters]);
|
}, [tableId, state.filters]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (isLocalStorageAvailable) {
|
if (isLocalStorageAvailable) {
|
||||||
localStorage.setItem(`${tableId}-pagination`, JSON.stringify(state.pagination));
|
localStorage.setItem(`${tableId}-pagination`, JSON.stringify(state.pagination));
|
||||||
}
|
}
|
||||||
|
if (JSON.stringify(state.pagination) != JSON.stringify({ current: 1, pageSize: 20 })) {
|
||||||
|
updateURLHash(`${tableId}-pagination`, JSON.stringify(state.pagination));
|
||||||
|
}
|
||||||
|
|
||||||
}, [tableId, state.pagination]);
|
}, [tableId, state.pagination]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (isLocalStorageAvailable) {
|
if (isLocalStorageAvailable) {
|
||||||
if (state.showColumns === undefined) {
|
if (state.showColumns === undefined) {
|
||||||
localStorage.removeItem(`${tableId}-showColumns`);
|
localStorage.removeItem(`${tableId}-showColumns`);
|
||||||
|
const params = new URLSearchParams(window.location.hash.substring(1));
|
||||||
|
if (params.has(`${tableId}-showColumns`)) {
|
||||||
|
params.delete(`${tableId}-showColumns`)
|
||||||
|
}
|
||||||
|
window.location.hash = params.toString();
|
||||||
} else {
|
} else {
|
||||||
localStorage.setItem(`${tableId}-showColumns`, JSON.stringify(state.showColumns));
|
localStorage.setItem(`${tableId}-showColumns`, JSON.stringify(state.showColumns));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}, [tableId, state.showColumns]);
|
}, [tableId, state.showColumns]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,3 +89,22 @@ export function useSavedState<T>(id: string, defaultValue: T) {
|
|||||||
|
|
||||||
return [state, setState] as const;
|
return [state, setState] as const;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function updateURLHash(Id: string, value: string) {
|
||||||
|
const params = new URLSearchParams(window.location.hash.substring(1));
|
||||||
|
if (!params.has(Id)) {
|
||||||
|
params.append(Id, value)
|
||||||
|
}
|
||||||
|
params.set(Id, value);
|
||||||
|
window.location.hash = params.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHashProperty(Id: string) {
|
||||||
|
const hash = new URLSearchParams(window.location.hash.substring(1));
|
||||||
|
return hash.get(Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasHashProperty(property: string): boolean {
|
||||||
|
const hash = new URLSearchParams(window.location.hash.substring(1));
|
||||||
|
return hash.has(property);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user