From 69ed5d79de5bfe277ab64125aefcc0c3a47e3dbc Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Wed, 9 Apr 2025 01:47:53 +0600 Subject: [PATCH] wip --- .../AITutor/AIFeaturedCoursesListing.tsx | 39 +++++++++++-------- src/queries/ai-course.ts | 32 +++++++++++++++ 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/components/AITutor/AIFeaturedCoursesListing.tsx b/src/components/AITutor/AIFeaturedCoursesListing.tsx index b65cd022f..a5df649ed 100644 --- a/src/components/AITutor/AIFeaturedCoursesListing.tsx +++ b/src/components/AITutor/AIFeaturedCoursesListing.tsx @@ -1,5 +1,6 @@ import { useQuery } from '@tanstack/react-query'; import { + listFeaturedAiCoursesOptions, listUserAiCoursesOptions, type ListUserAiCoursesQuery, } from '../../queries/ai-course'; @@ -8,6 +9,7 @@ import { useEffect, useState } from 'react'; import { Loader2 } from 'lucide-react'; import { getUrlParams, setUrlParams, deleteUrlParam } from '../../lib/browser'; import { AICourseCard } from '../GenerateCourse/AICourseCard'; +import { Pagination } from '../Pagination/Pagination'; type AIFeaturedCoursesListingProps = {}; @@ -15,21 +17,18 @@ export function AIFeaturedCoursesListing(props: AIFeaturedCoursesListingProps) { const [isInitialLoading, setIsInitialLoading] = useState(true); const [pageState, setPageState] = useState({ - perPage: '10', + perPage: '20', currPage: '1', - query: '', }); - const { data: userAiCourses, isFetching: isUserAiCoursesLoading } = useQuery( - listUserAiCoursesOptions(pageState), - queryClient, - ); + const { data: featuredAiCourses, isFetching: isFeaturedAiCoursesLoading } = + useQuery(listFeaturedAiCoursesOptions(pageState), queryClient); useEffect(() => { setIsInitialLoading(false); - }, [userAiCourses]); + }, [featuredAiCourses]); - const courses = userAiCourses?.data ?? []; + const courses = featuredAiCourses?.data ?? []; useEffect(() => { const queryParams = getUrlParams(); @@ -37,19 +36,16 @@ export function AIFeaturedCoursesListing(props: AIFeaturedCoursesListingProps) { setPageState({ ...pageState, currPage: queryParams?.p || '1', - query: queryParams?.q || '', }); }, []); useEffect(() => { - if (pageState?.currPage !== '1' || pageState?.query !== '') { + if (pageState?.currPage !== '1') { setUrlParams({ p: pageState?.currPage || '1', - q: pageState?.query || '', }); } else { deleteUrlParam('p'); - deleteUrlParam('q'); } }, [pageState]); @@ -61,7 +57,7 @@ export function AIFeaturedCoursesListing(props: AIFeaturedCoursesListingProps) { - {(isUserAiCoursesLoading || isInitialLoading) && ( + {(isFeaturedAiCoursesLoading || isInitialLoading) && (
)} - {!isUserAiCoursesLoading && courses && courses.length > 0 && ( + {!isFeaturedAiCoursesLoading && courses && courses.length > 0 && (
{courses.map((course) => ( ))} + + { + setPageState({ ...pageState, currPage: String(page) }); + }} + className="rounded-lg border border-gray-200 bg-white p-4" + />
)} - {!isUserAiCoursesLoading && - (userAiCourses?.data?.length || 0 > 0) && + {!isFeaturedAiCoursesLoading && + (featuredAiCourses?.data?.length || 0 > 0) && courses.length === 0 && (

diff --git a/src/queries/ai-course.ts b/src/queries/ai-course.ts index 0ab1a601c..4f90e2081 100644 --- a/src/queries/ai-course.ts +++ b/src/queries/ai-course.ts @@ -101,6 +101,38 @@ export function listUserAiCoursesOptions( }; } +type ListFeaturedAiCoursesParams = {}; + +type ListFeaturedAiCoursesQuery = { + perPage?: string; + currPage?: string; +}; + +type ListFeaturedAiCoursesResponse = { + data: AICourseWithLessonCount[]; + totalCount: number; + totalPages: number; + currPage: number; + perPage: number; +}; + +export function listFeaturedAiCoursesOptions( + params: ListFeaturedAiCoursesQuery = { + perPage: '10', + currPage: '1', + }, +) { + return { + queryKey: ['featured-ai-courses', params], + queryFn: () => { + return httpGet( + `/v1-list-featured-ai-courses`, + params, + ); + }, + }; +} + type ListExploreAiCoursesParams = {}; type ListExploreAiCoursesQuery = {