Formatted all .ts files
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { CrudFilter, CrudOperators } from "@refinedev/core";
|
||||
|
||||
interface TypedCrudFilter<Obj> {
|
||||
field: keyof Obj;
|
||||
operator: Exclude<CrudOperators, "or" | "and">;
|
||||
value: string[];
|
||||
field: keyof Obj;
|
||||
operator: Exclude<CrudOperators, "or" | "and">;
|
||||
value: string[];
|
||||
}
|
||||
|
||||
export function typeFilters<Obj>(filters: CrudFilter[]): TypedCrudFilter<Obj>[] {
|
||||
return filters as TypedCrudFilter<Obj>[]; // <-- Unsafe cast
|
||||
return filters as TypedCrudFilter<Obj>[]; // <-- Unsafe cast
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -16,12 +16,15 @@ export function typeFilters<Obj>(filters: CrudFilter[]): TypedCrudFilter<Obj>[]
|
||||
* @param field The field to get the filter values for.
|
||||
* @returns An array of filter values for the given field.
|
||||
*/
|
||||
export function getFiltersForField<Obj, Field extends keyof Obj>(filters: TypedCrudFilter<Obj>[], field: Field): string[] {
|
||||
const filterValues: string[] = [];
|
||||
filters.forEach((filter) => {
|
||||
if (filter.field === field) {
|
||||
filterValues.push(...filter.value as string[]);
|
||||
}
|
||||
});
|
||||
return filterValues;
|
||||
export function getFiltersForField<Obj, Field extends keyof Obj>(
|
||||
filters: TypedCrudFilter<Obj>[],
|
||||
field: Field
|
||||
): string[] {
|
||||
const filterValues: string[] = [];
|
||||
filters.forEach((filter) => {
|
||||
if (filter.field === field) {
|
||||
filterValues.push(...(filter.value as string[]));
|
||||
}
|
||||
});
|
||||
return filterValues;
|
||||
}
|
||||
|
||||
@@ -3,74 +3,74 @@ import { CrudFilter, CrudSort } from "@refinedev/core";
|
||||
import { isLocalStorageAvailable } from "./support";
|
||||
|
||||
interface Pagination {
|
||||
current: number;
|
||||
pageSize: number;
|
||||
current: number;
|
||||
pageSize: number;
|
||||
}
|
||||
|
||||
export interface TableState {
|
||||
sorters: CrudSort[];
|
||||
filters: CrudFilter[];
|
||||
pagination: Pagination;
|
||||
showColumns?: string[];
|
||||
sorters: CrudSort[];
|
||||
filters: CrudFilter[];
|
||||
pagination: Pagination;
|
||||
showColumns?: string[];
|
||||
}
|
||||
|
||||
export function useInitialTableState(tableId: string): TableState {
|
||||
const [initialState] = React.useState(() => {
|
||||
const savedSorters = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-sorters`) : null;
|
||||
const savedFilters = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-filters`) : null;
|
||||
const savedPagination = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-pagination`) : null;
|
||||
const savedShowColumns = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-showColumns`) : null;
|
||||
const [initialState] = React.useState(() => {
|
||||
const savedSorters = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-sorters`) : null;
|
||||
const savedFilters = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-filters`) : null;
|
||||
const savedPagination = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-pagination`) : null;
|
||||
const savedShowColumns = isLocalStorageAvailable ? localStorage.getItem(`${tableId}-showColumns`) : null;
|
||||
|
||||
const sorters = savedSorters ? JSON.parse(savedSorters) : [{ field: "id", order: "asc" }];
|
||||
const filters = savedFilters ? JSON.parse(savedFilters) : [];
|
||||
const pagination = savedPagination ? JSON.parse(savedPagination) : { page: 1, pageSize: 20 };
|
||||
const showColumns = savedShowColumns ? JSON.parse(savedShowColumns) : undefined;
|
||||
return { sorters, filters, pagination, showColumns };
|
||||
});
|
||||
return initialState;
|
||||
const sorters = savedSorters ? JSON.parse(savedSorters) : [{ field: "id", order: "asc" }];
|
||||
const filters = savedFilters ? JSON.parse(savedFilters) : [];
|
||||
const pagination = savedPagination ? JSON.parse(savedPagination) : { page: 1, pageSize: 20 };
|
||||
const showColumns = savedShowColumns ? JSON.parse(savedShowColumns) : undefined;
|
||||
return { sorters, filters, pagination, showColumns };
|
||||
});
|
||||
return initialState;
|
||||
}
|
||||
|
||||
export function useStoreInitialState(tableId: string, state: TableState) {
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`${tableId}-sorters`, JSON.stringify(state.sorters));
|
||||
}
|
||||
}, [tableId, state.sorters]);
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`${tableId}-sorters`, JSON.stringify(state.sorters));
|
||||
}
|
||||
}, [tableId, state.sorters]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`${tableId}-filters`, JSON.stringify(state.filters));
|
||||
}
|
||||
}, [tableId, state.filters]);
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`${tableId}-filters`, JSON.stringify(state.filters));
|
||||
}
|
||||
}, [tableId, state.filters]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`${tableId}-pagination`, JSON.stringify(state.pagination));
|
||||
}
|
||||
}, [tableId, state.pagination]);
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`${tableId}-pagination`, JSON.stringify(state.pagination));
|
||||
}
|
||||
}, [tableId, state.pagination]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
if (state.showColumns === undefined) {
|
||||
localStorage.removeItem(`${tableId}-showColumns`);
|
||||
} else {
|
||||
localStorage.setItem(`${tableId}-showColumns`, JSON.stringify(state.showColumns));
|
||||
}
|
||||
}
|
||||
}, [tableId, state.showColumns]);
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
if (state.showColumns === undefined) {
|
||||
localStorage.removeItem(`${tableId}-showColumns`);
|
||||
} else {
|
||||
localStorage.setItem(`${tableId}-showColumns`, JSON.stringify(state.showColumns));
|
||||
}
|
||||
}
|
||||
}, [tableId, state.showColumns]);
|
||||
}
|
||||
|
||||
export function useSavedState<T>(id: string, defaultValue: T) {
|
||||
const [state, setState] = React.useState<T>(() => {
|
||||
const savedState = isLocalStorageAvailable ? localStorage.getItem(`savedStates-${id}`) : null;
|
||||
return savedState ? JSON.parse(savedState) : defaultValue;
|
||||
});
|
||||
const [state, setState] = React.useState<T>(() => {
|
||||
const savedState = isLocalStorageAvailable ? localStorage.getItem(`savedStates-${id}`) : null;
|
||||
return savedState ? JSON.parse(savedState) : defaultValue;
|
||||
});
|
||||
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`savedStates-${id}`, JSON.stringify(state));
|
||||
}
|
||||
}, [id, state]);
|
||||
React.useEffect(() => {
|
||||
if (isLocalStorageAvailable) {
|
||||
localStorage.setItem(`savedStates-${id}`, JSON.stringify(state));
|
||||
}
|
||||
}, [id, state]);
|
||||
|
||||
return [state, setState] as const;
|
||||
return [state, setState] as const;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { CrudSort } from "@refinedev/core";
|
||||
import { SortOrder } from "antd/es/table/interface";
|
||||
|
||||
|
||||
interface TypedCrudSort<Obj> {
|
||||
field: keyof Obj;
|
||||
order: "asc" | "desc";
|
||||
field: keyof Obj;
|
||||
order: "asc" | "desc";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13,14 +12,17 @@ interface TypedCrudSort<Obj> {
|
||||
* @param field The field to get the sort order for.
|
||||
* @returns The sort order for the given field, or undefined if the field is not being sorted.
|
||||
*/
|
||||
export function getSortOrderForField<Obj, Field extends keyof Obj>(sorters: TypedCrudSort<Obj>[], field: Field): SortOrder | undefined {
|
||||
const sorter = sorters.find((s) => s.field === field);
|
||||
if (sorter) {
|
||||
return sorter.order === "asc" ? "ascend" : "descend";
|
||||
}
|
||||
return undefined;
|
||||
export function getSortOrderForField<Obj, Field extends keyof Obj>(
|
||||
sorters: TypedCrudSort<Obj>[],
|
||||
field: Field
|
||||
): SortOrder | undefined {
|
||||
const sorter = sorters.find((s) => s.field === field);
|
||||
if (sorter) {
|
||||
return sorter.order === "asc" ? "ascend" : "descend";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function typeSorters<Obj>(sorters: CrudSort[]): TypedCrudSort<Obj>[] {
|
||||
return sorters as TypedCrudSort<Obj>[]; // <-- Unsafe cast
|
||||
return sorters as TypedCrudSort<Obj>[]; // <-- Unsafe cast
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
|
||||
function _isLocalStorageAvailable(): boolean {
|
||||
try {
|
||||
localStorage.setItem('test', 'test');
|
||||
localStorage.removeItem('test');
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
localStorage.setItem("test", "test");
|
||||
localStorage.removeItem("test");
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export const isLocalStorageAvailable = _isLocalStorageAvailable();
|
||||
|
||||
Reference in New Issue
Block a user