refactor media page to support pagination

This commit is contained in:
Cory Dransfeldt 2022-06-07 18:30:45 -07:00
parent 468ff48bfe
commit 012acdb869
7 changed files with 131 additions and 25 deletions

View file

@ -1,11 +1,12 @@
import Link from '@/components/Link' import Link from '@/components/Link'
interface Props { interface Props {
page: string
totalPages: number totalPages: number
currentPage: number currentPage: number
} }
export default function Pagination({ totalPages, currentPage }: Props) { export default function Pagination({ page, totalPages, currentPage }: Props) {
const prevPage = currentPage - 1 > 0 const prevPage = currentPage - 1 > 0
const nextPage = currentPage + 1 <= totalPages const nextPage = currentPage + 1 <= totalPages
@ -18,7 +19,7 @@ export default function Pagination({ totalPages, currentPage }: Props) {
</button> </button>
)} )}
{prevPage && ( {prevPage && (
<Link href={currentPage - 1 === 1 ? `/blog/` : `/blog/page/${currentPage - 1}`}> <Link href={currentPage - 1 === 1 ? `/${page}/` : `/${page}/page/${currentPage - 1}`}>
<button>Previous</button> <button>Previous</button>
</Link> </Link>
)} )}
@ -31,7 +32,7 @@ export default function Pagination({ totalPages, currentPage }: Props) {
</button> </button>
)} )}
{nextPage && ( {nextPage && (
<Link href={`/blog/page/${currentPage + 1}`}> <Link href={`/${page}/page/${currentPage + 1}`}>
<button>Next</button> <button>Next</button>
</Link> </Link>
)} )}

41
layouts/ MediaList.tsx Normal file
View file

@ -0,0 +1,41 @@
import MediaItem from '@/components/MediaItem'
import Pagination from '@/components/Pagination'
import { ComponentProps } from 'react'
interface Props {
initialDisplayPosts?: { type: string; title: string; data: string | string[] }[]
pagination?: ComponentProps<typeof Pagination>
}
const MediaList = ({ initialDisplayPosts = [], pagination }: Props) => {
return (
<>
<div className="divide-y divide-gray-200 dark:divide-gray-700">
<div className="space-y-2 pt-6 pb-8 md:space-y-5">
<h1 className="text-3xl font-extrabold leading-9 tracking-tight text-gray-900 dark:text-gray-100 sm:text-4xl sm:leading-10 md:text-6xl md:leading-14">
Media
</h1>
</div>
<div className="container py-12">
<div className="flex flex-wrap">
{initialDisplayPosts.length ? (
initialDisplayPosts.map((d) => (
<MediaItem key={d.title} type={d.type} title={d.title} data={d.data} />
))
) : (
<p className="pb-2">No media found.</p>
)}
</div>
</div>
</div>
{pagination && pagination.totalPages > 1 && (
<Pagination
page="media"
currentPage={pagination.currentPage}
totalPages={pagination.totalPages}
/>
)}
</>
)
}
export default MediaList

View file

@ -90,7 +90,11 @@ export default function ListLayout({ posts, title, initialDisplayPosts = [], pag
</ul> </ul>
</div> </div>
{pagination && pagination.totalPages > 1 && !searchValue && ( {pagination && pagination.totalPages > 1 && !searchValue && (
<Pagination currentPage={pagination.currentPage} totalPages={pagination.totalPages} /> <Pagination
page="blog"
currentPage={pagination.currentPage}
totalPages={pagination.totalPages}
/>
)} )}
</> </>
) )

View file

@ -15,6 +15,7 @@ export const getStaticProps: GetStaticProps<{
const posts = await getAllFilesFrontMatter('blog') const posts = await getAllFilesFrontMatter('blog')
const initialDisplayPosts = posts.slice(0, POSTS_PER_PAGE) const initialDisplayPosts = posts.slice(0, POSTS_PER_PAGE)
const pagination = { const pagination = {
page: 'blog',
currentPage: 1, currentPage: 1,
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE), totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),
} }

View file

@ -22,7 +22,7 @@ export const getStaticPaths: GetStaticPaths<{ page: string }> = async () => {
export const getStaticProps: GetStaticProps<{ export const getStaticProps: GetStaticProps<{
posts: PostFrontMatter[] posts: PostFrontMatter[]
initialDisplayPosts: PostFrontMatter[] initialDisplayPosts: PostFrontMatter[]
pagination: { currentPage: number; totalPages: number } pagination: { page: string; currentPage: number; totalPages: number }
}> = async (context) => { }> = async (context) => {
const { const {
params: { page }, params: { page },
@ -34,6 +34,7 @@ export const getStaticProps: GetStaticProps<{
POSTS_PER_PAGE * pageNumber POSTS_PER_PAGE * pageNumber
) )
const pagination = { const pagination = {
page: 'blog',
currentPage: pageNumber, currentPage: pageNumber,
totalPages: Math.ceil(posts.length / POSTS_PER_PAGE), totalPages: Math.ceil(posts.length / POSTS_PER_PAGE),
} }

View file

@ -1,30 +1,34 @@
import siteMetadata from '@/data/siteMetadata' import siteMetadata from '@/data/siteMetadata'
import MediaList from '@/layouts/ MediaList'
import mediaData from '@/data/mediaData' import mediaData from '@/data/mediaData'
import { PageSEO } from '@/components/SEO' import { PageSEO } from '@/components/SEO'
import MediaItem from '@/components/MediaItem' import { GetStaticProps, InferGetStaticPropsType } from 'next'
import { ComponentProps } from 'react'
export default function Media() { export const POSTS_PER_PAGE = 5
export const getStaticProps: GetStaticProps<{
initialDisplayPosts: ComponentProps<typeof MediaList>['initialDisplayPosts']
pagination: ComponentProps<typeof MediaList>['pagination']
}> = async () => {
const initialDisplayPosts = mediaData.slice(0, POSTS_PER_PAGE)
const pagination = {
page: 'blog',
currentPage: 1,
totalPages: Math.ceil(mediaData.length / POSTS_PER_PAGE),
}
return { props: { initialDisplayPosts, pagination } }
}
export default function Media({
initialDisplayPosts,
pagination,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return ( return (
<> <>
<PageSEO title={`Media - ${siteMetadata.author}`} description={siteMetadata.description} /> <PageSEO title={`Media - ${siteMetadata.author}`} description={siteMetadata.description} />
<div className="divide-y divide-gray-200 dark:divide-gray-700"> <MediaList initialDisplayPosts={initialDisplayPosts} pagination={pagination} />
<div className="space-y-2 pt-6 pb-8 md:space-y-5">
<h1 className="text-3xl font-extrabold leading-9 tracking-tight text-gray-900 dark:text-gray-100 sm:text-4xl sm:leading-10 md:text-6xl md:leading-14">
Media
</h1>
</div>
<div className="container py-12">
<div className="flex flex-wrap">
{mediaData.length ? (
mediaData.map((d) => (
<MediaItem key={d.title} type={d.type} title={d.title} data={d.data} />
))
) : (
<p className="pb-2">No media found.</p>
)}
</div>
</div>
</div>
</> </>
) )
} }

View file

@ -0,0 +1,54 @@
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from 'next'
import siteMetadata from '@/data/siteMetadata'
import mediaData from '@/data/mediaData'
import { PageSEO } from '@/components/SEO'
import MediaList from '@/layouts/ MediaList'
import { POSTS_PER_PAGE } from '../../media'
export const getStaticPaths: GetStaticPaths<{ page: string }> = async () => {
const totalPages = Math.ceil(mediaData.length / POSTS_PER_PAGE)
const paths = Array.from({ length: totalPages }, (_, i) => ({
params: { page: (i + 1).toString() },
}))
return {
paths,
fallback: false,
}
}
export const getStaticProps: GetStaticProps = async (context) => {
const {
params: { page },
} = context
const pageNumber = parseInt(page as string)
const initialDisplayPosts = mediaData.slice(
POSTS_PER_PAGE * (pageNumber - 1),
POSTS_PER_PAGE * pageNumber
)
const pagination = {
page: 'media',
currentPage: pageNumber,
totalPages: Math.ceil(mediaData.length / POSTS_PER_PAGE),
}
return {
props: {
initialDisplayPosts,
pagination,
},
}
}
export default function Media({
initialDisplayPosts,
pagination,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return (
<>
<PageSEO title={`Media - ${siteMetadata.author}`} description={siteMetadata.description} />
<MediaList initialDisplayPosts={initialDisplayPosts} pagination={pagination} />
</>
)
}