From 1a031903de9ed7c603e7d639fc411152187e55af Mon Sep 17 00:00:00 2001 From: Donkie Date: Tue, 29 Aug 2023 21:45:26 +0200 Subject: [PATCH] Start of serverside client tables --- client/src/components/dataProvider.ts | 29 +++++++++++++++++++++-- client/src/pages/spools/list.tsx | 33 +++++++-------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/client/src/components/dataProvider.ts b/client/src/components/dataProvider.ts index c88ed91..4e93b26 100644 --- a/client/src/components/dataProvider.ts +++ b/client/src/components/dataProvider.ts @@ -13,12 +13,36 @@ const dataProvider = ( Required, "createMany" | "updateMany" | "deleteMany" > => ({ - getList: async ({ resource, meta }) => { + getList: async ({ resource, meta, pagination, sorters, filters }) => { const url = `${apiUrl}/${resource}`; const { headers: headersFromMeta, method, queryParams } = meta ?? {}; 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]( `${url}`, { @@ -29,7 +53,8 @@ const dataProvider = ( return { data, - total: data.length, + total: 100, + //TODO: total: data.length, }; }, diff --git a/client/src/pages/spools/list.tsx b/client/src/pages/spools/list.tsx index e7bae19..5adb287 100644 --- a/client/src/pages/spools/list.tsx +++ b/client/src/pages/spools/list.tsx @@ -26,7 +26,7 @@ const { confirm } = Modal; interface ISpoolCollapsed extends ISpool { filament_name: string; - material?: string; + filament_material?: string; } export const SpoolList: React.FC = () => { @@ -49,16 +49,16 @@ export const SpoolList: React.FC = () => { }, syncWithLocation: false, pagination: { - mode: "off", // Perform pagination in antd's Table instead. Otherwise client-side sorting/filtering doesn't work. + mode: "server", current: initialState.pagination.current, pageSize: initialState.pagination.pageSize, }, sorters: { - mode: "off", // Disable server-side sorting + mode: "server", initial: initialState.sorters, }, filters: { - mode: "off", // Disable server-side filtering + mode: "server", initial: initialState.filters, }, }); @@ -67,7 +67,7 @@ export const SpoolList: React.FC = () => { const allColumns: (keyof ISpoolCollapsed & string)[] = [ "id", "filament_name", - "material", + "filament_material", "used_weight", "remaining_weight", "used_length", @@ -84,10 +84,6 @@ export const SpoolList: React.FC = () => { ); const [showColumns, setShowColumns] = React.useState(initialState.showColumns ?? defaultColumns); - // Type the sorters and filters - const typedSorters = typeSorters(sorters); - const typedFilters = typeFilters(filters); - // Store state in local storage const tableState: TableState = { sorters, @@ -110,18 +106,14 @@ export const SpoolList: React.FC = () => { return { ...element, filament_name, - material: element.filament.material, + filament_material: element.filament.material, }; }), [tableProps.dataSource] ); // Filter and sort the dataSource - const filteredDataSource = React.useMemo(() => { - const filtered = dataSource.filter(genericFilterer(typedFilters)); - filtered.sort(genericSorter(typedSorters)); - return filtered; - }, [dataSource, typedFilters, typedSorters]); + const filteredDataSource = dataSource; // Function for opening an ant design modal that asks for confirmation for archiving a spool const archiveSpool = async (spool: ISpoolCollapsed, archive: boolean) => { @@ -205,15 +197,6 @@ export const SpoolList: React.FC = () => { { - setCurrent(page); - setPageSize(pageSize); - }, - }} rowKey="id" // Make archived rows greyed out onRow={(record) => { @@ -243,7 +226,7 @@ export const SpoolList: React.FC = () => { tableState, })} {FilteredColumn({ - id: "material", + id: "filament_material", i18ncat: "spool", dataSource, tableState,