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

Fix max page logic #55

Merged
merged 2 commits into from
Nov 29, 2020
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
23 changes: 4 additions & 19 deletions components/Pager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../contexts/LoadingContext";
import removeEmpty from "../lib/removeEmpty";
import { UrlObject } from "url";
import { actualMaxPageNumber } from "../lib/pager";

type Url = string | UrlObject;

Expand Down Expand Up @@ -42,33 +43,17 @@ const PagerButton: FC<PagerButtonProps> = ({ href, children }) => {

const Pager = () => {
const options = useSearchGlobalState();
const loading = useLoadingGlobalState();
const [, setMaxPages] = useState(1);
const [hasPrevPrevPage, setHasPrevPrevPage] = useState(false);
const [hasPrevPage, setHasPrevPage] = useState(false);
const [hasNextPage, setHasNextPage] = useState(false);
const [hasNextNextPage, setHasNextNextPage] = useState(false);

const LIMIT = 100;
const calcPageNumber = () => {
const { count } = options;
if (count === 0) {
return 0;
}
return Math.ceil(count / LIMIT);
};

useEffect(() => {
const pageNumber = calcPageNumber();
setMaxPages(pageNumber);
const actualMaxPageNum = actualMaxPageNumber(options.per, options.count);
setHasPrevPrevPage(options.page > 2);
setHasPrevPage(options.page > 1);
setHasNextPage(
pageNumber > options.page && 1600 / options.per >= options.page
);
setHasNextNextPage(
pageNumber > options.page + 1 && 1600 / options.per >= options.page + 1
);
setHasNextPage(actualMaxPageNum > options.page);
setHasNextNextPage(actualMaxPageNum > options.page + 1);
}, [options]);

return (
Expand Down
15 changes: 15 additions & 0 deletions lib/pager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const MAX_OFFSET = 1600;

export const maxPageNumber = (per: number): number => {
return Math.ceil(MAX_OFFSET / per) + 1;
};

export const calcPageNumber = (per: number, count: number) => {
if (count === 0) {
return 0;
}
return Math.ceil(count / per);
};

export const actualMaxPageNumber = (per: number, count: number) =>
Math.min(maxPageNumber(per), calcPageNumber(per, count));
18 changes: 15 additions & 3 deletions pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { format } from "date-fns";
import * as Sentry from "@sentry/node";
import { AxiosError } from "axios";

import { actualMaxPageNumber, MAX_OFFSET } from "../lib/pager";

export default function Search({
videos,
searchOptions,
Expand Down Expand Up @@ -90,9 +92,6 @@ const calcOffset = (page?: number, per: number = 100): number => {
page = 1;
}
let offset = (page - 1) * per;
if (offset > 1600) {
return 1600;
}

return offset;
};
Expand Down Expand Up @@ -288,6 +287,10 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
> => {
if (shouldExecCall(searchOptions)) {
const searchQuery = getSearchQuery(searchOptions);
if (searchQuery._offset > MAX_OFFSET) {
searchQuery._offset = 0;
searchQuery._limit = 0;
}
const response = (
await client.search(searchQuery, usedFields).catch((e: AxiosError) => {
Sentry.captureException(e);
Expand Down Expand Up @@ -316,6 +319,15 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
};
}

const actualMaxPageNum = actualMaxPageNumber(
searchOptions.per,
response.meta.totalCount
);

if (actualMaxPageNum < searchOptions.page) {
searchOptions.page = actualMaxPageNum + 1;
}

return {
props: {
videos: response.data,
Expand Down