From 2e7bf51ed861b1b0614196e8b24d0edb30c74a3c Mon Sep 17 00:00:00 2001 From: German Ferrero Date: Fri, 17 Dec 2021 12:01:33 -0300 Subject: [PATCH] refactor(networknodes): fix api format --- .../deleteNodes.spec.js | 22 +++++++++---------- .../src/deleteNodesPage.js | 2 +- .../src/networkNodesQueries.js | 9 ++++---- .../reachableNodes.spec.js | 22 +++++++++---------- .../src/reachableNodesPage.js | 7 +++--- .../src/upgradedNodesPage.js | 2 +- .../upgradedNodes.spec.js | 12 +++++----- 7 files changed, 38 insertions(+), 38 deletions(-) diff --git a/plugins/lime-plugin-delete-nodes/deleteNodes.spec.js b/plugins/lime-plugin-delete-nodes/deleteNodes.spec.js index 9dfe24fc..8ec7143d 100644 --- a/plugins/lime-plugin-delete-nodes/deleteNodes.spec.js +++ b/plugins/lime-plugin-delete-nodes/deleteNodes.spec.js @@ -12,17 +12,17 @@ jest.mock('plugins/lime-plugin-network-nodes/src/networkNodesApi'); describe('delete nodes page', () => { beforeEach(() => { - getNodes.mockImplementation(async () => [ - { hostname: 'node1', status: 'recently_reachable' }, - { hostname: 'node2', status: 'recently_reachable' }, - { hostname: 'node3', status: 'recently_reachable' }, - { hostname: 'node4', status: 'unreachable' }, - { hostname: 'node5', status: 'unreachable' }, - { hostname: 'node6', status: 'unreachable' }, - { hostname: 'node7', status: 'unreachable' }, - { hostname: 'node8', status: 'gone' }, - { hostname: 'node9', status: 'gone' }, - ]); + getNodes.mockImplementation(async () => ({ + 'node1': { hostname: 'node1', status: 'recently_reachable' }, + 'node2': { hostname: 'node2', status: 'recently_reachable' }, + 'node3': { hostname: 'node3', status: 'recently_reachable' }, + 'node4': { hostname: 'node4', status: 'unreachable' }, + 'node5': { hostname: 'node5', status: 'unreachable' }, + 'node6': { hostname: 'node6', status: 'unreachable' }, + 'node7': { hostname: 'node7', status: 'unreachable' }, + 'node8': { hostname: 'node8', status: 'gone' }, + 'node9': { hostname: 'node9', status: 'gone' }, + })); markNodesAsGone.mockImplementation(async () => []); }); diff --git a/plugins/lime-plugin-delete-nodes/src/deleteNodesPage.js b/plugins/lime-plugin-delete-nodes/src/deleteNodesPage.js index f0417d7d..418a762f 100644 --- a/plugins/lime-plugin-delete-nodes/src/deleteNodesPage.js +++ b/plugins/lime-plugin-delete-nodes/src/deleteNodesPage.js @@ -11,7 +11,7 @@ import I18n from 'i18n-js'; export const DeleteNodesPage_ = ({ nodes, onDelete, isSubmitting, isSuccess }) => { const [selectedNodes, { toggle, has, reset }] = useSet(new Set([])); const [showSuccess, setshowSuccess] = useState(false); - const unreachableNodes = nodes.filter(n => n.status === "unreachable"); + const unreachableNodes = Object.values(nodes).filter(n => n.status === "unreachable"); useEffect(() => { if (isSuccess) { diff --git a/plugins/lime-plugin-network-nodes/src/networkNodesQueries.js b/plugins/lime-plugin-network-nodes/src/networkNodesQueries.js index 0e82bc51..ad8a9356 100644 --- a/plugins/lime-plugin-network-nodes/src/networkNodesQueries.js +++ b/plugins/lime-plugin-network-nodes/src/networkNodesQueries.js @@ -8,10 +8,11 @@ export const useNetworkNodes = () => export const useMarkNodesAsGone = () => useMutation(markNodesAsGone, { onSuccess: hostnames => queryCache.setQueryData(['network-nodes', 'get_nodes'], old => { - const result = old.map( - node => hostnames.indexOf(node.hostname) != -1 ? { ...node, status: "gone" } : node - ) + const result = old; + hostnames.forEach(hostname => { + result[hostname] = {...old[hostname], status: "gone"} + }); return result; } ) -}) \ No newline at end of file +}) diff --git a/plugins/lime-plugin-reachable-nodes/reachableNodes.spec.js b/plugins/lime-plugin-reachable-nodes/reachableNodes.spec.js index be0d3371..e490f644 100644 --- a/plugins/lime-plugin-reachable-nodes/reachableNodes.spec.js +++ b/plugins/lime-plugin-reachable-nodes/reachableNodes.spec.js @@ -10,17 +10,17 @@ import { render } from 'utils/test_utils'; jest.mock('plugins/lime-plugin-network-nodes/src/networkNodesApi'); beforeEach(() => { - getNodes.mockImplementation(async () => [ - { hostname: 'node1', status: 'recently_reachable' }, - { hostname: 'node2', status: 'recently_reachable' }, - { hostname: 'node3', status: 'recently_reachable' }, - { hostname: 'node4', status: 'unreachable' }, - { hostname: 'node5', status: 'unreachable' }, - { hostname: 'node6', status: 'unreachable' }, - { hostname: 'node7', status: 'unreachable' }, - { hostname: 'node8', status: 'gone' }, - { hostname: 'node9', status: 'gone' }, - ]); + getNodes.mockImplementation(async () => ({ + 'node1': { hostname: 'node1', status: 'recently_reachable' }, + 'node2': { hostname: 'node2', status: 'recently_reachable' }, + 'node3': { hostname: 'node3', status: 'recently_reachable' }, + 'node4': { hostname: 'node4', status: 'unreachable' }, + 'node5': { hostname: 'node5', status: 'unreachable' }, + 'node6': { hostname: 'node6', status: 'unreachable' }, + 'node7': { hostname: 'node7', status: 'unreachable' }, + 'node8': { hostname: 'node8', status: 'gone' }, + 'node9': { hostname: 'node9', status: 'gone' }, + })); markNodesAsGone.mockImplementation(async () => []); }); diff --git a/plugins/lime-plugin-reachable-nodes/src/reachableNodesPage.js b/plugins/lime-plugin-reachable-nodes/src/reachableNodesPage.js index 92172c6b..bc02bd4f 100644 --- a/plugins/lime-plugin-reachable-nodes/src/reachableNodesPage.js +++ b/plugins/lime-plugin-reachable-nodes/src/reachableNodesPage.js @@ -27,8 +27,8 @@ const PageHelp = () => ( ); const PageTabs = ({ nodes, ...props }) => { - const nReachable = nodes.filter(n => n.status === "recently_reachable").length; - const nUnreachable = nodes.filter(n => n.status === "unreachable").length; + const nReachable = Object.values(nodes).filter(n => n.status === "recently_reachable").length; + const nUnreachable = Object.values(nodes).filter(n => n.status === "unreachable").length; const tabs = [ { key: 'recently_reachable', repr: I18n.t('Reachable (%{howMany})', { howMany: nReachable }) }, { key: 'unreachable', repr: I18n.t('Unreachable (%{howMany})', { howMany: nUnreachable }) }, @@ -39,7 +39,6 @@ const PageTabs = ({ nodes, ...props }) => { export const ReachableNodesPage_ = ({ nodes }) => { const [ selectedGroup, setselectedGroup ] = useState('recently_reachable'); const [ unfoldedNode, setunfoldedNode ] = useState(null); - function changeUnfolded(hostname) { if (unfoldedNode == hostname) { setunfoldedNode(null); @@ -56,7 +55,7 @@ export const ReachableNodesPage_ = ({ nodes }) => { - {nodes + {Object.values(nodes) .filter(n => n.status === selectedGroup) .sort((a, b) => a.hostname > b.hostname) .map( diff --git a/plugins/lime-plugin-upgraded-nodes/src/upgradedNodesPage.js b/plugins/lime-plugin-upgraded-nodes/src/upgradedNodesPage.js index 0e1ba5a9..99b5c203 100644 --- a/plugins/lime-plugin-upgraded-nodes/src/upgradedNodesPage.js +++ b/plugins/lime-plugin-upgraded-nodes/src/upgradedNodesPage.js @@ -103,7 +103,7 @@ const UpgradedNodesPage = () => { useEffect(() => { if (nodes && newVersion) { - const taggedNodes = [...nodes].map( + const taggedNodes = [...Object.values(nodes)].map( n => ({ ...n, group: n.fw_version === newVersion.version ? 'upgraded' : 'not_upgraded' diff --git a/plugins/lime-plugin-upgraded-nodes/upgradedNodes.spec.js b/plugins/lime-plugin-upgraded-nodes/upgradedNodes.spec.js index 16d4c800..aa3d68be 100644 --- a/plugins/lime-plugin-upgraded-nodes/upgradedNodes.spec.js +++ b/plugins/lime-plugin-upgraded-nodes/upgradedNodes.spec.js @@ -14,12 +14,12 @@ jest.mock('plugins/lime-plugin-firmware/src/firmwareApi'); describe('upgradedNodes', () => { beforeEach(() => { - getNodes.mockImplementation(async () => [ - { hostname: 'node1', status: 'unreachable', fw_version: 'LibreRouterOS 1.4' }, - { hostname: 'node2', status: 'recently_reachable', fw_version: 'LibreRouterOS 1.4' }, - { hostname: 'node3', status: 'unreachable', fw_version: 'LibreRouterOS 1.3' }, - { hostname: 'node4', status: 'recently_reachable', fw_version: 'LibreRouterOS 1.3' }, - ]); + getNodes.mockImplementation(async () => ({ + 'node1': { hostname: 'node1', status: 'unreachable', fw_version: 'LibreRouterOS 1.4' }, + 'node2': { hostname: 'node2', status: 'recently_reachable', fw_version: 'LibreRouterOS 1.4' }, + 'node3': { hostname: 'node3', status: 'unreachable', fw_version: 'LibreRouterOS 1.3' }, + 'node4': { hostname: 'node4', status: 'recently_reachable', fw_version: 'LibreRouterOS 1.3' }, + })); getNewVersion.mockImplementation(async () => ({ version: 'LibreRouterOS 1.4' }))