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

Add pagination to display only 10 search results at a time #51

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion pages/article/[article_slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ShareButton from "../../components/ShareButton";
import Link from "next/link";
import generate_contributors_jsx from "../../components/GenerateContributorsJSX";
import { generateMetaTags } from "../../utils/generateMetaTags";
import Advertisement from "../../components/Advertisement";

interface Props {
article: ReceivedArticle;
Expand Down Expand Up @@ -157,7 +158,8 @@ function Article(props: Props) {
id={styles.content}
dangerouslySetInnerHTML={{ __html: text }}
></div>

<Advertisement
index={0}/>
{/* <RecommendedArticles /> */}
</article>
<div id={styles.advertisements}></div>
Expand Down
68 changes: 55 additions & 13 deletions pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,40 @@ enum sortingOptions {
}

function SearchRoute(props: Props) {
const [sortingOption, setSortingOption] = useState<sortingOptions>(sortingOptions.Relevance);
const [sortedArticles, setSortedArticles] = useState<ReceivedArticle[]>(props.articles);
const [sortingOption, setSortingOption] = useState<sortingOptions>(
sortingOptions.Relevance
);
const [sortedArticles, setSortedArticles] = useState<ReceivedArticle[]>(
props.articles
);
const [currentPage, setCurrentPage] = useState<number>(1);
const articlesPerPage = 10;

useEffect(() => {
sortArticles();
}, [sortingOption]);
}, [sortingOption, props.articles]);

useEffect(() => {
paginateArticles();
}, [currentPage, sortedArticles]);

const handleSortChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setSortingOption(event.target.value as sortingOptions);
setCurrentPage(1); // Reset to the first page when sorting changes
};

const sortArticles = () => {
const articlesCopy = [...props.articles];
let articlesCopy = [...props.articles];

if (sortingOption === sortingOptions.Newest) {
articlesCopy.sort((a, b) => {
articlesCopy = articlesCopy.sort((a, b) => {
if (a.volume === b.volume) {
return b.issue - a.issue;
}
return b.volume - a.volume;
});
} else if (sortingOption === sortingOptions.Oldest) {
articlesCopy.sort((a, b) => {
articlesCopy = articlesCopy.sort((a, b) => {
if (a.volume === b.volume) {
return a.issue - b.issue;
}
Expand All @@ -53,18 +64,28 @@ function SearchRoute(props: Props) {
setSortedArticles(articlesCopy);
};

const paginateArticles = () => {
const startIndex = (currentPage - 1) * articlesPerPage;
const endIndex = startIndex + articlesPerPage;
const currentArticles = props.articles.slice(startIndex, endIndex);
setSortedArticles(currentArticles);
};

return (
<div>
<main id={styles.main}>
<h1 id={styles.heading}>
Showing {props.articles.length + props.staff.length} results for:{" "}
{"'"}
Showing {props.articles.length + props.staff.length} results for:{"'"}
{props.query}
{"'"}
</h1>
<div>
<label htmlFor="sort-select">Sort by:</label>
<select id="sort-select" value={sortingOption} onChange={handleSortChange}>
<select
id="sort-select"
value={sortingOption}
onChange={handleSortChange}
>
<option value={sortingOptions.Relevance}>Relevance</option>
<option value={sortingOptions.Newest}>Newest</option>
<option value={sortingOptions.Oldest}>Oldest</option>
Expand All @@ -77,12 +98,17 @@ function SearchRoute(props: Props) {
{props.staff.map((staff_member, index) => (
<div key={index}>
<h1 id={styles.name}>
<Link href={"/staff/" + encodeURIComponent(staff_member.slug)}>
<Link
href={"/staff/" + encodeURIComponent(staff_member.slug)}
>
{staff_member.name}{" "}
<span id={styles.slug}>({staff_member.slug})</span>
</Link>
</h1>
<a href={"mailto:" + staff_member.email} id={styles.email}>
<a
href={"mailto:" + staff_member.email}
id={styles.email}
>
{staff_member.email}
</a>
</div>
Expand All @@ -91,13 +117,29 @@ function SearchRoute(props: Props) {
</>
)}
<h2 id={styles.articles_found_label}>
{sortedArticles.length} articles found:
{props.articles.length} articles found:
</h2>
<ListArticleDisplay articles={sortedArticles} />
{/* Pagination */}
<div>
<button
onClick={() => setCurrentPage(currentPage - 1)}
disabled={currentPage === 1}
>
Previous
</button>
<span>{currentPage}</span>
<button
onClick={() => setCurrentPage(currentPage + 1)}
disabled={currentPage * articlesPerPage >= props.articles.length}
>
Next
</button>
</div>
</main>
</div>
);
};
}

export default SearchRoute;

Expand Down
23 changes: 22 additions & 1 deletion styles/Navbar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
#subscribe_parent {
margin-left: auto;
}

@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}

#logo_container {
font-size: var(--large-text);
color: var(--primary-immutable);
Expand All @@ -43,7 +49,7 @@
position: absolute;
top: 32px; /* position the top edge of the element at the middle of the parent */
left: 50%; /* position the left edge of the element at the middle of the parent */

animation: fade-in 2s ease;
transform: translate(-50%, -50%);
cursor: pointer;
font-family: "Old English Text MT";
Expand All @@ -55,10 +61,25 @@
place-self: center;
}

@keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}

#hamburgerMenu {
/* display: none; */
background: none;
cursor: pointer;
animation: rotate 2s linear 1s infinite;
animation-play-state: paused;
}

#hamburgerMenu:hover {
animation-play-state: running;
}

#hamburgerMenu:hover,
Expand Down