From 014f340c5b48c69541bd24161a274640a0647637 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 28 Jun 2026 07:47:05 +0000 Subject: [PATCH] Refactoring --- src/frontend/lib/types/Forms.tsx | 2 - .../importer/ImportDataSelector.tsx | 9 -- src/frontend/src/forms/StockForms.tsx | 108 +++++++----------- .../src/pages/stock/LocationDetail.tsx | 3 +- src/frontend/src/states/LocalState.tsx | 2 +- .../tables/settings/SelectionListDrawer.tsx | 2 - 6 files changed, 43 insertions(+), 83 deletions(-) diff --git a/src/frontend/lib/types/Forms.tsx b/src/frontend/lib/types/Forms.tsx index 03acfb0ad9..48323dd6a0 100644 --- a/src/frontend/lib/types/Forms.tsx +++ b/src/frontend/lib/types/Forms.tsx @@ -221,8 +221,6 @@ export interface BulkEditApiFormModalProps extends ApiFormModalProps { export type StockOperationProps = { items?: any[]; - pk?: number; filters?: any; - model: ModelType.stockitem | 'location' | ModelType.part; refresh: () => void; }; diff --git a/src/frontend/src/components/importer/ImportDataSelector.tsx b/src/frontend/src/components/importer/ImportDataSelector.tsx index 82aadf2d5c..cf426f5cf2 100644 --- a/src/frontend/src/components/importer/ImportDataSelector.tsx +++ b/src/frontend/src/components/importer/ImportDataSelector.tsx @@ -172,15 +172,6 @@ export default function ImporterDataSelector({ description: fieldDef.help_text, filters: filters }; - - console.log('Defined Field:', field); - console.log({ - ...fieldDef, - ...customField, - field_type: fieldDef.type, - description: fieldDef.help_text, - filters: filters - }); } } diff --git a/src/frontend/src/forms/StockForms.tsx b/src/frontend/src/forms/StockForms.tsx index 0712ae5863..a3d6d3771a 100644 --- a/src/frontend/src/forms/StockForms.tsx +++ b/src/frontend/src/forms/StockForms.tsx @@ -559,51 +559,57 @@ function moveToDefault( * Memoize a list of stock items for use in a stock operations modal. * These items may be provided directly, or fetched from the API * - * @param enabled - Whether to enable the query (if false, items must be provided directly) + * @param opened - Is the underlying modal opened or closed? * @param items - Optional list of stock items to use directly * @param category - Optional category ID to filter stock items * @param location - Optional location ID to filter stock items * @param part - Optional part ID to filter stock items + * @param filters - Optional additional filters to apply to the stock item query */ function useStockItems({ - enabled, + opened, items, - category, - location, - part + filters }: Readonly<{ - enabled: boolean; - items?: any | any[]; - category?: number | string; - location?: number | string; - part?: number | string; + opened: boolean; + items?: any[] | any; + filters?: { [key: string]: any }; }>) { const query = useQuery({ - enabled: enabled, - queryKey: ['stockItems', category, location, part], + enabled: opened, + queryKey: ['stockItems', filters], queryFn: async () => { if (items) { return Array.isArray(items) ? items : [items]; } - if (!enabled) { + if (!opened) { return []; } // Fetch via the API const url = apiUrl(ApiEndpoints.stock_item_list); - return api.get(url, { - params: { - category: category || undefined, - location: location || undefined, - part: part || undefined - } - }); + return api + .get(url, { + params: { + ...filters, + part_detail: true, + location_detail: true, + cascade: false + } + }) + .then((response) => response.data ?? []); } }); - return useMemo(() => query.data ?? [], [query.data]); + return useMemo(() => { + if (!opened) { + return []; + } + + return query.data ?? []; + }, [opened, query.data]); } type StockAdjustmentItemWithRecord = { @@ -771,7 +777,10 @@ function StockOperationsRow({
{t`Loading...`}
) : ( <> - + @@ -936,6 +945,7 @@ type StockAdjustmentItem = { function mapAdjustmentItems(items: any[], mergeDefault?: boolean) { const mappedItems: StockAdjustmentItemWithRecord[] = items.map((elem) => { return { + id: elem.pk, pk: elem.pk, quantity: elem.quantity, batch: elem.batch || undefined, @@ -1411,8 +1421,6 @@ type apiModalFunc = (props: ApiFormModalProps) => { function useStockOperationModal({ items, - pk, - model, refresh, fieldGenerator, endpoint, @@ -1423,9 +1431,7 @@ function useStockOperationModal({ modalFunc = useCreateApiFormModal }: { items?: object; - pk?: number; filters?: any; - model: ModelType | string; refresh: () => void; fieldGenerator: (items: any[]) => ApiFormFieldSet; endpoint: ApiEndpoints; @@ -1434,51 +1440,19 @@ function useStockOperationModal({ successMessage?: string; modalFunc?: apiModalFunc; }) { - const baseParams: any = { - part_detail: true, - location_detail: true, - cascade: false - }; - - const params = useMemo(() => { - const query_params: any = { - ...baseParams, - ...(filters ?? {}) - }; - - query_params[model] = - pk === undefined && model === 'location' ? 'null' : pk; - - return query_params; - }, [baseParams, filters, model, pk]); - const [opened, setOpened] = useState(false); - const { data } = useQuery({ - queryKey: ['stockitems', opened, model, pk, items, params], - queryFn: async () => { - if (items) { - // If a list of items is provided, use that directly - return Array.isArray(items) ? items : [items]; - } - - if (!pk || !opened) { - return []; - } - - const url = apiUrl(ApiEndpoints.stock_item_list); - - return api - .get(url, { - params: params - }) - .then((response) => response.data ?? []); - } + const stockItems = useStockItems({ + opened: opened, + items: items, + filters: filters }); - const fields = useMemo(() => { - return fieldGenerator(data); - }, [data]); + // Rebuild the "fields" object + const fields = useMemo( + () => fieldGenerator(stockItems), + [fieldGenerator, stockItems] + ); return modalFunc({ url: endpoint, diff --git a/src/frontend/src/pages/stock/LocationDetail.tsx b/src/frontend/src/pages/stock/LocationDetail.tsx index 0b13ba9f23..f9bdbad8fa 100644 --- a/src/frontend/src/pages/stock/LocationDetail.tsx +++ b/src/frontend/src/pages/stock/LocationDetail.tsx @@ -349,10 +349,9 @@ export default function Stock() { const stockOperationProps: StockOperationProps = useMemo(() => { return { - pk: location.pk, - model: 'location', refresh: refreshInstance, filters: { + location: location.pk, in_stock: true } }; diff --git a/src/frontend/src/states/LocalState.tsx b/src/frontend/src/states/LocalState.tsx index 5b043d44c0..d8b40983bc 100644 --- a/src/frontend/src/states/LocalState.tsx +++ b/src/frontend/src/states/LocalState.tsx @@ -181,6 +181,6 @@ export function patchUser(key: 'language' | 'theme' | 'widgets', val: any) { if (uid) { api.patch(apiUrl(ApiEndpoints.user_me_profile), { [key]: val }); } else { - console.log('user not logged in, not patching'); + console.warn('user not logged in, not patching'); } } diff --git a/src/frontend/src/tables/settings/SelectionListDrawer.tsx b/src/frontend/src/tables/settings/SelectionListDrawer.tsx index 8b0100ae1a..4f3ab3d133 100644 --- a/src/frontend/src/tables/settings/SelectionListDrawer.tsx +++ b/src/frontend/src/tables/settings/SelectionListDrawer.tsx @@ -105,14 +105,12 @@ function SelectionListEntriesTable({ return [ RowEditAction({ onClick: () => { - console.log('record:', record); setSelectedEntry(record.id); editEntry.open(); } }), RowDeleteAction({ onClick: () => { - console.log('record:', record); setSelectedEntry(record.id); deleteEntry.open(); }