From d6f66c74d3458a5b0014d2fbf0a17d91783a2b93 Mon Sep 17 00:00:00 2001 From: Peter Makowski Date: Mon, 20 May 2024 15:30:58 +0200 Subject: [PATCH] refactor: pod selectors caching (#5439) --- .../AppSideNavigation.test.tsx | 12 ++++++++---- src/app/store/pod/selectors.ts | 17 +++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/app/base/components/AppSideNavigation/AppSideNavigation.test.tsx b/src/app/base/components/AppSideNavigation/AppSideNavigation.test.tsx index 9d84096ec0..fa30611e84 100644 --- a/src/app/base/components/AppSideNavigation/AppSideNavigation.test.tsx +++ b/src/app/base/components/AppSideNavigation/AppSideNavigation.test.tsx @@ -349,17 +349,21 @@ describe("GlobalSideNav", () => { ); }); - it("hides the 'Virsh' link if the user does not have any Virsh KVM hosts", () => { - const { rerender } = renderWithBrowserRouter(, { + it("displays 'Virsh' link if user has Virsh KVM hosts", () => { + renderWithBrowserRouter(, { route: "/machines", state, }); expect(screen.getByRole("link", { name: "Virsh" })).toBeInTheDocument(); + }); + it("hides 'Virsh' link if user has no Virsh KVM hosts", () => { state.pod.items = []; - - rerender(); + renderWithBrowserRouter(, { + route: "/machines", + state, + }); expect( screen.queryByRole("link", { name: "Virsh" }) diff --git a/src/app/store/pod/selectors.ts b/src/app/store/pod/selectors.ts index 7d09ca2384..94185ed5b2 100644 --- a/src/app/store/pod/selectors.ts +++ b/src/app/store/pod/selectors.ts @@ -25,18 +25,18 @@ const defaultSelectors = generateBaseSelectors( * @param {RootState} state - The redux state. * @returns {Pod[]} A list of all KVMs. */ -const kvms = (state: RootState): Pod[] => - state.pod.items.filter((pod) => - [PodType.LXD, PodType.VIRSH].includes(pod.type) - ); +const kvms = createSelector([defaultSelectors.all], (pods) => + pods.filter((pod) => [PodType.LXD, PodType.VIRSH].includes(pod.type)) +); /** * Returns all LXD pods. * @param state - The redux state. * @returns A list of all LXD pods. */ -const lxd = (state: RootState): Pod[] => - state.pod.items.filter((pod) => pod.type === PodType.LXD); +const lxd = createSelector([defaultSelectors.all], (pods) => + pods.filter((pod) => pod.type === PodType.LXD) +); /** * Returns all LXD single hosts (i.e. LXD pods that are not cluster hosts). @@ -101,8 +101,9 @@ const searchInCluster = createSelector( * @param state - The redux state. * @returns A list of all virsh pods. */ -const virsh = (state: RootState): Pod[] => - state.pod.items.filter((pod) => pod.type === PodType.VIRSH); +const virsh = createSelector([defaultSelectors.all], (pods) => + pods.filter((pod) => pod.type === PodType.VIRSH) +); /** * Returns active pod id.