Client: Spool table is now sortable

This commit is contained in:
Donkie
2023-07-06 15:38:42 +02:00
parent f0403b3afb
commit d0f67d766c
2 changed files with 39 additions and 9 deletions

View File

@@ -13,19 +13,36 @@ import { Table, Space } from "antd";
import { NumberFieldUnit } from "../../components/numberField"; import { NumberFieldUnit } from "../../components/numberField";
import dayjs from "dayjs"; import dayjs from "dayjs";
import utc from "dayjs/plugin/utc"; import utc from "dayjs/plugin/utc";
import { genericSorter } from "../../utils/sorting";
dayjs.extend(utc); dayjs.extend(utc);
export const SpoolList: React.FC<IResourceComponentsProps> = () => { export const SpoolList: React.FC<IResourceComponentsProps> = () => {
const { tableProps } = useTable({ const { tableProps, sorters } = useTable({
syncWithLocation: true, syncWithLocation: true,
pagination: { pagination: {
mode: "client", mode: "client",
pageSize: 20, pageSize: 20,
}, },
sorters: {
mode: "off", // Disable server-side sorting
initial: [
{
field: "id",
order: "asc",
},
],
},
}); });
tableProps.dataSource?.forEach((element) => { // Copy dataSource to avoid mutating the original
const dataSource = [...(tableProps.dataSource || [])];
// Sort dataSource by the sorters
dataSource.sort(genericSorter(sorters));
// Add a filament_name field to the dataSource
dataSource.forEach((element) => {
if ("vendor" in element.filament) { if ("vendor" in element.filament) {
element.filament_name = `${element.filament.vendor.name} - ${element.filament.name}`; element.filament_name = `${element.filament.vendor.name} - ${element.filament.name}`;
} else { } else {
@@ -35,12 +52,22 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
return ( return (
<List> <List>
<Table {...tableProps} rowKey="id"> <Table {...tableProps} dataSource={dataSource} rowKey="id">
<Table.Column dataIndex="id" title="Id" /> <Table.Column
<Table.Column dataIndex="filament_name" title="Filament" /> dataIndex="id"
title="Id"
sorter={true}
defaultSortOrder="ascend"
/>
<Table.Column
dataIndex="filament_name"
title="Filament"
sorter={true}
/>
<Table.Column <Table.Column
dataIndex="used_weight" dataIndex="used_weight"
title="Used Weight" title="Used Weight"
sorter={true}
render={(value) => { render={(value) => {
return ( return (
<NumberFieldUnit <NumberFieldUnit
@@ -56,6 +83,7 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
<Table.Column <Table.Column
dataIndex="remaining_weight" dataIndex="remaining_weight"
title="Estimated Remaining Weight" title="Estimated Remaining Weight"
sorter={true}
render={(value) => { render={(value) => {
if (value === null || value === undefined) { if (value === null || value === undefined) {
return <TextField value="Unknown" />; return <TextField value="Unknown" />;
@@ -71,11 +99,12 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
); );
}} }}
/> />
<Table.Column dataIndex="location" title="Location" /> <Table.Column dataIndex="location" title="Location" sorter={true} />
<Table.Column dataIndex="lot_nr" title="Lot Nr" /> <Table.Column dataIndex="lot_nr" title="Lot Nr" sorter={true} />
<Table.Column <Table.Column
dataIndex={["first_used"]} dataIndex={["first_used"]}
title="First Used" title="First Used"
sorter={true}
render={(value) => ( render={(value) => (
<DateField <DateField
hidden={!value} hidden={!value}
@@ -88,6 +117,7 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
<Table.Column <Table.Column
dataIndex={["last_used"]} dataIndex={["last_used"]}
title="Last Used" title="Last Used"
sorter={true}
render={(value) => ( render={(value) => (
<DateField <DateField
hidden={!value} hidden={!value}
@@ -97,7 +127,7 @@ export const SpoolList: React.FC<IResourceComponentsProps> = () => {
/> />
)} )}
/> />
<Table.Column dataIndex={["comment"]} title="Comment" /> <Table.Column dataIndex={["comment"]} title="Comment" sorter={true} />
<Table.Column <Table.Column
title="Actions" title="Actions"
dataIndex="actions" dataIndex="actions"

View File

@@ -1,7 +1,7 @@
import { IFilament } from "../filaments/model"; import { IFilament } from "../filaments/model";
export interface ISpool { export interface ISpool {
id: string; id: number;
registered: string; registered: string;
first_used?: string; first_used?: string;
last_used?: string; last_used?: string;