Refactor URL routing out of PaginationToolbar
in preparation for PaginationToolbar being able to support other kinds of state!
This commit is contained in:
parent
98e89e4302
commit
ca58bc1be3
3 changed files with 45 additions and 20 deletions
|
@ -10,7 +10,9 @@ import SquareItemCard, {
|
||||||
SquareItemCardSkeleton,
|
SquareItemCardSkeleton,
|
||||||
} from "./components/SquareItemCard";
|
} from "./components/SquareItemCard";
|
||||||
import { Delay, MajorErrorMessage, useDebounce } from "./util";
|
import { Delay, MajorErrorMessage, useDebounce } from "./util";
|
||||||
import PaginationToolbar from "./components/PaginationToolbar";
|
import PaginationToolbar, {
|
||||||
|
useRouterPagination,
|
||||||
|
} from "./components/PaginationToolbar";
|
||||||
import { useSearchQueryInUrl } from "./components/ItemSearchPageToolbar";
|
import { useSearchQueryInUrl } from "./components/ItemSearchPageToolbar";
|
||||||
|
|
||||||
function ItemSearchPage() {
|
function ItemSearchPage() {
|
||||||
|
@ -91,6 +93,16 @@ function ItemSearchPage() {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const items = data?.itemSearch?.items || [];
|
||||||
|
const numTotalItems = data?.itemSearch?.numTotalItems || null;
|
||||||
|
|
||||||
|
const {
|
||||||
|
numTotalPages,
|
||||||
|
currentPageNumber,
|
||||||
|
goToPageNumber,
|
||||||
|
buildPageUrl,
|
||||||
|
} = useRouterPagination(numTotalItems, 30);
|
||||||
|
|
||||||
if (searchQueryIsEmpty(query)) {
|
if (searchQueryIsEmpty(query)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -99,7 +111,7 @@ function ItemSearchPage() {
|
||||||
return <MajorErrorMessage error={error} variant="network" />;
|
return <MajorErrorMessage error={error} variant="network" />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data?.itemSearch?.numTotalItems === 0) {
|
if (numTotalItems === 0) {
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
We couldn't find any matching items{" "}
|
We couldn't find any matching items{" "}
|
||||||
|
@ -111,13 +123,13 @@ function ItemSearchPage() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const items = data?.itemSearch?.items || [];
|
|
||||||
const numTotalItems = data?.itemSearch?.numTotalItems || null;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<PaginationToolbar
|
<PaginationToolbar
|
||||||
totalCount={numTotalItems}
|
numTotalPages={numTotalPages}
|
||||||
|
currentPageNumber={currentPageNumber}
|
||||||
|
goToPageNumber={goToPageNumber}
|
||||||
|
buildPageUrl={buildPageUrl}
|
||||||
isLoading={loading}
|
isLoading={loading}
|
||||||
marginBottom="6"
|
marginBottom="6"
|
||||||
/>
|
/>
|
||||||
|
@ -136,7 +148,10 @@ function ItemSearchPage() {
|
||||||
</Delay>
|
</Delay>
|
||||||
)}
|
)}
|
||||||
<PaginationToolbar
|
<PaginationToolbar
|
||||||
totalCount={numTotalItems}
|
numTotalPages={numTotalPages}
|
||||||
|
currentPageNumber={currentPageNumber}
|
||||||
|
goToPageNumber={goToPageNumber}
|
||||||
|
buildPageUrl={buildPageUrl}
|
||||||
isLoading={loading}
|
isLoading={loading}
|
||||||
marginTop="4"
|
marginTop="4"
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -9,7 +9,9 @@ import { useRouter } from "next/router";
|
||||||
import { Heading1, MajorErrorMessage, useCommonStyles } from "./util";
|
import { Heading1, MajorErrorMessage, useCommonStyles } from "./util";
|
||||||
import HangerSpinner from "./components/HangerSpinner";
|
import HangerSpinner from "./components/HangerSpinner";
|
||||||
import OutfitThumbnail from "./components/OutfitThumbnail";
|
import OutfitThumbnail from "./components/OutfitThumbnail";
|
||||||
import PaginationToolbar from "./components/PaginationToolbar";
|
import PaginationToolbar, {
|
||||||
|
useRouterPagination,
|
||||||
|
} from "./components/PaginationToolbar";
|
||||||
import useCurrentUser from "./components/useCurrentUser";
|
import useCurrentUser from "./components/useCurrentUser";
|
||||||
|
|
||||||
function UserOutfitsPage() {
|
function UserOutfitsPage() {
|
||||||
|
@ -93,6 +95,13 @@ function UserOutfitsPageContent() {
|
||||||
|
|
||||||
const isLoading = userLoading || queryLoading;
|
const isLoading = userLoading || queryLoading;
|
||||||
|
|
||||||
|
const {
|
||||||
|
numTotalPages,
|
||||||
|
currentPageNumber,
|
||||||
|
goToPageNumber,
|
||||||
|
buildPageUrl,
|
||||||
|
} = useRouterPagination(numTotalOutfits, PER_PAGE);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return <MajorErrorMessage error={error} variant="network" />;
|
return <MajorErrorMessage error={error} variant="network" />;
|
||||||
}
|
}
|
||||||
|
@ -102,8 +111,11 @@ function UserOutfitsPageContent() {
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<PaginationToolbar
|
<PaginationToolbar
|
||||||
|
numTotalPages={numTotalPages}
|
||||||
|
currentPageNumber={currentPageNumber}
|
||||||
|
goToPageNumber={goToPageNumber}
|
||||||
|
buildPageUrl={buildPageUrl}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
totalCount={numTotalOutfits}
|
|
||||||
numPerPage={PER_PAGE}
|
numPerPage={PER_PAGE}
|
||||||
/>
|
/>
|
||||||
<Box height="6" />
|
<Box height="6" />
|
||||||
|
@ -131,8 +143,11 @@ function UserOutfitsPageContent() {
|
||||||
)}
|
)}
|
||||||
<Box height="6" />
|
<Box height="6" />
|
||||||
<PaginationToolbar
|
<PaginationToolbar
|
||||||
|
numTotalPages={numTotalPages}
|
||||||
|
currentPageNumber={currentPageNumber}
|
||||||
|
goToPageNumber={goToPageNumber}
|
||||||
|
buildPageUrl={buildPageUrl}
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
totalCount={numTotalOutfits}
|
|
||||||
numPerPage={PER_PAGE}
|
numPerPage={PER_PAGE}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
|
@ -5,17 +5,12 @@ import { useRouter } from "next/router";
|
||||||
|
|
||||||
function PaginationToolbar({
|
function PaginationToolbar({
|
||||||
isLoading,
|
isLoading,
|
||||||
totalCount,
|
numTotalPages,
|
||||||
numPerPage = 30,
|
currentPageNumber,
|
||||||
|
goToPageNumber,
|
||||||
|
buildPageUrl,
|
||||||
...props
|
...props
|
||||||
}) {
|
}) {
|
||||||
const {
|
|
||||||
numTotalPages,
|
|
||||||
currentPageNumber,
|
|
||||||
goToPageNumber,
|
|
||||||
buildPageUrl,
|
|
||||||
} = useRouterPagination(totalCount, numPerPage);
|
|
||||||
|
|
||||||
const pagesAreLoaded = currentPageNumber != null && numTotalPages != null;
|
const pagesAreLoaded = currentPageNumber != null && numTotalPages != null;
|
||||||
const hasPrevPage = pagesAreLoaded && currentPageNumber > 1;
|
const hasPrevPage = pagesAreLoaded && currentPageNumber > 1;
|
||||||
const hasNextPage = pagesAreLoaded && currentPageNumber < numTotalPages;
|
const hasNextPage = pagesAreLoaded && currentPageNumber < numTotalPages;
|
||||||
|
@ -63,7 +58,7 @@ function PaginationToolbar({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function useRouterPagination(totalCount, numPerPage) {
|
export function useRouterPagination(totalCount, numPerPage) {
|
||||||
const { query, push: pushHistory } = useRouter();
|
const { query, push: pushHistory } = useRouter();
|
||||||
|
|
||||||
const currentOffset = parseInt(query.offset) || 0;
|
const currentOffset = parseInt(query.offset) || 0;
|
||||||
|
|
Loading…
Reference in a new issue