From 2e0ece6b488ffda45f2df2f29ae945aacef881bd Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 28 Jun 2026 07:23:49 +0000 Subject: [PATCH] Create useStockItems hook for memoizing items --- src/frontend/src/forms/StockForms.tsx | 61 ++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/frontend/src/forms/StockForms.tsx b/src/frontend/src/forms/StockForms.tsx index 148c8d4c17..0712ae5863 100644 --- a/src/frontend/src/forms/StockForms.tsx +++ b/src/frontend/src/forms/StockForms.tsx @@ -555,20 +555,61 @@ 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 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 + */ +function useStockItems({ + enabled, + items, + category, + location, + part +}: Readonly<{ + enabled: boolean; + items?: any | any[]; + category?: number | string; + location?: number | string; + part?: number | string; +}>) { + const query = useQuery({ + enabled: enabled, + queryKey: ['stockItems', category, location, part], + queryFn: async () => { + if (items) { + return Array.isArray(items) ? items : [items]; + } + + if (!enabled) { + 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 useMemo(() => query.data ?? [], [query.data]); +} + type StockAdjustmentItemWithRecord = { obj: any; } & StockAdjustmentItem; -type TableFieldRefreshFn = (idx: number) => void; -type TableFieldChangeFn = (idx: number, key: string, value: any) => void; - -type StockRow = { - item: StockAdjustmentItemWithRecord; - idx: number; - changeFn: TableFieldChangeFn; - removeFn: TableFieldRefreshFn; -}; - function ReturnStockMoveButton({ record, quantity,