feat: add pagination to Haiku detail pages and sort entries

This commit is contained in:
Stefan Imhoff
2023-04-26 18:30:56 +02:00
parent a32217ad9d
commit a6a2b37c9a
2 changed files with 24 additions and 5 deletions

View File

@@ -1,19 +1,29 @@
--- ---
import { getCollection } from 'astro:content'; import { getCollection } from 'astro:content';
import { sortByDate } from '../../utils/sort-by-date';
import BaseLayout from '../../layouts/BaseLayout.astro'; import BaseLayout from '../../layouts/BaseLayout.astro';
import PageHeader from '../../components/PageHeader.astro'; import PageHeader from '../../components/PageHeader.astro';
import { Verse } from '../../components'; import { Pagination, Verse } from '../../components';
export async function getStaticPaths() { export async function getStaticPaths() {
const haikuEntries = await getCollection('haiku'); const haikuEntries = await getCollection('haiku');
return haikuEntries.map((entry) => ({ const numberOfPages = haikuEntries.length;
haikuEntries.sort(sortByDate).reverse();
return haikuEntries.map((entry, index) => ({
params: { slug: entry.slug }, params: { slug: entry.slug },
props: { entry }, props: {
entry,
nextPost:
index + 1 === numberOfPages ? { slug: null, data: null } : haikuEntries[index + 1],
prevPost: index === 0 ? {} : haikuEntries[index - 1],
},
})); }));
} }
const { entry } = Astro.props; const { entry, prevPost, nextPost } = Astro.props;
--- ---
<BaseLayout title={`Haiku ${entry.slug}`} header={false} footer={false}> <BaseLayout title={`Haiku ${entry.slug}`} header={false} footer={false}>
@@ -24,7 +34,7 @@ const { entry } = Astro.props;
backLink="/haiku/" backLink="/haiku/"
/> />
<div <div
class="col-span-full row-start-1 row-end-3 grid w-full grid-cols-haiku haiku:grid-cols-haiku-xl" class="haiku:grid-cols-haiku-xl col-span-full row-start-1 row-end-3 grid w-full grid-cols-haiku"
> >
<div class="grid h-full w-full items-center"> <div class="grid h-full w-full items-center">
<Verse <Verse
@@ -42,4 +52,10 @@ const { entry } = Astro.props;
</div> </div>
</div> </div>
</div> </div>
<Pagination
nextText={`Haiku ${nextPost.slug}`}
nextUrl={nextPost.slug && `/haiku/${nextPost.slug}`}
previousText={`Haiku ${prevPost.slug}`}
previousUrl={prevPost.slug && `/haiku/${prevPost.slug}`}
/>
</BaseLayout> </BaseLayout>

View File

@@ -0,0 +1,3 @@
export const sortByDate = (a: any, b: any) => {
return Date.parse(b.data.date.toISOString()) - Date.parse(a.data.date.toISOString());
};