Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bug): Fix dashboard tab loading #1210

Merged
merged 9 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions react-app/src/components/Opensearch/main/useOpensearch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useState, useRef } from "react";
import { useQuery } from "@tanstack/react-query";
import { UserRoles, opensearch } from "shared-types";
import { getOsData, useOsSearch, useGetUser } from "@/api";
Expand Down Expand Up @@ -50,8 +50,17 @@
opensearch.main.Field,
opensearch.main.Response
>();

const [tabLoading, setTabLoading] = useState(false);
const previousTab = useRef(params.state.tab);

const onRequest = async (query: opensearch.main.State, options?: any) => {
try {
if (params.state.tab !== previousTab.current) {
setTabLoading(true);
previousTab.current = params.state.tab;
}

await mutateAsync(
{
index: "main",
Expand All @@ -63,16 +72,23 @@
...(DEFAULT_FILTERS[params.state.tab].filters || []),
],
},
{ ...options, onSuccess: (res) => setData(res.hits) },
{
...options,
onSuccess: (res) => {
setData(res.hits);
setTabLoading(false);
},
},
);
} catch (error) {
console.error("Error occurred during search:", error);
setTabLoading(false);
}
};
useEffect(() => {
onRequest(params.state);
}, [params.queryString]);

Check warning on line 90 in react-app/src/components/Opensearch/main/useOpensearch.ts

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'onRequest' and 'params.state'. Either include them or remove the dependency array
return { data, isLoading, error, ...params };
return { data, isLoading, error, ...params, tabLoading };
};
export const useOsAggregate = () => {
const { data: user } = useGetUser();
Expand Down
50 changes: 31 additions & 19 deletions react-app/src/features/dashboard/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { describe, expect, it, vi, beforeAll, afterAll, beforeEach, afterEach } from "vitest";
import { screen, within, waitForElementToBeRemoved, cleanup } from "@testing-library/react";
import {
screen,
within,
waitForElementToBeRemoved,
cleanup,
waitFor,
} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {
renderWithQueryClientAndMemoryRouter,
Expand Down Expand Up @@ -173,12 +179,13 @@ describe("Dashboard", () => {
it("should handle switching to the Waiver tab", async () => {
const isWaiver = true;

const waiverTab = screen.queryByRole("heading", { level: 2, name: "Waivers" });

await user.click(waiverTab.parentElement);
await waitFor(async () => {
const waiverTab = await screen.findByRole("heading", { level: 2, name: "Waivers" });
await user.click(waiverTab.parentElement);

expect(waiverTab).toBeInTheDocument();
expect(waiverTab.parentElement.getAttribute("aria-selected")).toEqual("true");
expect(waiverTab).toBeInTheDocument();
expect(waiverTab.parentElement.getAttribute("aria-selected")).toEqual("true");
});

const spaTab = screen.queryByRole("heading", { level: 2, name: "SPAs" });
expect(spaTab).toBeInTheDocument();
Expand All @@ -192,12 +199,13 @@ describe("Dashboard", () => {
it("should handle switching back to the SPA tab", async () => {
const isWaiver = false;

const spaTab = screen.queryByRole("heading", { level: 2, name: "SPAs" });

await user.click(spaTab.parentElement);
await waitFor(async () => {
const spaTab = await screen.findByRole("heading", { level: 2, name: "SPAs" });
await user.click(spaTab.parentElement);

expect(spaTab).toBeInTheDocument();
expect(spaTab.parentElement.getAttribute("aria-selected")).toEqual("true");
expect(spaTab).toBeInTheDocument();
expect(spaTab.parentElement.getAttribute("aria-selected")).toEqual("true");
});

const waiverTab = screen.queryByRole("heading", { level: 2, name: "Waivers" });
expect(waiverTab).toBeInTheDocument();
Expand Down Expand Up @@ -266,11 +274,13 @@ describe("Dashboard", () => {
it("should handle switching to the Waiver tab", async () => {
const isWaiver = true;

const waiverTab = screen.queryByRole("heading", { level: 2, name: "Waivers" });
await user.click(waiverTab.parentElement);
await waitFor(async () => {
const waiverTab = await screen.findByRole("heading", { level: 2, name: "Waivers" });
await user.click(waiverTab.parentElement);

expect(waiverTab).toBeInTheDocument();
expect(waiverTab.parentElement.getAttribute("aria-selected")).toEqual("true");
expect(waiverTab).toBeInTheDocument();
expect(waiverTab.parentElement.getAttribute("aria-selected")).toEqual("true");
});

const spaTab = screen.queryByRole("heading", { level: 2, name: "SPAs" });
expect(spaTab).toBeInTheDocument();
Expand All @@ -284,11 +294,13 @@ describe("Dashboard", () => {
it("should handle switching back to the SPA tab", async () => {
const isWaiver = false;

const spaTab = screen.queryByRole("heading", { level: 2, name: "SPAs" });
await user.click(spaTab.parentElement);
await waitFor(async () => {
const spaTab = screen.queryByRole("heading", { level: 2, name: "SPAs" });
await user.click(spaTab.parentElement);

expect(spaTab).toBeInTheDocument();
expect(spaTab.parentElement.getAttribute("aria-selected")).toEqual("true");
expect(spaTab).toBeInTheDocument();
expect(spaTab.parentElement.getAttribute("aria-selected")).toEqual("true");
});

const waiverTab = screen.queryByRole("heading", { level: 2, name: "Waivers" });
expect(waiverTab).toBeInTheDocument();
Expand Down
2 changes: 1 addition & 1 deletion react-app/src/features/dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const Dashboard = () => {
);
};

if (isLoading) {
if (isLoading || osData.tabLoading) {
return <LoadingSpinner />;
}

Expand Down
Loading