Skip to content

Commit

Permalink
[PUI] Column Refactoring (#7242)
Browse files Browse the repository at this point in the history
* Refactor column helpers

* Make reference column switchable in BomTable

* Make 'accessor' a required field againt

* Update props

* Fix c0d3
  • Loading branch information
SchrodingersGat committed May 17, 2024
1 parent 2a83c19 commit acb1ec4
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 135 deletions.
14 changes: 9 additions & 5 deletions src/frontend/src/tables/Column.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* Interface for the table column definition
*/
export type TableColumn<T = any> = {
accessor: string; // The key in the record to access
export type TableColumnProps<T = any> = {
accessor?: string; // The key in the record to access
title?: string; // The title of the column - Note: this may be supplied by the API, and is not required, but it can be overridden if required
ordering?: string; // The key in the record to sort by (defaults to accessor)
sortable?: boolean; // Whether the column is sortable
Expand All @@ -18,3 +15,10 @@ export type TableColumn<T = any> = {
cellsStyle?: any; // The style of the cells in the column
extra?: any; // Extra data to pass to the render function
};

/**
* Interface for the table column definition
*/
export type TableColumn<T = any> = {
accessor: string; // The key in the record to access
} & TableColumnProps<T>;
163 changes: 62 additions & 101 deletions src/frontend/src/tables/ColumnRenderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { formatCurrency, renderDate } from '../defaults/formatters';
import { ModelType } from '../enums/ModelType';
import { resolveItem } from '../functions/conversion';
import { cancelEvent } from '../functions/events';
import { TableColumn } from './Column';
import { TableColumn, TableColumnProps } from './Column';
import { ProjectCodeHoverCard } from './TableHoverCard';

// Render a Part instance within a table
Expand All @@ -26,24 +26,14 @@ export function PartColumn(part: any, full_name?: boolean) {
);
}

export function LocationColumn({
accessor,
title,
sortable,
ordering
}: {
accessor: string;
title?: string;
sortable?: boolean;
ordering?: string;
}): TableColumn {
export function LocationColumn(props: TableColumnProps): TableColumn {
return {
accessor: accessor,
title: title ?? t`Location`,
sortable: sortable ?? true,
ordering: ordering ?? 'location',
accessor: 'location',
title: t`Location`,
sortable: true,
ordering: 'location',
render: (record: any) => {
let location = resolveItem(record, accessor);
let location = resolveItem(record, props.accessor ?? '');

if (!location) {
return (
Expand All @@ -52,62 +42,38 @@ export function LocationColumn({
}

return <Text>{location.name}</Text>;
}
},
...props
};
}

export function BooleanColumn({
accessor,
title,
sortable,
switchable,
ordering
}: {
accessor: string;
title?: string;
ordering?: string;
sortable?: boolean;
switchable?: boolean;
}): TableColumn {
export function BooleanColumn(props: TableColumn): TableColumn {
return {
accessor: accessor,
title: title,
ordering: ordering,
sortable: sortable ?? true,
switchable: switchable ?? true,
sortable: true,
switchable: true,
render: (record: any) => (
<YesNoButton value={resolveItem(record, accessor)} />
)
<YesNoButton value={resolveItem(record, props.accessor ?? '')} />
),
...props
};
}

export function DescriptionColumn({
accessor,
sortable,
switchable
}: {
accessor?: string;
sortable?: boolean;
switchable?: boolean;
}): TableColumn {
export function DescriptionColumn(props: TableColumnProps): TableColumn {
return {
accessor: accessor ?? 'description',
accessor: 'description',
title: t`Description`,
sortable: sortable ?? false,
switchable: switchable ?? true
sortable: false,
switchable: true,
...props
};
}

export function LinkColumn({
accessor = 'link'
}: {
accessor?: string;
}): TableColumn {
export function LinkColumn(props: TableColumnProps): TableColumn {
return {
accessor: accessor,
accessor: 'link',
sortable: false,
render: (record: any) => {
let url = resolveItem(record, accessor);
let url = resolveItem(record, props.accessor ?? 'link');

if (!url) {
return '-';
Expand All @@ -127,24 +93,28 @@ export function LinkColumn({
{url}
</Anchor>
);
}
},
...props
};
}

export function ReferenceColumn(): TableColumn {
export function ReferenceColumn(props: TableColumnProps): TableColumn {
return {
accessor: 'reference',
title: t`Reference`,
sortable: true,
switchable: false
switchable: true,
...props
};
}

export function NoteColumn(): TableColumn {
export function NoteColumn(props: TableColumnProps): TableColumn {
return {
accessor: 'note',
sortable: false,
title: t`Note`,
render: (record: any) => record.note ?? record.notes
render: (record: any) => record.note ?? record.notes,
...props
};
}

Expand All @@ -162,13 +132,14 @@ export function LineItemsProgressColumn(): TableColumn {
};
}

export function ProjectCodeColumn(): TableColumn {
export function ProjectCodeColumn(props: TableColumnProps): TableColumn {
return {
accessor: 'project_code',
sortable: true,
render: (record: any) => (
<ProjectCodeHoverCard projectCode={record.project_code_detail} />
)
),
...props
};
}

Expand All @@ -188,62 +159,52 @@ export function StatusColumn({
};
}

export function ResponsibleColumn(): TableColumn {
export function ResponsibleColumn(props: TableColumnProps): TableColumn {
return {
accessor: 'responsible',
sortable: true,
switchable: true,
render: (record: any) =>
record.responsible && RenderOwner({ instance: record.responsible_detail })
record.responsible &&
RenderOwner({ instance: record.responsible_detail }),
...props
};
}

export function DateColumn({
accessor,
sortable,
switchable,
ordering,
title
}: {
accessor?: string;
ordering?: string;
sortable?: boolean;
switchable?: boolean;
title?: string;
}): TableColumn {
export function DateColumn(props: TableColumnProps): TableColumn {
return {
accessor: accessor ?? 'date',
sortable: sortable ?? true,
ordering: ordering,
title: title ?? t`Date`,
switchable: switchable,
render: (record: any) => renderDate(resolveItem(record, accessor ?? 'date'))
accessor: 'date',
sortable: true,
title: t`Date`,
switchable: true,
render: (record: any) =>
renderDate(resolveItem(record, props.accessor ?? 'date')),
...props
};
}

export function TargetDateColumn(): TableColumn {
return {
export function TargetDateColumn(props: TableColumnProps): TableColumn {
return DateColumn({
accessor: 'target_date',
sortable: true,
title: t`Target Date`,
// TODO: custom renderer which alerts user if target date is overdue
render: (record: any) => renderDate(record.target_date)
};
...props
});
}

export function CreationDateColumn(): TableColumn {
return {
export function CreationDateColumn(props: TableColumnProps): TableColumn {
return DateColumn({
accessor: 'creation_date',
sortable: true,
render: (record: any) => renderDate(record.creation_date)
};
title: t`Creation Date`,
...props
});
}

export function ShipmentDateColumn(): TableColumn {
return {
export function ShipmentDateColumn(props: TableColumnProps): TableColumn {
return DateColumn({
accessor: 'shipment_date',
sortable: true,
render: (record: any) => renderDate(record.shipment_date)
};
title: t`Shipment Date`,
...props
});
}

export function CurrencyColumn({
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/tables/bom/BomTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export function BomTable({
DescriptionColumn({
accessor: 'sub_part_detail.description'
}),
ReferenceColumn(),
ReferenceColumn({
switchable: true
}),
{
accessor: 'quantity',
switchable: false,
Expand Down Expand Up @@ -248,7 +250,7 @@ export function BomTable({
);
}
},
NoteColumn()
NoteColumn({})
];
}, [partId, params]);

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/tables/bom/UsedInTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function UsedInTable({
);
}
},
ReferenceColumn()
ReferenceColumn({})
];
}, [partId]);

Expand Down
10 changes: 5 additions & 5 deletions src/frontend/src/tables/build/BuildOrderTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { InvenTreeTable } from '../InvenTreeTable';
*/
function buildOrderTableColumns(): TableColumn[] {
return [
ReferenceColumn(),
ReferenceColumn({}),
{
accessor: 'part',
sortable: true,
Expand All @@ -60,13 +60,13 @@ function buildOrderTableColumns(): TableColumn[] {
)
},
StatusColumn({ model: ModelType.build }),
ProjectCodeColumn(),
ProjectCodeColumn({}),
{
accessor: 'priority',
sortable: true
},
CreationDateColumn(),
TargetDateColumn(),
CreationDateColumn({}),
TargetDateColumn({}),
DateColumn({
accessor: 'completion_date',
sortable: true
Expand All @@ -78,7 +78,7 @@ function buildOrderTableColumns(): TableColumn[] {
<RenderUser instance={record?.issued_by_detail} />
)
},
ResponsibleColumn()
ResponsibleColumn({})
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function PurchaseOrderLineItemTable({
sortable: false,
render: (record: any) => record?.part_detail?.description
},
ReferenceColumn(),
ReferenceColumn({}),
{
accessor: 'quantity',
title: t`Quantity`,
Expand Down Expand Up @@ -170,7 +170,7 @@ export function PurchaseOrderLineItemTable({
title: t`Unit Price`
}),
TotalPriceColumn(),
TargetDateColumn(),
TargetDateColumn({}),
{
accessor: 'destination',
title: t`Destination`,
Expand All @@ -180,7 +180,7 @@ export function PurchaseOrderLineItemTable({
? RenderStockLocation({ instance: record.destination_detail })
: '-'
},
NoteColumn(),
NoteColumn({}),
LinkColumn({})
];
}, [orderId, user]);
Expand Down

0 comments on commit acb1ec4

Please sign in to comment.