-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
165 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
74 changes: 74 additions & 0 deletions
74
src/app/(default)/community/_components/infinite-scroll-art.tsx
This file contains 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,74 @@ | ||
'use client'; | ||
|
||
import { ArtCards, SkeletonCards } from '@/components/art-cards'; | ||
import type { ArtsResponse, Resolution } from '@/types'; | ||
import { useInfiniteQuery } from '@tanstack/react-query'; | ||
import { useEffect } from 'react'; | ||
import { useInView } from 'react-intersection-observer'; | ||
|
||
export const InfiniteScrollArt = ({ | ||
resolution, | ||
}: { resolution: Resolution }) => { | ||
const { ref, inView } = useInView(); | ||
|
||
const fetchArts = async ({ | ||
pageParam, | ||
}: { pageParam: number }): Promise<ArtsResponse> => { | ||
const seartchParams = new URLSearchParams(); | ||
seartchParams.set('cursor', pageParam.toString()); | ||
seartchParams.set('width', resolution === 'fullhd' ? '26' : '27'); | ||
|
||
const res = await fetch(`/api/arts/?${seartchParams}`); | ||
return res.json(); | ||
}; | ||
|
||
const { | ||
data, | ||
error, | ||
fetchNextPage, | ||
hasNextPage, | ||
isFetching, | ||
isFetchingNextPage, | ||
status, | ||
} = useInfiniteQuery({ | ||
queryKey: ['arts'], | ||
queryFn: fetchArts, | ||
initialPageParam: 0, | ||
getNextPageParam: (lastPage) => lastPage.next, | ||
}); | ||
|
||
useEffect(() => { | ||
if (inView) { | ||
fetchNextPage(); | ||
} | ||
}, [fetchNextPage, inView]); | ||
|
||
return status === 'pending' ? ( | ||
<SkeletonCards /> | ||
) : status === 'error' ? ( | ||
<p className='text-center'>Error: {error.message}</p> | ||
) : ( | ||
<> | ||
{data.pages.map((page, i) => ( | ||
<ArtCards arts={page.data ?? []} key={i.toString()} /> | ||
))} | ||
<div className='text-center mt-8'> | ||
<button | ||
type='button' | ||
ref={ref} | ||
onClick={() => fetchNextPage()} | ||
disabled={!hasNextPage || isFetchingNextPage} | ||
> | ||
{isFetchingNextPage | ||
? 'Loading more...' | ||
: hasNextPage | ||
? 'Load Newer' | ||
: 'Nothing more to load'} | ||
</button> | ||
</div> | ||
<div className='text-center mt-8'> | ||
{isFetching && !isFetchingNextPage ? 'Fetching...' : null} | ||
</div> | ||
</> | ||
); | ||
}; |
This file contains 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,14 @@ | ||
'use client'; | ||
|
||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import type { ReactNode } from 'react'; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
const Layout = ({ children }: { children: ReactNode }) => { | ||
return ( | ||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider> | ||
); | ||
}; | ||
|
||
export default Layout; |
This file contains 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
This file contains 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,47 @@ | ||
import { prisma } from '@/client'; | ||
import { type NextRequest, NextResponse } from 'next/server'; | ||
|
||
export const GET = async (req: NextRequest) => { | ||
const pageSize = 20; | ||
|
||
const searchParams = req.nextUrl.searchParams; | ||
const cursor = Number.parseInt(searchParams.get('cursor') ?? '0'); | ||
const width = Number.parseInt(searchParams.get('width') ?? '26'); | ||
|
||
if (width !== 26 && width !== 27) { | ||
return NextResponse.json({ success: false }, { status: 400 }); | ||
} | ||
|
||
if (Number.isNaN(cursor)) { | ||
return NextResponse.json({ success: false }, { status: 400 }); | ||
} | ||
|
||
try { | ||
const artsCount = await prisma.art.count({ | ||
where: { | ||
width, | ||
}, | ||
}); | ||
|
||
if (cursor > artsCount) { | ||
return NextResponse.json({ success: false }, { status: 404 }); | ||
} | ||
|
||
const arts = await prisma.art.findMany({ | ||
orderBy: { | ||
createdAt: 'desc', | ||
}, | ||
where: { | ||
width, | ||
}, | ||
skip: cursor, | ||
take: pageSize, | ||
}); | ||
|
||
const next = cursor + pageSize < artsCount ? cursor + pageSize : null; | ||
|
||
return NextResponse.json({ success: true, data: arts, next }); | ||
} catch { | ||
return NextResponse.json({ success: false }, { status: 500 }); | ||
} | ||
}; |
This file contains 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
This file contains 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
This file contains 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