Start of serverside client tables

This commit is contained in:
Donkie
2023-08-29 21:45:26 +02:00
parent e2d937aaf1
commit 1a031903de
2 changed files with 35 additions and 27 deletions

View File

@@ -13,12 +13,36 @@ const dataProvider = (
Required<DataProvider>, Required<DataProvider>,
"createMany" | "updateMany" | "deleteMany" "createMany" | "updateMany" | "deleteMany"
> => ({ > => ({
getList: async ({ resource, meta }) => { getList: async ({ resource, meta, pagination, sorters, filters }) => {
const url = `${apiUrl}/${resource}`; const url = `${apiUrl}/${resource}`;
const { headers: headersFromMeta, method, queryParams } = meta ?? {}; const { headers: headersFromMeta, method, queryParams } = meta ?? {};
const requestMethod = (method as MethodTypes) ?? "get"; const requestMethod = (method as MethodTypes) ?? "get";
if (pagination && pagination.mode == "server") {
const pageSize = pagination.pageSize ?? 10;
const offset = ((pagination.current ?? 1) - 1) * pageSize;
queryParams["limit"] = pageSize;
queryParams["offset"] = offset;
}
if (sorters && sorters.length > 0) {
queryParams["sort"] = sorters.map((sort) => {
const field = sort.field.replace("_", ".")
return `${field}:${sort.order}`;
}).join(",")
}
if (filters && filters.length > 0) {
filters.forEach(filter => {
if (!("field" in filter)) {
throw Error("Filter must be a LogicalFilter.")
}
const field = filter.field.replace(".", "_")
queryParams[field] = filter.value
});
}
const { data } = await httpClient[requestMethod]( const { data } = await httpClient[requestMethod](
`${url}`, `${url}`,
{ {
@@ -29,7 +53,8 @@ const dataProvider = (
return { return {
data, data,
total: data.length, total: 100,
//TODO: total: data.length,
}; };
}, },

View File

@@ -26,7 +26,7 @@ const { confirm } = Modal;
interface ISpoolCollapsed extends ISpool { interface ISpoolCollapsed extends ISpool {
filament_name: string; filament_name: string;
material?: string; filament_material?: string;
} }
export const SpoolList: React.FC<IResourceComponentsProps> = () => { export const SpoolList: React.FC<IResourceComponentsProps> = () => {
@@ -49,16 +49,16 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
}, },
syncWithLocation: false, syncWithLocation: false,
pagination: { pagination: {
mode: "off", // Perform pagination in antd's Table instead. Otherwise client-side sorting/filtering doesn't work. mode: "server",
current: initialState.pagination.current, current: initialState.pagination.current,
pageSize: initialState.pagination.pageSize, pageSize: initialState.pagination.pageSize,
}, },
sorters: { sorters: {
mode: "off", // Disable server-side sorting mode: "server",
initial: initialState.sorters, initial: initialState.sorters,
}, },
filters: { filters: {
mode: "off", // Disable server-side filtering mode: "server",
initial: initialState.filters, initial: initialState.filters,
}, },
}); });
@@ -67,7 +67,7 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
const allColumns: (keyof ISpoolCollapsed & string)[] = [ const allColumns: (keyof ISpoolCollapsed & string)[] = [
"id", "id",
"filament_name", "filament_name",
"material", "filament_material",
"used_weight", "used_weight",
"remaining_weight", "remaining_weight",
"used_length", "used_length",
@@ -84,10 +84,6 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
); );
const [showColumns, setShowColumns] = React.useState<string[]>(initialState.showColumns ?? defaultColumns); const [showColumns, setShowColumns] = React.useState<string[]>(initialState.showColumns ?? defaultColumns);
// Type the sorters and filters
const typedSorters = typeSorters<ISpoolCollapsed>(sorters);
const typedFilters = typeFilters<ISpoolCollapsed>(filters);
// Store state in local storage // Store state in local storage
const tableState: TableState = { const tableState: TableState = {
sorters, sorters,
@@ -110,18 +106,14 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
return { return {
...element, ...element,
filament_name, filament_name,
material: element.filament.material, filament_material: element.filament.material,
}; };
}), }),
[tableProps.dataSource] [tableProps.dataSource]
); );
// Filter and sort the dataSource // Filter and sort the dataSource
const filteredDataSource = React.useMemo(() => { const filteredDataSource = dataSource;
const filtered = dataSource.filter(genericFilterer(typedFilters));
filtered.sort(genericSorter(typedSorters));
return filtered;
}, [dataSource, typedFilters, typedSorters]);
// Function for opening an ant design modal that asks for confirmation for archiving a spool // Function for opening an ant design modal that asks for confirmation for archiving a spool
const archiveSpool = async (spool: ISpoolCollapsed, archive: boolean) => { const archiveSpool = async (spool: ISpoolCollapsed, archive: boolean) => {
@@ -205,15 +197,6 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
<Table <Table
{...tableProps} {...tableProps}
dataSource={filteredDataSource} dataSource={filteredDataSource}
pagination={{
showSizeChanger: true,
current: current,
pageSize: pageSize,
onChange: (page, pageSize) => {
setCurrent(page);
setPageSize(pageSize);
},
}}
rowKey="id" rowKey="id"
// Make archived rows greyed out // Make archived rows greyed out
onRow={(record) => { onRow={(record) => {
@@ -243,7 +226,7 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
tableState, tableState,
})} })}
{FilteredColumn({ {FilteredColumn({
id: "material", id: "filament_material",
i18ncat: "spool", i18ncat: "spool",
dataSource, dataSource,
tableState, tableState,