Skip to content

Commit b153838

Browse files
committed
adding infinte query, working through transforms and refetching
1 parent a99710e commit b153838

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

frontends/api/src/mitxonline/hooks/courses/queries.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { queryOptions } from "@tanstack/react-query"
1+
import { infiniteQueryOptions, queryOptions } from "@tanstack/react-query"
22
import type {
33
CoursesApiApiV2CoursesListRequest,
44
PaginatedCourseWithCourseRunsSerializerV2List,
@@ -23,6 +23,18 @@ const coursesQueries = {
2323
return coursesApi.apiV2CoursesList(opts).then((res) => res.data)
2424
},
2525
}),
26+
coursesListInfinite: (opts?: CoursesApiApiV2CoursesListRequest) =>
27+
infiniteQueryOptions({
28+
queryKey: coursesKeys.coursesList(opts),
29+
queryFn:
30+
async ({ pageParam = 1 }): Promise<PaginatedCourseWithCourseRunsSerializerV2List> => {
31+
return coursesApi.apiV2CoursesList({ ...opts, page: pageParam }).then((res) => res.data)
32+
},
33+
initialPageParam: 1,
34+
getNextPageParam: (lastPage, allPages, pageParam): number => {
35+
return lastPage && lastPage.next ? (pageParam ? pageParam + 1 : 1) : 1
36+
},
37+
}),
2638
}
2739

2840
export { coursesQueries, coursesKeys }

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use client"
22

3-
import React from "react"
3+
import React, { useMemo } from "react"
44
import DOMPurify from "dompurify"
55
import Image from "next/image"
66
import { useFeatureFlagEnabled } from "posthog-js/react"
77
import { FeatureFlags } from "@/common/feature_flags"
8-
import { useQueries, useQuery } from "@tanstack/react-query"
8+
import { useInfiniteQuery, useQueries, useQuery } from "@tanstack/react-query"
99
import {
1010
programsQueries,
1111
programCollectionQueries,
@@ -26,6 +26,7 @@ import {
2626
CourseRunEnrollment,
2727
OrganizationPage,
2828
UserProgramEnrollmentDetail,
29+
CourseWithCourseRunsSerializerV2,
2930
} from "@mitodl/mitxonline-api-axios/v2"
3031
import { useMitxOnlineCurrentUser } from "api/mitxonline-hooks/user"
3132
import { ButtonLink } from "@mitodl/smoot-design"
@@ -249,15 +250,24 @@ const OrgProgramDisplay: React.FC<{
249250
(enrollment) => enrollment.program.id === program.id,
250251
)
251252
const hasValidCertificate = !!programEnrollment?.certificate
252-
const courses = useQuery(
253-
coursesQueries.coursesList({ id: program.courseIds, org_id: orgId }),
253+
const courses = useInfiniteQuery(
254+
coursesQueries.coursesListInfinite({ id: program.courseIds, org_id: orgId }),
254255
)
255256
const skeleton = (
256257
<Skeleton width="100%" height="65px" style={{ marginBottom: "16px" }} />
257258
)
258259
if (programLoading || courses.isLoading) return skeleton
260+
if (!courses.isLoading) console.log(courses.data)
259261
const transformedCourses = transform.mitxonlineOrgCourses({
260-
courses: courses.data?.results ?? [],
262+
courses: (() =>{
263+
let courseData: Array<CourseWithCourseRunsSerializerV2> = []
264+
265+
for (const page of courses.data?.pages || []) {
266+
courseData = courseData.concat(page.results)
267+
}
268+
269+
return courseData
270+
})(),
261271
contracts: contracts ?? [],
262272
enrollments: courseRunEnrollments ?? [],
263273
})

0 commit comments

Comments
 (0)