-
Notifications
You must be signed in to change notification settings - Fork 0
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
14 changed files
with
358 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"presets": [ | ||
"next/babel" | ||
], | ||
"plugins": [ | ||
"inline-react-svg" | ||
] | ||
} |
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,92 @@ | ||
import Link from "next/link"; | ||
import { useMemo } from "react"; | ||
import DoubleChev from "../icons/double-chev.svg"; | ||
import SingleChev from "../icons/single-chev.svg"; | ||
|
||
const Pagination = ({ isLast, currentPage, numberOfPages }) => { | ||
const { isFirst, prevRoute, nextRoute } = useMemo( | ||
() => ({ | ||
isFirst: currentPage === 1, | ||
prevRoute: `/blogs/page/${currentPage - 1}`, | ||
nextRoute: `/blogs/page/${currentPage + 1}`, | ||
}), | ||
[currentPage] | ||
); | ||
|
||
return ( | ||
numberOfPages > 1 && ( | ||
<div className="flex items-center justify-between max-w-lg mx-auto mt-2 px-2"> | ||
{/* 1st and Previous btn */} | ||
<div className="flex items-center space-x-2"> | ||
<Link href="/blogs/page/1"> | ||
<button | ||
disabled={isFirst} | ||
className="pagination-btn group"> | ||
<DoubleChev | ||
fill={isFirst ? "white" : "#10b981"} | ||
className="group-hover:fill-current group-hover:text-white" | ||
/> | ||
</button> | ||
</Link> | ||
<Link href={prevRoute}> | ||
<button | ||
disabled={isFirst} | ||
className="pagination-btn group"> | ||
<SingleChev | ||
fill={isFirst ? "white" : "#10b981"} | ||
className="group-hover:fill-current group-hover:text-white" | ||
/> | ||
</button> | ||
</Link> | ||
</div> | ||
|
||
<div className="flex items-center space-x-2"> | ||
{Array.from({ length: numberOfPages }, (_, i) => ( | ||
<Link | ||
href={ | ||
currentPage === i + 1 | ||
? `#` | ||
: `/blogs/page/${i + 1}` | ||
} | ||
key={i}> | ||
<button | ||
disabled={currentPage === i + 1} | ||
className={`pagination-btn group ${ | ||
currentPage === i + 1 && | ||
"pagination-btn-active !opacity-100" | ||
}`}> | ||
{i + 1} | ||
</button> | ||
</Link> | ||
))} | ||
</div> | ||
|
||
{/* Last and Next btn */} | ||
<div className="flex items-center space-x-2"> | ||
<Link href={nextRoute}> | ||
<button | ||
disabled={isLast} | ||
className="pagination-btn group rotate-180"> | ||
<SingleChev | ||
fill={isLast ? "white" : "#10b981"} | ||
className="group-hover:fill-current group-hover:text-white" | ||
/> | ||
</button> | ||
</Link> | ||
<Link href={`/blogs/page/${numberOfPages}`}> | ||
<button | ||
disabled={isLast} | ||
className="pagination-btn group rotate-180"> | ||
<DoubleChev | ||
fill={isLast ? "white" : "#10b981"} | ||
className="group-hover:fill-current group-hover:text-white" | ||
/> | ||
</button> | ||
</Link> | ||
</div> | ||
</div> | ||
) | ||
); | ||
}; | ||
|
||
export default Pagination; |
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 @@ | ||
export const BLOGS_PER_PAGE = 6; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,17 @@ | ||
import fs from "fs"; | ||
import { read } from "gray-matter"; | ||
import path from "path"; | ||
import { sortbyDate } from "utils/"; | ||
|
||
const getPosts = () => { | ||
const files = fs.readdirSync(path.join("posts")); | ||
const posts = files.map(fileName => { | ||
const slug = fileName.replace(".md", ""); | ||
const { data: frontmatter } = read(path.join("posts", fileName)); | ||
return { slug, ...frontmatter }; | ||
}); | ||
|
||
return posts.sort(sortbyDate); | ||
}; | ||
|
||
export default getPosts; |
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 |
---|---|---|
@@ -1,34 +1,6 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import Layout from "components/Layout"; | ||
import matter from "gray-matter"; | ||
import Post from "components/Post"; | ||
import { sortbyDate } from "utils"; | ||
|
||
const BlogsPage = ({ posts }) => { | ||
return ( | ||
<Layout title="All Blogs"> | ||
<h1 className="text-4xl border-b-4 p-5 mb-3">Blogs</h1> | ||
<div className="grid md:grid-cols-2 lg:grid-cols-3 gap-5"> | ||
{posts.map(({ slug, ...post }) => ( | ||
<Post key={slug} post={{ slug, ...post }} /> | ||
))} | ||
</div> | ||
</Layout> | ||
); | ||
}; | ||
import BlogsPage from "./page/[pageNumber]"; | ||
import { getStaticProps } from "./page/[pageNumber]"; | ||
|
||
export default BlogsPage; | ||
|
||
export async function getStaticProps(context) { | ||
const files = fs.readdirSync(path.join("posts")); | ||
|
||
const posts = files.map(fileName => { | ||
const slug = fileName.replace(".md", ""); | ||
const { data: frontmatter } = matter.read(path.join("posts", fileName)); | ||
return { slug, ...frontmatter }; | ||
}); | ||
return { | ||
props: { posts: posts.sort(sortbyDate) }, | ||
}; | ||
} | ||
export { getStaticProps }; |
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,61 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import Layout from "components/Layout"; | ||
import Post from "components/Post"; | ||
import { BLOGS_PER_PAGE } from "config"; | ||
import getPosts from "lib/getPosts"; | ||
import Pagination from "components/Pagination"; | ||
|
||
const BlogsPage = ({ posts, numberOfPages, hasMorePages, currentPage }) => { | ||
return ( | ||
<Layout title={`Blogs - Page ${currentPage}`}> | ||
<h1 className="text-4xl border-b-4 p-5 mb-3">Blogs</h1> | ||
<div className="grid md:grid-cols-2 lg:grid-cols-3 gap-5"> | ||
{posts.map(({ slug, ...post }) => ( | ||
<Post key={slug} post={{ slug, ...post }} /> | ||
))} | ||
</div> | ||
<Pagination | ||
currentPage={currentPage} | ||
isLast={!hasMorePages} | ||
numberOfPages={numberOfPages} | ||
/> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default BlogsPage; | ||
|
||
export async function getStaticPaths() { | ||
const files = fs.readdirSync(path.join("posts")); | ||
const numberOfPages = Math.ceil(files.length / BLOGS_PER_PAGE); | ||
|
||
const paths = []; | ||
for (let i = 0; i < numberOfPages; i++) { | ||
paths.push({ params: { pageNumber: (i + 1).toString() } }); | ||
} | ||
return { | ||
paths, | ||
fallback: false, | ||
}; | ||
} | ||
|
||
export async function getStaticProps({ params }) { | ||
const pageNumber = parseInt(params?.pageNumber ?? 1); | ||
const files = fs.readdirSync(path.join("posts")); | ||
const numberOfPages = Math.ceil(files.length / BLOGS_PER_PAGE); | ||
|
||
const posts = getPosts(); | ||
|
||
return { | ||
props: { | ||
posts: posts.slice( | ||
(pageNumber - 1) * BLOGS_PER_PAGE, | ||
pageNumber * BLOGS_PER_PAGE | ||
), | ||
numberOfPages, | ||
currentPage: pageNumber, | ||
hasMorePages: numberOfPages > pageNumber, | ||
}, | ||
}; | ||
} |
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
Oops, something went wrong.