summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormabashian <mabashia@redhat.com>2020-09-10 22:52:12 +0200
committermabashian <mabashia@redhat.com>2020-09-29 15:09:52 +0200
commit10110643eda96636aae66a3b179cfd857d521122 (patch)
tree1b57da194f99955e0ec2a575dba8c4aacb50b963
parentCopy the query params so that we don't add id__in to them. This fixes a bug ... (diff)
downloadawx-10110643eda96636aae66a3b179cfd857d521122.tar.xz
awx-10110643eda96636aae66a3b179cfd857d521122.zip
Flatten out decision tree when an inventory websocket message is processed
-rw-r--r--awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js70
-rw-r--r--awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx29
2 files changed, 63 insertions, 36 deletions
diff --git a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js
index 6156d80692..7797256064 100644
--- a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js
+++ b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.js
@@ -75,38 +75,56 @@ export default function useWsInventories(
return;
}
+ const params = parseQueryString(qsConfig, location.search);
+
const inventory = inventories[index];
const updatedInventory = {
...inventory,
};
- if (lastMessage.group_name === 'inventories') {
- if (lastMessage.status === 'pending_deletion') {
- updatedInventory.pending_deletion = true;
- } else if (lastMessage.status === 'deleted') {
- if (inventories.length === 1) {
- const params = parseQueryString(qsConfig, location.search);
- if (params.page > 1) {
- const newParams = encodeNonDefaultQueryString(
- qsConfig,
- replaceParams(params, {
- page: params.page - 1,
- })
- );
- history.push(`${location.pathname}?${newParams}`);
- }
- } else {
- fetchInventories();
- }
- } else {
- return;
- }
- } else {
- if (!['pending', 'waiting', 'running'].includes(lastMessage.status)) {
- enqueueId(lastMessage.inventory_id);
- return;
- }
+ if (
+ lastMessage.group_name === 'inventories' &&
+ lastMessage.status === 'deleted' &&
+ inventories.length === 1 &&
+ params.page > 1
+ ) {
+ // We've deleted the last inventory on this page so we'll
+ // try to navigate back to the previous page
+ const newParams = encodeNonDefaultQueryString(
+ qsConfig,
+ replaceParams(params, {
+ page: params.page - 1,
+ })
+ );
+ history.push(`${location.pathname}?${newParams}`);
+ return;
+ }
+
+ if (
+ lastMessage.group_name === 'inventories' &&
+ lastMessage.status === 'deleted'
+ ) {
+ fetchInventories();
+ return;
+ }
+
+ if (
+ !['pending', 'waiting', 'running', 'pending_deletion'].includes(
+ lastMessage.status
+ )
+ ) {
+ enqueueId(lastMessage.inventory_id);
+ return;
+ }
+
+ if (
+ lastMessage.group_name === 'inventories' &&
+ lastMessage.status === 'pending_deletion'
+ ) {
+ updatedInventory.pending_deletion = true;
+ }
+ if (lastMessage.group_name !== 'inventories') {
updatedInventory.isSourceSyncRunning = true;
}
diff --git a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx
index f7640522d2..fa18e5b175 100644
--- a/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx
+++ b/awx/ui_next/src/screens/Inventory/InventoryList/useWsInventories.test.jsx
@@ -31,6 +31,10 @@ function Test({
return <TestInner inventories={syncedInventories} />;
}
+const QS_CONFIG = {
+ defaultParams: {},
+};
+
describe('useWsInventories hook', () => {
let debug;
let wrapper;
@@ -41,14 +45,16 @@ describe('useWsInventories hook', () => {
afterEach(() => {
global.console.debug = debug;
+ WS.clean();
});
test('should return inventories list', () => {
const inventories = [{ id: 1 }];
- wrapper = mountWithContexts(<Test inventories={inventories} />);
+ wrapper = mountWithContexts(
+ <Test inventories={inventories} qsConfig={QS_CONFIG} />
+ );
expect(wrapper.find('TestInner').prop('inventories')).toEqual(inventories);
- WS.clean();
});
test('should establish websocket connection', async () => {
@@ -57,7 +63,9 @@ describe('useWsInventories hook', () => {
const inventories = [{ id: 1 }];
await act(async () => {
- wrapper = await mountWithContexts(<Test inventories={inventories} />);
+ wrapper = await mountWithContexts(
+ <Test inventories={inventories} qsConfig={QS_CONFIG} />
+ );
});
await mockServer.connected;
@@ -71,7 +79,6 @@ describe('useWsInventories hook', () => {
},
})
);
- WS.clean();
});
test('should update inventory sync status', async () => {
@@ -80,7 +87,9 @@ describe('useWsInventories hook', () => {
const inventories = [{ id: 1 }];
await act(async () => {
- wrapper = await mountWithContexts(<Test inventories={inventories} />);
+ wrapper = await mountWithContexts(
+ <Test inventories={inventories} qsConfig={QS_CONFIG} />
+ );
});
await mockServer.connected;
@@ -108,7 +117,6 @@ describe('useWsInventories hook', () => {
expect(
wrapper.find('TestInner').prop('inventories')[0].isSourceSyncRunning
).toEqual(true);
- WS.clean();
});
test('should fetch fresh inventory after sync runs', async () => {
@@ -123,6 +131,7 @@ describe('useWsInventories hook', () => {
inventories={inventories}
fetchInventories={fetchInventories}
fetchInventoriesById={fetchInventoriesById}
+ qsConfig={QS_CONFIG}
/>
);
});
@@ -139,7 +148,6 @@ describe('useWsInventories hook', () => {
});
expect(fetchInventoriesById).toHaveBeenCalledWith([1]);
- WS.clean();
});
test('should update inventory pending_deletion', async () => {
@@ -148,7 +156,9 @@ describe('useWsInventories hook', () => {
const inventories = [{ id: 1, pending_deletion: false }];
await act(async () => {
- wrapper = await mountWithContexts(<Test inventories={inventories} />);
+ wrapper = await mountWithContexts(
+ <Test inventories={inventories} qsConfig={QS_CONFIG} />
+ );
});
await mockServer.connected;
@@ -176,7 +186,6 @@ describe('useWsInventories hook', () => {
expect(
wrapper.find('TestInner').prop('inventories')[0].pending_deletion
).toEqual(true);
- WS.clean();
});
test('should refetch inventories after an inventory is deleted', async () => {
@@ -191,6 +200,7 @@ describe('useWsInventories hook', () => {
inventories={inventories}
fetchInventories={fetchInventories}
fetchInventoriesById={fetchInventoriesById}
+ qsConfig={QS_CONFIG}
/>
);
});
@@ -207,6 +217,5 @@ describe('useWsInventories hook', () => {
});
expect(fetchInventories).toHaveBeenCalled();
- WS.clean();
});
});