diff --git a/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.jsx b/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.jsx
index 89121e8cd5..ae4dc53bc1 100644
--- a/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.jsx
+++ b/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.jsx
@@ -23,6 +23,11 @@ import JobList from '@components/JobList';
import InventoryHostDetail from '../InventoryHostDetail';
import InventoryHostEdit from '../InventoryHostEdit';
+const checkHostInventory = (host, inventory) =>
+ host &&
+ inventory &&
+ host.summary_fields?.inventory?.id === parseInt(inventory.id, 10);
+
function InventoryHost({ i18n, setBreadcrumb, inventory }) {
const location = useLocation();
const match = useRouteMatch('/inventories/inventory/:id/hosts/:hostId');
@@ -40,7 +45,7 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
return {
host: data,
};
- }, [match.params.hostId]), // eslint-disable-line react-hooks/exhaustive-deps
+ }, [match.params.hostId]),
{
host: null,
}
@@ -48,7 +53,7 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
useEffect(() => {
fetchHost();
- }, [fetchHost]);
+ }, [fetchHost, location.pathname]);
useEffect(() => {
if (inventory && host) {
@@ -89,24 +94,7 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
},
];
- let cardHeader = (
-
-
-
-
-
-
- );
-
- if (location.pathname.endsWith('edit')) {
- cardHeader = null;
- }
-
- if (isLoading) {
- return ;
- }
-
- if (!isLoading && contentError) {
+ if (contentError) {
return (
@@ -123,50 +111,62 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
);
}
+ const isInventoryValid = checkHostInventory(host, inventory);
+ if (!isLoading && !isInventoryValid) {
+ return (
+
+ {i18n._(t`View all Inventory Hosts.`)}
+
+ );
+ }
+
return (
<>
- {cardHeader}
-
-
- {host &&
- inventory && [
-
-
- ,
-
-
- ,
-
-
- ,
- ]}
-
- !isLoading && (
-
-
- {i18n._(`View Inventory Host Details`)}
-
-
- )
- }
- />
-
+ {['edit'].some(name => location.pathname.includes(name)) ? null : (
+
+
+
+
+
+
+ )}
+
+ {isLoading && }
+
+ {!isLoading && isInventoryValid && (
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {i18n._(`View Inventory Host Details`)}
+
+
+
+
+ )}
>
);
}
diff --git a/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.test.jsx b/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.test.jsx
index bea26df827..b844668eca 100644
--- a/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.test.jsx
+++ b/awx/ui_next/src/screens/Inventory/InventoryHost/InventoryHost.test.jsx
@@ -76,4 +76,13 @@ describe('', () => {
});
await waitForElement(wrapper, 'ContentError', el => el.length === 1);
});
+
+ test('should show content error when inventory id does not match host inventory', async () => {
+ await act(async () => {
+ wrapper = mountWithContexts(
+ {}} />
+ );
+ });
+ await waitForElement(wrapper, 'ContentError', el => el.length === 1);
+ });
});