Skip to content

Commit

Permalink
feat: (#277) category pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewilson committed Jun 17, 2021
1 parent 0aa4bd2 commit a3b8850
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GetStaticPropsContext } from 'next';
import Page from 'pages/category/[categorySlug]';
import { getNextStaticProps } from '@wpengine/headless-next';

export default Page;

export async function getStaticProps(context: GetStaticPropsContext) {
return getNextStaticProps(context, {
Page,
});
}

export function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
};
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { client, getNextStaticProps } from '@wpengine/headless-next';
import Head from 'next/head';
import { Header, Footer, Posts } from 'components';
import { Header, Footer, Posts, Pagination } from 'components';
import { GetStaticPropsContext } from 'next';
import { useRouter } from 'next/router';

const POSTS_PER_PAGE = 6;

export default function Page() {
const { useGeneralSettings, usePosts } = client();
const { query = {} } = useRouter();
const { categorySlug, paginationTerm, categoryCursor } = query;
const generalSettings = useGeneralSettings();
const posts = usePosts();
const isBefore = paginationTerm === 'before';
const posts = usePosts({
after: !isBefore ? (categoryCursor as string) : undefined,
before: isBefore ? (categoryCursor as string) : undefined,
first: !isBefore ? POSTS_PER_PAGE : undefined,
last: isBefore ? POSTS_PER_PAGE : undefined,
where: {
categoryName: categorySlug as string,
},
});

return (
<>
Expand All @@ -21,7 +35,11 @@ export default function Page() {

<main className="content content-index">
<Posts posts={posts.nodes} />
{/* {posts?.pageInfo && <Pagination pageInfo={posts.pageInfo} />} */}

<Pagination
pageInfo={posts.pageInfo}
basePath={`/category/${categorySlug}`}
/>
</main>

<Footer copyrightHolder={generalSettings.title} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default function Page() {
{post && (
<div dangerouslySetInnerHTML={{ __html: post.content() ?? '' }} />
)}
<div>{ post.author.node.nicename }</div>
</div>
</main>

Expand Down
10 changes: 5 additions & 5 deletions examples/next/getting-started/src/pages/posts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { useRouter } from 'next/router';
import React from 'react';
import styles from 'scss/pages/home.module.scss';

const POSTS_PER_PAGE = 6;

export default function Page() {
const {
query = {},
} = useRouter();
const { query = {} } = useRouter();
const { postSlug, postCursor } = query;
const { usePosts, useGeneralSettings } = client();
const generalSettings = useGeneralSettings();
const isBefore = postSlug === 'before';
const posts = usePosts({
after: !isBefore ? (postCursor as string) : undefined,
before: isBefore ? (postCursor as string) : undefined,
first: !isBefore ? 6 : undefined,
last: isBefore ? 6 : undefined,
first: !isBefore ? POSTS_PER_PAGE : undefined,
last: isBefore ? POSTS_PER_PAGE : undefined,
});

return (
Expand Down

0 comments on commit a3b8850

Please sign in to comment.