From 468efbacfcc4f4cb45208dc7197d11c2f6d40118 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 20 Nov 2025 06:34:25 +1100 Subject: [PATCH] [UI] Delete stock fix (#10868) * Add helper func getOverviewUrl * Redirect to parent page when stock item is counted to zero --- src/frontend/lib/functions/Navigation.tsx | 22 ++++++++++++++++++++ src/frontend/src/pages/stock/StockDetail.tsx | 17 +++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/frontend/lib/functions/Navigation.tsx b/src/frontend/lib/functions/Navigation.tsx index 9b8e91587a..21e9324d20 100644 --- a/src/frontend/lib/functions/Navigation.tsx +++ b/src/frontend/lib/functions/Navigation.tsx @@ -7,6 +7,28 @@ import { cancelEvent } from './Events'; export const getBaseUrl = (): string => (window as any).INVENTREE_SETTINGS?.base_url || 'web'; +/** + * Returns the overview URL for a given model type. + * This is the UI URL, not the API URL. + */ +export function getOverviewUrl(model: ModelType, absolute?: boolean): string { + const modelInfo = ModelInformationDict[model]; + + if (modelInfo?.url_overview) { + const url = modelInfo.url_overview; + const base = getBaseUrl(); + + if (absolute && base) { + return `/${base}${url}`; + } else { + return url; + } + } + + console.error(`No overview URL found for model ${model}`); + return ''; +} + /** * Returns the detail view URL for a given model type. * This is the UI URL, not the API URL. diff --git a/src/frontend/src/pages/stock/StockDetail.tsx b/src/frontend/src/pages/stock/StockDetail.tsx index b6466b0b85..3faf693eff 100644 --- a/src/frontend/src/pages/stock/StockDetail.tsx +++ b/src/frontend/src/pages/stock/StockDetail.tsx @@ -32,7 +32,7 @@ import { ApiEndpoints } from '@lib/enums/ApiEndpoints'; import { ModelType } from '@lib/enums/ModelType'; import { UserRoles } from '@lib/enums/Roles'; import { apiUrl } from '@lib/functions/Api'; -import { getDetailUrl } from '@lib/functions/Navigation'; +import { getDetailUrl, getOverviewUrl } from '@lib/functions/Navigation'; import type { StockOperationProps } from '@lib/types/Forms'; import { notifications } from '@mantine/notifications'; import { useBarcodeScanDialog } from '../../components/barcodes/BarcodeScanDialog'; @@ -730,7 +730,20 @@ export default function StockDetail() { return { items: [stockitem], model: ModelType.stockitem, - refresh: refreshInstance, + refresh: () => { + const location = stockitem?.location; + refreshInstancePromise().then((response) => { + if (response.status == 'error') { + // If an error occurs refreshing the instance, + // the stock likely has likely been depleted + if (location) { + navigate(getDetailUrl(ModelType.stocklocation, location)); + } else { + navigate(getOverviewUrl(ModelType.stockitem)); + } + } + }); + }, filters: { in_stock: true }