Skip to content

Commit

Permalink
refactor: pod selectors caching (#5439)
Browse files Browse the repository at this point in the history
  • Loading branch information
petermakowski authored May 20, 2024
1 parent 8501323 commit d6f66c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,21 @@ describe("GlobalSideNav", () => {
);
});

it("hides the 'Virsh' link if the user does not have any Virsh KVM hosts", () => {
const { rerender } = renderWithBrowserRouter(<AppSideNavigation />, {
it("displays 'Virsh' link if user has Virsh KVM hosts", () => {
renderWithBrowserRouter(<AppSideNavigation />, {
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(<AppSideNavigation />);
renderWithBrowserRouter(<AppSideNavigation />, {
route: "/machines",
state,
});

expect(
screen.queryByRole("link", { name: "Virsh" })
Expand Down
17 changes: 9 additions & 8 deletions src/app/store/pod/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ const defaultSelectors = generateBaseSelectors<PodState, Pod, PodMeta.PK>(
* @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).
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit d6f66c7

Please sign in to comment.