Repository navigation
DINO-839: Add user submission metrics to the frontend #1048
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
js/src/app/user/[userId]/_components/UserProfile/MetricsChart/MetricsChart.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| import MetricsChart from "@/app/user/[userId]/_components/UserProfile/MetricsChart/MetricsChart.tsx"; | ||
| import { ApiURL } from "@/lib/api/common/apiURL"; | ||
| import { TestUtils, TestUtilTypes } from "@/lib/test"; | ||
| import { cleanup, screen, waitFor } from "@testing-library/react"; | ||
| import { http, HttpResponse } from "msw"; | ||
| import { setupServer } from "msw/node"; | ||
| import { v4 as uuid } from "uuid"; | ||
|
|
||
| const FIXED_USER_ID = "metrics-test-user"; | ||
|
|
||
| const userMetricsUrl = ApiURL.create("/api/user/{userId}/metrics", { | ||
| method: "GET", | ||
| params: { userId: FIXED_USER_ID }, | ||
| }); | ||
|
|
||
| const MOCK_METRICS = [ | ||
| { | ||
| id: "metric-1", | ||
| userId: FIXED_USER_ID, | ||
| points: 500, | ||
| createdAt: new Date(Date.UTC(2026, 9, 1)).toISOString(), | ||
| }, | ||
| { | ||
| id: "metric-2", | ||
| userId: FIXED_USER_ID, | ||
| points: 1500, | ||
| createdAt: new Date(Date.UTC(2026, 9, 2)).toISOString(), | ||
| }, | ||
| { | ||
| id: "metric-3", | ||
| userId: FIXED_USER_ID, | ||
| points: 2000, | ||
| createdAt: new Date(Date.UTC(2026, 9, 3)).toISOString(), | ||
| }, | ||
| { | ||
| id: "metric-4", | ||
| userId: FIXED_USER_ID, | ||
| points: 3000, | ||
| createdAt: new Date(Date.UTC(2026, 9, 4)).toISOString(), | ||
| }, | ||
| { | ||
| id: "metric-5", | ||
| userId: FIXED_USER_ID, | ||
| points: 3000, | ||
| createdAt: new Date(Date.UTC(2026, 9, 5)).toISOString(), | ||
| }, | ||
| { | ||
| id: "metric-6", | ||
| userId: FIXED_USER_ID, | ||
| points: 3000, | ||
| createdAt: new Date(Date.UTC(2026, 9, 6)).toISOString(), | ||
| }, | ||
| ]; | ||
|
|
||
| const userMetricsSuccessHandler = http.get(userMetricsUrl.url.toString(), () => | ||
| HttpResponse.json({ | ||
| success: true, | ||
| message: "Metrics loaded!", | ||
| payload: { | ||
| hasNextPage: false, | ||
| pages: 1, | ||
| items: MOCK_METRICS, | ||
| success: true, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| const userMetricsErrorHandler = http.get(userMetricsUrl.url.toString(), () => | ||
| HttpResponse.error(), | ||
| ); | ||
|
|
||
| const mockUseUserMetricsQuery = vi.fn(); | ||
|
|
||
| vi.mock("@/lib/api/queries/user", () => ({ | ||
| useUserMetricsQuery: (...args: unknown[]) => mockUseUserMetricsQuery(...args), | ||
| })); | ||
|
|
||
| const BASE_QUERY_RESULT = { | ||
| data: undefined, | ||
| status: "pending", | ||
| page: 1, | ||
| goBack: vi.fn(), | ||
| goForward: vi.fn(), | ||
| goTo: vi.fn(), | ||
| }; | ||
|
|
||
| describe("MetricsChart succeeded", () => { | ||
| afterEach(() => { | ||
| cleanup(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| let renderProviderFn: TestUtilTypes.RenderWithAllProvidersFn | null = null; | ||
| beforeEach(() => { | ||
| renderProviderFn = TestUtils.getRenderWithAllProvidersFn(); | ||
| mockUseUserMetricsQuery.mockReturnValue(BASE_QUERY_RESULT); | ||
| }); | ||
|
|
||
| it("should render skeleton metrics chart initially", () => { | ||
| const MOCK_USER_ID = uuid(); | ||
| renderProviderFn?.(<MetricsChart userId={MOCK_USER_ID} />); | ||
| const element = screen.getByTestId("user-metrics-chart-skeleton"); | ||
| expect(element).toBeInTheDocument(); | ||
| expect(element).toBeVisible(); | ||
| }); | ||
|
|
||
| it("should render AreaChart eventually", async () => { | ||
| mockUseUserMetricsQuery.mockReturnValue({ | ||
| ...BASE_QUERY_RESULT, | ||
| status: "success", | ||
| data: { | ||
| payload: { items: [], pages: 1, hasNextPage: false }, | ||
| success: true, | ||
| }, | ||
| }); | ||
| const MOCK_USER_ID = uuid(); | ||
| renderProviderFn?.(<MetricsChart userId={MOCK_USER_ID} />); | ||
| const element = await screen.findByTestId(/metrics-area-chart.*$/); | ||
| expect(element).toBeInTheDocument(); | ||
| expect(element).toBeVisible(); | ||
| }); | ||
|
|
||
| it("should show data message toast and not render AreaChart when data is not successful", async () => { | ||
| const mockMessage = "Data not success."; | ||
| mockUseUserMetricsQuery.mockReturnValue({ | ||
| ...BASE_QUERY_RESULT, | ||
| status: "success", | ||
| data: { | ||
| payload: { items: [], pages: 1, hasNextPage: false }, | ||
| success: false, | ||
| message: mockMessage, | ||
| }, | ||
| }); | ||
| const MOCK_USER_ID = uuid(); | ||
| renderProviderFn?.(<MetricsChart userId={MOCK_USER_ID} />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText(mockMessage)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect( | ||
| screen.queryByTestId(/metrics-area-chart.*$/), | ||
| ).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("UserMetricsChart with successful API", () => { | ||
| const server = setupServer(userMetricsSuccessHandler); | ||
|
|
||
| beforeAll(() => server.listen()); | ||
| afterEach(() => { | ||
| server.resetHandlers(); | ||
| cleanup(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
| afterAll(() => server.close()); | ||
|
|
||
| let renderProviderFn: TestUtilTypes.RenderWithAllProvidersFn | null = null; | ||
| beforeEach(() => { | ||
| renderProviderFn = TestUtils.getRenderWithAllProvidersFn(); | ||
|
|
||
| mockUseUserMetricsQuery.mockReturnValue({ | ||
| ...BASE_QUERY_RESULT, | ||
| status: "success", | ||
| data: { | ||
| payload: { items: MOCK_METRICS, pages: 2, hasNextPage: true }, | ||
| success: true, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("should have formatted dates for chart after successful API call", async () => { | ||
| renderProviderFn?.(<MetricsChart userId={FIXED_USER_ID} />); | ||
|
|
||
| const element = await screen.findByTestId( | ||
| /metrics-area-chart Oct 01, 2026.*$/, | ||
| ); | ||
| expect(element).toBeInTheDocument(); | ||
| expect(element).toBeVisible(); | ||
| }); | ||
|
|
||
| it("should have correct userMetrics pages and hasNextPage based on data payload", async () => { | ||
| renderProviderFn?.(<MetricsChart userId={FIXED_USER_ID} />); | ||
|
|
||
| const element = await screen.findByTestId( | ||
| "metrics-area-chart Oct 01, 2026 pages2 hasNextPagetrue", | ||
| ); | ||
| expect(element).toBeInTheDocument(); | ||
| expect(element).toBeVisible(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("UserMetricsChart error state", () => { | ||
| const server = setupServer(userMetricsErrorHandler); | ||
|
|
||
| beforeAll(() => server.listen()); | ||
| afterEach(() => { | ||
| server.resetHandlers(); | ||
| cleanup(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
| afterAll(() => server.close()); | ||
|
|
||
| let renderProviderFn: TestUtilTypes.RenderWithAllProvidersFn | null = null; | ||
| beforeEach(() => { | ||
| renderProviderFn = TestUtils.getRenderWithAllProvidersFn(); | ||
| mockUseUserMetricsQuery.mockReturnValue({ | ||
| ...BASE_QUERY_RESULT, | ||
| status: "error", | ||
| data: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| it("should show error toast when API errors", async () => { | ||
| renderProviderFn?.(<MetricsChart userId={FIXED_USER_ID} />); | ||
|
|
||
| await waitFor(() => { | ||
| expect( | ||
| screen.getByText(/sorry, something went wrong/i), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
68 changes: 68 additions & 0 deletions
68
js/src/app/user/[userId]/_components/UserProfile/MetricsChart/MetricsChart.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import MetricsChartSkeleton from "@/app/user/[userId]/_components/UserProfile/MetricsChart/MetricsChartSkeleton"; | ||
| import Paginator from "@/components/ui/table/Paginator"; | ||
| import Toast from "@/components/ui/toast/Toast"; | ||
| import { useUserMetricsQuery } from "@/lib/api/queries/user"; | ||
| import { theme } from "@/lib/theme"; | ||
| import { AreaChart } from "@mantine/charts"; | ||
| import { Box, Divider } from "@mantine/core"; | ||
| import "@mantine/charts/styles.css"; | ||
| import d from "dayjs"; | ||
| import utc from "dayjs/plugin/utc"; | ||
|
|
||
| d.extend(utc); | ||
|
|
||
| export default function MetricsChart({ userId }: { readonly userId: string }) { | ||
| const { data, status, page, goBack, goForward, goTo } = useUserMetricsQuery({ | ||
| userId, | ||
| }); | ||
|
|
||
| if (status === "pending") { | ||
| return <MetricsChartSkeleton />; | ||
| } | ||
|
|
||
| if (status === "error") { | ||
| return ( | ||
| <Toast message="Sorry, something went wrong. Please try again later." /> | ||
| ); | ||
| } | ||
|
|
||
| if (!data.success) { | ||
| return <Toast message={data.message} />; | ||
| } | ||
|
|
||
| const userMetrics = data.payload; | ||
| const pageData = userMetrics.items.map((item) => { | ||
|
angelayu0530 marked this conversation as resolved.
Resolved
|
||
| return { | ||
| ...item, | ||
| createdAt: d(item.createdAt).utc().format("MMM DD, YYYY"), | ||
| }; | ||
| }); | ||
|
|
||
| return ( | ||
| <> | ||
| <Divider w={"70%"} /> | ||
| <Box w={"90%"}> | ||
| <AreaChart | ||
| h={250} | ||
| data={pageData} | ||
| dataKey="createdAt" | ||
| data-testId={`metrics-area-chart ${pageData[0]?.createdAt} pages${userMetrics?.pages ?? 0} hasNextPage${userMetrics?.hasNextPage ?? false}`} | ||
| series={[ | ||
| { name: "points", label: "Points", color: theme.colors.patina[4] }, | ||
| ]} | ||
| curveType="natural" | ||
| tickLine="none" | ||
| gridAxis="xy" | ||
| /> | ||
| </Box> | ||
| <Paginator | ||
| pages={userMetrics?.pages ?? 0} | ||
| currentPage={page} | ||
| hasNextPage={userMetrics?.hasNextPage ?? false} | ||
| goBack={goBack} | ||
| goForward={goForward} | ||
| goTo={goTo} | ||
| /> | ||
| </> | ||
| ); | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
js/src/app/user/[userId]/_components/UserProfile/MetricsChart/MetricsChartSkeleton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Flex, Skeleton, Divider } from "@mantine/core"; | ||
|
|
||
| export default function MetricsChartSkeleton() { | ||
| return ( | ||
| <Flex direction={"column"} align={"center"} gap={"sm"} w={"100%"}> | ||
| <Divider w={"70%"} /> | ||
| <Skeleton | ||
| data-testid="user-metrics-chart-skeleton" | ||
| height={250} | ||
| width={"70%"} | ||
| mx={"md"} | ||
| /> | ||
| </Flex> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.