Skip to content

Commit 6deb3ab

Browse files
committed
clear the error message query param after the first time it's seen
1 parent 672f171 commit 6deb3ab

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

frontends/main/src/app-pages/DashboardPage/HomeContent.test.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import * as mitxonline from "api/mitxonline-test-utils"
1818
import { useFeatureFlagEnabled } from "posthog-js/react"
1919
import HomeContent from "./HomeContent"
2020
import invariant from "tiny-invariant"
21+
import * as NextProgressBar from "next-nprogress-bar"
2122

2223
jest.mock("posthog-js/react")
24+
jest.mock("next-nprogress-bar")
2325
const mockedUseFeatureFlagEnabled = jest
2426
.mocked(useFeatureFlagEnabled)
2527
.mockImplementation(() => false)
@@ -251,8 +253,15 @@ describe("HomeContent", () => {
251253
expect(screen.queryByText("Enrollment Error")).not.toBeInTheDocument()
252254
})
253255

254-
test("Displays enrollment error alert when query param is present", async () => {
256+
test("Displays enrollment error alert when query param is present and then clears it", async () => {
255257
setupAPIs()
258+
const mockReplace = jest.fn()
259+
jest.spyOn(NextProgressBar, "useRouter").mockReturnValue({
260+
replace: mockReplace,
261+
} as Partial<ReturnType<typeof NextProgressBar.useRouter>> as ReturnType<
262+
typeof NextProgressBar.useRouter
263+
>)
264+
256265
renderWithProviders(<HomeContent />, {
257266
url: "/dashboard?enrollment_error=1",
258267
})
@@ -261,12 +270,37 @@ describe("HomeContent", () => {
261270
name: "Your MIT Learning Journey",
262271
})
263272

273+
// Verify the alert was shown
264274
expect(screen.getByText("Enrollment Error")).toBeInTheDocument()
265275
expect(
266276
screen.getByText(
267277
/The Enrollment Code is incorrect or no longer available/,
268278
),
269279
).toBeInTheDocument()
270280
expect(screen.getByText("Contact Support")).toBeInTheDocument()
281+
282+
// Verify the query param is cleared
283+
expect(mockReplace).toHaveBeenCalledWith("/dashboard")
284+
})
285+
286+
test("Does not clear query param when it is not present", async () => {
287+
setupAPIs()
288+
const mockReplace = jest.fn()
289+
jest.spyOn(NextProgressBar, "useRouter").mockReturnValue({
290+
replace: mockReplace,
291+
} as Partial<ReturnType<typeof NextProgressBar.useRouter>> as ReturnType<
292+
typeof NextProgressBar.useRouter
293+
>)
294+
295+
renderWithProviders(<HomeContent />, {
296+
url: "/dashboard",
297+
})
298+
299+
await screen.findByRole("heading", {
300+
name: "Your MIT Learning Journey",
301+
})
302+
303+
// Verify router.replace was not called
304+
expect(mockReplace).not.toHaveBeenCalled()
271305
})
272306
})

frontends/main/src/app-pages/DashboardPage/HomeContent.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { FeatureFlags } from "@/common/feature_flags"
1919
import { useUserMe } from "api/hooks/user"
2020
import { OrganizationCards } from "./CoursewareDisplay/OrganizationCards"
2121
import { useSearchParams } from "next/navigation"
22+
import { useRouter } from "next-nprogress-bar"
2223

2324
const SubTitleText = styled(Typography)(({ theme }) => ({
2425
color: theme.custom.colors.darkGray2,
@@ -73,6 +74,7 @@ const AlertBanner = styled(Alert)({
7374

7475
const HomeContent: React.FC = () => {
7576
const searchParams = useSearchParams()
77+
const router = useRouter()
7678
const enrollmentError = searchParams.get(ENROLLMENT_ERROR_QUERY_PARAM)
7779
const { isLoading: isLoadingProfile, data: user } = useUserMe()
7880
const topics = user?.profile?.preference_search_filters.topic
@@ -81,6 +83,19 @@ const HomeContent: React.FC = () => {
8183
FeatureFlags.EnrollmentDashboard,
8284
)
8385
const supportEmail = process.env.NEXT_PUBLIC_MITOL_SUPPORT_EMAIL || ""
86+
87+
// Clear the enrollment error query param on mount so it doesn't persist on reload/back navigation
88+
React.useEffect(() => {
89+
if (enrollmentError) {
90+
const newParams = new URLSearchParams(searchParams.toString())
91+
newParams.delete(ENROLLMENT_ERROR_QUERY_PARAM)
92+
const newUrl = newParams.toString()
93+
? `${window.location.pathname}?${newParams.toString()}`
94+
: window.location.pathname
95+
router.replace(newUrl)
96+
}
97+
}, [enrollmentError, searchParams, router])
98+
8499
return (
85100
<>
86101
<HomeHeader>

0 commit comments

Comments
 (0)