Skip to content

Commit

Permalink
Fixed formatting in saveload.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Donkie committed Oct 24, 2023
1 parent e52675e commit 9b823af
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions client/src/utils/saveload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ export interface TableState {

export function useInitialTableState(tableId: string): TableState {
const [initialState] = React.useState(() => {
const savedSorters = hasHashProperty('sorters') ? getHashProperty('sorters') : isLocalStorageAvailable ? localStorage.getItem(`${tableId}-sorters`) : null;
const savedFilters = hasHashProperty('filters') ? getHashProperty('filters') : isLocalStorageAvailable ? localStorage.getItem(`${tableId}-filters`) : null;
const savedPagination = hasHashProperty('pagination') ? getHashProperty('pagination') : isLocalStorageAvailable ? localStorage.getItem(`${tableId}-pagination`) : null;
const savedSorters = hasHashProperty("sorters")
? getHashProperty("sorters")
: isLocalStorageAvailable
? localStorage.getItem(`${tableId}-sorters`)
: null;
const savedFilters = hasHashProperty("filters")
? getHashProperty("filters")
: isLocalStorageAvailable
? localStorage.getItem(`${tableId}-filters`)
: null;
const savedPagination = hasHashProperty("pagination")
? getHashProperty("pagination")
: isLocalStorageAvailable
? localStorage.getItem(`${tableId}-pagination`)
: null;
const savedShowColumns = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-showColumns`) : null;

const sorters = savedSorters ? JSON.parse(savedSorters) : [{ field: "id", order: "asc" }];
Expand All @@ -37,20 +49,20 @@ export function useStoreInitialState(tableId: string, state: TableState) {
}
setURLHash(`sorters`, JSON.stringify(state.sorters));
} else {
localStorage.removeItem(`${tableId}-sorters`)
removeURLHash('sorters');
localStorage.removeItem(`${tableId}-sorters`);
removeURLHash("sorters");
}
}, [tableId, state.sorters]);

React.useEffect(() => {
let filters = state.filters.filter(f => f.value.length != 0);
const filters = state.filters.filter((f) => f.value.length != 0);
if (filters.length > 0) {
if (isLocalStorageAvailable) {
localStorage.setItem(`${tableId}-filters`, JSON.stringify(filters));
setURLHash('filters', JSON.stringify(state.filters));
setURLHash("filters", JSON.stringify(state.filters));
}
} else {
localStorage.removeItem(`${tableId}-filters`)
localStorage.removeItem(`${tableId}-filters`);
removeURLHash(`filters`);
}
}, [tableId, state.filters]);
Expand All @@ -62,10 +74,9 @@ export function useStoreInitialState(tableId: string, state: TableState) {
}
setURLHash(`pagination`, JSON.stringify(state.pagination));
} else {
localStorage.removeItem(`${tableId}-pagination`)
localStorage.removeItem(`${tableId}-pagination`);
removeURLHash(`pagination`);
}

}, [tableId, state.pagination]);

React.useEffect(() => {
Expand All @@ -76,7 +87,6 @@ export function useStoreInitialState(tableId: string, state: TableState) {
localStorage.setItem(`${tableId}-showColumns`, JSON.stringify(state.showColumns));
}
}

}, [tableId, state.showColumns]);
}

Expand All @@ -98,15 +108,15 @@ export function useSavedState<T>(id: string, defaultValue: T) {
function setURLHash(Id: string, value: string) {
const params = new URLSearchParams(window.location.hash.substring(1));
if (!params.has(Id)) {
params.append(Id, value)
params.append(Id, value);
}
params.set(Id, value);
window.location.hash = params.toString();
}
function removeURLHash(Id: string) {
const params = new URLSearchParams(window.location.hash.substring(1));
if (params.has(Id)) {
params.delete(Id)
params.delete(Id);
}
window.location.hash = params.toString();
}
Expand All @@ -119,4 +129,4 @@ function getHashProperty(Id: string) {
function hasHashProperty(property: string): boolean {
const hash = new URLSearchParams(window.location.hash.substring(1));
return hash.has(property);
}
}

0 comments on commit 9b823af

Please sign in to comment.