Skip to content

Commit

Permalink
無限スクロールページネーション
Browse files Browse the repository at this point in the history
  • Loading branch information
kn1515 committed Oct 26, 2024
1 parent 3fc0230 commit d4200dc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
26 changes: 8 additions & 18 deletions src/components/pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
// components/Pagination.tsx

type PaginationProps = {
totalPosts: number;
postsPerPage: number;
currentPage: number;
paginate: (pageNumber: number) => void;
loadMore: () => void;
};

const Pagination: React.FC<PaginationProps> = ({
totalPosts,
postsPerPage,
currentPage,
paginate,
loadMore,
}) => {
const pageNumbers = Array.from(
{ length: Math.ceil(totalPosts / postsPerPage) },
(_, i) => i + 1
);
const totalPages = Math.ceil(totalPosts / postsPerPage);

return (
<div className="flex justify-center mt-8">
{pageNumbers.map((number) => (
{currentPage < totalPages && (
<button
key={number}
type="button"
onClick={() => paginate(number)}
className={`mx-1 px-3 py-1 rounded ${
currentPage === number
? "bg-blue-500 text-white"
: "bg-gray-200 text-gray-700"
}`}
onClick={loadMore}
className="mx-1 px-3 py-1 rounded bg-blue-500 text-white"
>
{number}
Load More
</button>
))}
)}
</div>
);
};
Expand Down
6 changes: 3 additions & 3 deletions src/components/post-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type PostListProps = {
totalPosts: number;
postsPerPage: number;
currentPage: number;
paginate: (pageNumber: number) => void;
loadMore: () => void;
};

const PostList: React.FC<PostListProps> = ({
posts,
totalPosts,
postsPerPage,
currentPage,
paginate,
loadMore,
}) => {
return (
<>
Expand All @@ -37,7 +37,7 @@ const PostList: React.FC<PostListProps> = ({
totalPosts={totalPosts}
postsPerPage={postsPerPage}
currentPage={currentPage}
paginate={paginate}
loadMore={loadMore}
/>
</>
);
Expand Down
8 changes: 4 additions & 4 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ const Home: NextPage<Props> = ({ allPosts }) => {

// Calculate the current posts to display
const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const indexOfFirstPost = 0; // Always start from the first post
const currentPosts = allPosts.slice(indexOfFirstPost, indexOfLastPost);

// Change page
const paginate = (pageNumber: number) => setCurrentPage(pageNumber);
// Load more posts
const loadMore = () => setCurrentPage((prevPage) => prevPage + 1);

// Extract unique categories
const categories = Array.from(new Set(allPosts.flatMap((post) => post.tags)));
Expand All @@ -71,7 +71,7 @@ const Home: NextPage<Props> = ({ allPosts }) => {
totalPosts={allPosts.length}
postsPerPage={postsPerPage}
currentPage={currentPage}
paginate={paginate}
loadMore={loadMore}
/>
</div>
<Sidebar categories={categories} allPosts={allPosts} />
Expand Down

0 comments on commit d4200dc

Please sign in to comment.