Improve pagination

feat/ai-tutor-redesign
Kamran Ahmed 1 week ago
parent 3918112884
commit ab981b8c88
  1. 51
      src/components/AITutor/AIExploreCourseListing.tsx
  2. 2
      src/components/GenerateCourse/UserCoursesList.tsx
  3. 2
      src/queries/ai-course.ts

@ -2,9 +2,13 @@ import { useListExploreAiCourses } from '../../queries/ai-course';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { AlertCircle, Loader2 } from 'lucide-react'; import { AlertCircle, Loader2 } from 'lucide-react';
import { AICourseCard } from '../GenerateCourse/AICourseCard'; import { AICourseCard } from '../GenerateCourse/AICourseCard';
import { AILoadingState } from './AILoadingState';
import { AITutorHeader } from './AITutorHeader';
import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal';
export function AIExploreCourseListing() { export function AIExploreCourseListing() {
const [isInitialLoading, setIsInitialLoading] = useState(true); const [isInitialLoading, setIsInitialLoading] = useState(true);
const [showUpgradePopup, setShowUpgradePopup] = useState(false);
const { const {
data, data,
@ -23,38 +27,39 @@ export function AIExploreCourseListing() {
const courses = data?.pages.flatMap((page) => page.data) ?? []; const courses = data?.pages.flatMap((page) => page.data) ?? [];
if (isInitialLoading || isExploreAiCoursesLoading) {
return ( return (
<> <AILoadingState
<div className="mb-3 flex min-h-[35px] items-center justify-between max-sm:mb-1"> title="Loading courses"
<div className="flex items-center gap-2"> subtitle="This may take a moment..."
<h2 className="text-lg font-semibold">Explore Courses</h2>
</div>
</div>
{(isExploreAiCoursesLoading || isInitialLoading) && (
<div className="flex min-h-[152px] items-center justify-center gap-2 rounded-lg border border-gray-200 bg-white py-4">
<Loader2
className="size-4 animate-spin text-gray-400"
strokeWidth={2.5}
/> />
<p className="text-sm font-medium text-gray-600">Loading...</p> );
</div> }
)}
{error && !isExploreAiCoursesLoading && !isInitialLoading && ( if (error) {
return (
<div className="flex min-h-[152px] items-center justify-center gap-2 rounded-lg border border-gray-200 bg-white py-4"> <div className="flex min-h-[152px] items-center justify-center gap-2 rounded-lg border border-gray-200 bg-white py-4">
<AlertCircle className="size-4 text-red-500" /> <AlertCircle className="size-4 text-red-500" />
<p className="text-sm font-medium text-red-600"> <p className="text-sm font-medium text-red-600">
{error?.message ?? 'Error loading courses.'} {error?.message ?? 'Error loading courses.'}
</p> </p>
</div> </div>
);
}
return (
<>
{showUpgradePopup && (
<UpgradeAccountModal onClose={() => setShowUpgradePopup(false)} />
)} )}
{!isExploreAiCoursesLoading && <AITutorHeader
courses && title="Explore Courses"
courses.length > 0 && onUpgradeClick={() => setShowUpgradePopup(true)}
!error && ( />
<div className="grid grid-cols-2 gap-2">
{courses && courses.length > 0 && (
<div className="grid grid-cols-3 gap-2">
{courses.map((course) => ( {courses.map((course) => (
<AICourseCard <AICourseCard
key={course._id} key={course._id}
@ -66,7 +71,7 @@ export function AIExploreCourseListing() {
</div> </div>
)} )}
{hasNextPage && !isFetchingNextPage && !error && ( {hasNextPage && !isFetchingNextPage && (
<div className="mt-4 flex items-center justify-center"> <div className="mt-4 flex items-center justify-center">
<button <button
onClick={() => fetchNextPage()} onClick={() => fetchNextPage()}
@ -78,7 +83,7 @@ export function AIExploreCourseListing() {
</div> </div>
)} )}
{isFetchingNextPage && !error && ( {isFetchingNextPage && (
<div className="mt-4 flex items-center justify-center gap-2"> <div className="mt-4 flex items-center justify-center gap-2">
<Loader2 <Loader2
className="size-4 animate-spin text-gray-400" className="size-4 animate-spin text-gray-400"

@ -22,7 +22,7 @@ export function UserCoursesList() {
const [showUpgradePopup, setShowUpgradePopup] = useState(false); const [showUpgradePopup, setShowUpgradePopup] = useState(false);
const [pageState, setPageState] = useState<ListUserAiCoursesQuery>({ const [pageState, setPageState] = useState<ListUserAiCoursesQuery>({
perPage: '10', perPage: '21',
currPage: '1', currPage: '1',
query: '', query: '',
}); });

@ -154,7 +154,7 @@ export function useListExploreAiCourses() {
return httpGet<ListExploreAiCoursesResponse>( return httpGet<ListExploreAiCoursesResponse>(
`/v1-list-explore-ai-courses`, `/v1-list-explore-ai-courses`,
{ {
perPage: '20', perPage: '21',
currPage: String(pageParam), currPage: String(pageParam),
}, },
); );

Loading…
Cancel
Save