From ec54dbf6e05cc1c7a3195d2f153e4d2b4f5f46f2 Mon Sep 17 00:00:00 2001 From: Julian Dominguez-Schatz Date: Sun, 17 Sep 2023 13:50:18 -0400 Subject: [PATCH] PR feedback: extract common component --- .../pages/teacher/DisplayAssessmentsPage.tsx | 38 ++------------- .../pages/teacher/TeacherDashboardPage.tsx | 2 +- .../teacher/dashboard/AssessmentsSection.tsx | 38 ++------------- .../teacher/view-sessions/TestSessionTabs.tsx | 47 +++++++++++++++++++ .../view-sessions/useAssessmentDataQuery.ts | 22 ++++++++- 5 files changed, 76 insertions(+), 71 deletions(-) create mode 100644 frontend/src/components/teacher/view-sessions/TestSessionTabs.tsx diff --git a/frontend/src/components/pages/teacher/DisplayAssessmentsPage.tsx b/frontend/src/components/pages/teacher/DisplayAssessmentsPage.tsx index 65fb5720b..40080dcbb 100644 --- a/frontend/src/components/pages/teacher/DisplayAssessmentsPage.tsx +++ b/frontend/src/components/pages/teacher/DisplayAssessmentsPage.tsx @@ -1,26 +1,15 @@ import React, { useMemo } from "react"; -import { - Box, - Center, - Tab, - TabList, - TabPanel, - TabPanels, - Tabs, - VStack, -} from "@chakra-ui/react"; +import { Box, Center, VStack } from "@chakra-ui/react"; import * as Routes from "../../../constants/Routes"; import { TestSessionStatus } from "../../../types/TestSessionTypes"; -import { TEST_SESSION_STATUSES } from "../../../types/TestSessionTypes"; -import { titleCase } from "../../../utils/GeneralUtils"; import HeaderWithButton from "../../common/HeaderWithButton"; import ErrorState from "../../common/info/ErrorState"; import LoadingState from "../../common/info/LoadingState"; import EmptySessionsMessage from "../../common/info/messages/EmptySessionsMessage"; import Pagination from "../../common/table/Pagination"; import usePaginatedData from "../../common/table/usePaginatedData"; -import TestSessionListItem from "../../teacher/view-sessions/TestSessionListItem"; +import TestSessionTabs from "../../teacher/view-sessions/TestSessionTabs"; import useAssessmentDataQuery from "../../teacher/view-sessions/useAssessmentDataQuery"; const DisplayAssessmentsPage = (): React.ReactElement => { @@ -68,28 +57,7 @@ const DisplayAssessmentsPage = (): React.ReactElement => { )} {!!data?.length && !loading && !error && ( <> - setCurrentTab(TEST_SESSION_STATUSES[index])} - > - - {TEST_SESSION_STATUSES.map((status) => ( - {titleCase(status)} - ))} - - - {TEST_SESSION_STATUSES.map((status) => ( - - {paginatedData?.map((session) => ( - - ))} - - ))} - - + {totalPages > 1 && (
, - headerGap: 2, + headerGap: 0, }, ]; diff --git a/frontend/src/components/teacher/dashboard/AssessmentsSection.tsx b/frontend/src/components/teacher/dashboard/AssessmentsSection.tsx index 894e50ae2..3bd420ffa 100644 --- a/frontend/src/components/teacher/dashboard/AssessmentsSection.tsx +++ b/frontend/src/components/teacher/dashboard/AssessmentsSection.tsx @@ -1,23 +1,11 @@ import React, { useMemo, useState } from "react"; -import { - Box, - Center, - Tab, - TabList, - TabPanel, - TabPanels, - Tabs, -} from "@chakra-ui/react"; +import { Box, Center } from "@chakra-ui/react"; -import { - TEST_SESSION_STATUSES, - TestSessionStatus, -} from "../../../types/TestSessionTypes"; -import { titleCase } from "../../../utils/GeneralUtils"; +import { TestSessionStatus } from "../../../types/TestSessionTypes"; import ErrorState from "../../common/info/ErrorState"; import LoadingState from "../../common/info/LoadingState"; import EmptySessionsMessage from "../../common/info/messages/EmptySessionsMessage"; -import TestSessionListItem from "../view-sessions/TestSessionListItem"; +import TestSessionTabs from "../view-sessions/TestSessionTabs"; import useAssessmentDataQuery from "../view-sessions/useAssessmentDataQuery"; const QUERY_DATA_LIMIT_PER_STATUS = 6; @@ -56,25 +44,7 @@ const AssessmentsSection = () => { )} {!!data?.length && !error && !loading && ( - setCurrentTab(TEST_SESSION_STATUSES[index])}> - - {TEST_SESSION_STATUSES.map((status) => ( - {titleCase(status)} - ))} - - - {TEST_SESSION_STATUSES.map((status) => ( - - {sortedData?.map((session) => ( - - ))} - - ))} - - + )} {!data?.length && !loading && !error && ( diff --git a/frontend/src/components/teacher/view-sessions/TestSessionTabs.tsx b/frontend/src/components/teacher/view-sessions/TestSessionTabs.tsx new file mode 100644 index 000000000..22eacbab2 --- /dev/null +++ b/frontend/src/components/teacher/view-sessions/TestSessionTabs.tsx @@ -0,0 +1,47 @@ +import type { ReactElement } from "react"; +import React from "react"; +import { Tab, TabList, TabPanel, TabPanels, Tabs } from "@chakra-ui/react"; + +import type { TestSessionStatus } from "../../../types/TestSessionTypes"; +import { TEST_SESSION_STATUSES } from "../../../types/TestSessionTypes"; +import { titleCase } from "../../../utils/GeneralUtils"; + +import TestSessionListItem from "./TestSessionListItem"; +import type { FormattedAssessmentData } from "./useAssessmentDataQuery"; + +type TestSessionTabsProps = { + data?: FormattedAssessmentData; + setCurrentTab: (status: TestSessionStatus) => void; +}; + +const TestSessionTabs = ({ + data, + setCurrentTab, +}: TestSessionTabsProps): ReactElement => { + return ( + setCurrentTab(TEST_SESSION_STATUSES[index])} + > + + {TEST_SESSION_STATUSES.map((status) => ( + {titleCase(status)} + ))} + + + {TEST_SESSION_STATUSES.map((status) => ( + + {data?.map((session) => ( + + ))} + + ))} + + + ); +}; + +export default TestSessionTabs; diff --git a/frontend/src/components/teacher/view-sessions/useAssessmentDataQuery.ts b/frontend/src/components/teacher/view-sessions/useAssessmentDataQuery.ts index c0a649d19..8b5af102b 100644 --- a/frontend/src/components/teacher/view-sessions/useAssessmentDataQuery.ts +++ b/frontend/src/components/teacher/view-sessions/useAssessmentDataQuery.ts @@ -4,9 +4,29 @@ import { useQuery } from "@apollo/client"; import { GET_TEST_SESSIONS_BY_TEACHER_ID } from "../../../APIClients/queries/TestSessionQueries"; import type { TestSessionOverviewData } from "../../../APIClients/types/TestSessionClientTypes"; import AuthContext from "../../../contexts/AuthContext"; +import type { TestSessionStatus } from "../../../types/TestSessionTypes"; import { getSessionTargetDate } from "../../../utils/TestSessionUtils"; -const useAssessmentDataQuery = (limit?: number) => { +export type FormattedAssessmentData = { + testSessionId: string; + testId: string; + testName: string; + classroomId: string; + classroomName: string; + startDate: Date; + endDate: Date; + targetDate: Date; + status: TestSessionStatus; + accessCode: string; +}[]; + +type AssessmentDataQueryResult = { + loading: boolean; + error?: Error; + data?: FormattedAssessmentData; +}; + +const useAssessmentDataQuery = (limit?: number): AssessmentDataQueryResult => { const { authenticatedUser } = useContext(AuthContext); const { id: teacherId } = authenticatedUser ?? {};