Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PUI] Table refactor #8395

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/backend/InvenTree/part/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,10 @@ def setup_eager_loading(queryset):
'sub_part__stock_items__sales_order_allocations',
)

queryset = queryset.select_related(
'part__pricing_data', 'sub_part__pricing_data'
)

queryset = queryset.prefetch_related(
'substitutes', 'substitutes__part__stock_items'
)
Expand Down
49 changes: 41 additions & 8 deletions src/frontend/src/hooks/UseTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { randomId, useLocalStorage } from '@mantine/hooks';
import { useCallback, useMemo, useState } from 'react';
import { SetURLSearchParams, useSearchParams } from 'react-router-dom';

import { TableFilter } from '../tables/Filter';

Expand All @@ -8,19 +9,47 @@ import { TableFilter } from '../tables/Filter';
*
* tableKey: A unique key for the table. When this key changes, the table will be refreshed.
* refreshTable: A callback function to externally refresh the table.
* isLoading: A boolean flag to indicate if the table is currently loading data
* setIsLoading: A function to set the isLoading flag
* activeFilters: An array of active filters (saved to local storage)
* setActiveFilters: A function to set the active filters
* clearActiveFilters: A function to clear all active filters
* queryFilters: A map of query filters (e.g. ?active=true&overdue=false) passed in the URL
* setQueryFilters: A function to set the query filters
* clearQueryFilters: A function to clear all query filters
* expandedRecords: An array of expanded records (rows) in the table
* setExpandedRecords: A function to set the expanded records
* isRowExpanded: A function to determine if a record is expanded
* selectedRecords: An array of selected records (rows) in the table
* selectedIds: An array of primary key values for selected records
* hasSelectedRecords: A boolean flag to indicate if any records are selected
* setSelectedRecords: A function to set the selected records
* clearSelectedRecords: A function to clear all selected records
* hiddenColumns: An array of hidden column names
* setHiddenColumns: A function to set the hidden columns
* searchTerm: The current search term for the table
* setSearchTerm: A function to set the search term
* recordCount: The total number of records in the table
* setRecordCount: A function to set the record count
* page: The current page number
* setPage: A function to set the current page number
* pageSize: The number of records per page
* setPageSize: A function to set the number of records per page
* records: An array of records (rows) in the table
* setRecords: A function to set the records
* updateRecord: A function to update a single record in the table
*/
export type TableState = {
tableKey: string;
refreshTable: () => void;
activeFilters: TableFilter[];
isLoading: boolean;
setIsLoading: (value: boolean) => void;
activeFilters: TableFilter[];
setActiveFilters: (filters: TableFilter[]) => void;
clearActiveFilters: () => void;
queryFilters: URLSearchParams;
setQueryFilters: SetURLSearchParams;
clearQueryFilters: () => void;
expandedRecords: any[];
setExpandedRecords: (records: any[]) => void;
isRowExpanded: (pk: number) => boolean;
Expand All @@ -42,8 +71,6 @@ export type TableState = {
records: any[];
setRecords: (records: any[]) => void;
updateRecord: (record: any) => void;
editable: boolean;
setEditable: (value: boolean) => void;
};

/**
Expand All @@ -58,6 +85,13 @@ export function useTable(tableName: string): TableState {
return `${tableName.replaceAll('-', '')}-${randomId()}`;
}

// Extract URL query parameters (e.g. ?active=true&overdue=false)
const [queryFilters, setQueryFilters] = useSearchParams();

const clearQueryFilters = useCallback(() => {
setQueryFilters({});
}, []);

const [tableKey, setTableKey] = useState<string>(generateTableName());

// Callback used to refresh (reload) the table
Expand Down Expand Up @@ -145,8 +179,6 @@ export function useTable(tableName: string): TableState {

const [isLoading, setIsLoading] = useState<boolean>(false);

const [editable, setEditable] = useState<boolean>(false);

return {
tableKey,
refreshTable,
Expand All @@ -155,6 +187,9 @@ export function useTable(tableName: string): TableState {
activeFilters,
setActiveFilters,
clearActiveFilters,
queryFilters,
setQueryFilters,
clearQueryFilters,
expandedRecords,
setExpandedRecords,
isRowExpanded,
Expand All @@ -175,8 +210,6 @@ export function useTable(tableName: string): TableState {
setPageSize,
records,
setRecords,
updateRecord,
editable,
setEditable
updateRecord
};
}
10 changes: 6 additions & 4 deletions src/frontend/src/pages/part/PartDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -618,16 +618,18 @@ export default function PartDetail() {
label: t`Allocations`,
icon: <IconBookmarks />,
hidden: !part.component && !part.salable,
content: part.pk ? <PartAllocationPanel part={part} /> : <Skeleton />
content: part?.pk ? (
<BomTable partId={part.pk ?? -1} partLocked={part?.locked == true} />
) : (
<Skeleton />
)
},
{
name: 'bom',
label: t`Bill of Materials`,
icon: <IconListTree />,
hidden: !part.assembly,
content: (
<BomTable partId={part.pk ?? -1} partLocked={part?.locked == true} />
)
content: part.pk ? <BomTable partId={part.pk} /> : <Skeleton />
},
{
name: 'builds',
Expand Down
Loading
Loading