Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#8 pagination #12

Merged
merged 2 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
import Head from 'next/head';
import { useState } from 'react';
import { Article, getAllArticles } from '../lib/article';

const PAGE_SIZE = 3

export default function Index({ siteName, posts }: IndexProps) {
const [pageCurrent, setPageCurrent] = useState(0)
const pageLast = posts.length < PAGE_SIZE ? 0 : Math.ceil(posts.length / PAGE_SIZE) - 1
return (
<>
<Head>
<title>{siteName}</title>
</Head>
{posts.map((post) => <ArticlePreview article={post} />)}
{posts
.slice(pageCurrent * PAGE_SIZE, pageCurrent * PAGE_SIZE + PAGE_SIZE)
.map((post) => <ArticlePreview article={post} />)}
<nav className="main-nav">
{pageCurrent > 0 && (
<button
className="prev"
type="button"
onClick={() => setPageCurrent(pageCurrent - 1)}
>
prev_page
</button>
)}
{pageCurrent !== pageLast && (
<button
className="next"
type="button"
onClick={() => setPageCurrent(pageCurrent + 1)}
>
next_page
</button>
) }
</nav>
</>
)
}
Expand Down
4 changes: 2 additions & 2 deletions public/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ img {
margin-top: 5rem;
}

.main-nav a {
.main-nav button {
font-size: 1.8rem;
line-height: 5.5rem;
color: var(--white);
Expand Down Expand Up @@ -434,7 +434,7 @@ img {
}
}

/* Post content
/* Article content
-------------------------------------------------- */
.post-content {
font-size: 2.2rem;
Expand Down