Add ?offset parameter to item search page

No links to it yet, but it's respected as a way to request other pages!
This commit is contained in:
Emi Matchu 2021-02-06 21:54:49 -08:00
parent 2d0601cfeb
commit 2115bd64a7

View file

@ -15,7 +15,7 @@ import WIPCallout from "./components/WIPCallout";
import { Delay, ErrorMessage, useCommonStyles, useDebounce } from "./util"; import { Delay, ErrorMessage, useCommonStyles, useDebounce } from "./util";
function ItemSearchPage() { function ItemSearchPage() {
const [query, setQuery] = useSearchQueryInUrl(); const [query, offset, setQuery] = useSearchQueryInUrl();
const { brightBackground } = useCommonStyles(); const { brightBackground } = useCommonStyles();
return ( return (
@ -29,14 +29,14 @@ function ItemSearchPage() {
autoFocus autoFocus
/> />
<Box height="6" /> <Box height="6" />
<ItemSearchPageResults query={query} /> <ItemSearchPageResults query={query} offset={offset} />
</Box> </Box>
); );
} }
/** /**
* useSearchQueryInUrl provides an API like useState, but stores the search * useSearchQueryInUrl provides an API like useState, but stores the search
* query in the URL! * query in the URL! It also parses out the offset for us.
*/ */
function useSearchQueryInUrl() { function useSearchQueryInUrl() {
const history = useHistory(); const history = useHistory();
@ -51,6 +51,9 @@ function useSearchQueryInUrl() {
filterToItemKind: searchParams.get("kind") || null, filterToItemKind: searchParams.get("kind") || null,
filterToCurrentUserOwnsOrWants: searchParams.get("user") || null, filterToCurrentUserOwnsOrWants: searchParams.get("user") || null,
}; };
const offset = parseInt(searchParams.get("offset")) || 0;
const setQuery = React.useCallback( const setQuery = React.useCallback(
(newQuery) => { (newQuery) => {
let url = `/items/search`; let url = `/items/search`;
@ -69,6 +72,10 @@ function useSearchQueryInUrl() {
if (newQuery.filterToCurrentUserOwnsOrWants) { if (newQuery.filterToCurrentUserOwnsOrWants) {
newParams.append("user", newQuery.filterToCurrentUserOwnsOrWants); newParams.append("user", newQuery.filterToCurrentUserOwnsOrWants);
} }
// NOTE: We omit `offset`, because changing the query should reset us
// back to the first page!
const search = newParams.toString(); const search = newParams.toString();
if (search) { if (search) {
url += "?" + search; url += "?" + search;
@ -79,10 +86,13 @@ function useSearchQueryInUrl() {
[history] [history]
); );
return [query, setQuery]; // NOTE: We don't provide a `setOffset`, because that's handled via
// pagination links.
return [query, offset, setQuery];
} }
function ItemSearchPageResults({ query: latestQuery }) { function ItemSearchPageResults({ query: latestQuery, offset }) {
// NOTE: Some of this is copied from SearchPanel... but all of this is messy // NOTE: Some of this is copied from SearchPanel... but all of this is messy
// enough that I'm not comfy code-sharing yet, esp since I feel like // enough that I'm not comfy code-sharing yet, esp since I feel like
// SearchPanel pagination is a bit of a mess and will need refactoring. // SearchPanel pagination is a bit of a mess and will need refactoring.
@ -120,13 +130,14 @@ function ItemSearchPageResults({ query: latestQuery }) {
$itemKind: ItemKindSearchFilter $itemKind: ItemKindSearchFilter
$currentUserOwnsOrWants: OwnsOrWants $currentUserOwnsOrWants: OwnsOrWants
$zoneIds: [ID!]! $zoneIds: [ID!]!
$offset: Int!
) { ) {
itemSearch( itemSearch(
query: $query query: $query
itemKind: $itemKind itemKind: $itemKind
currentUserOwnsOrWants: $currentUserOwnsOrWants currentUserOwnsOrWants: $currentUserOwnsOrWants
zoneIds: $zoneIds zoneIds: $zoneIds
offset: 0 offset: $offset
limit: 30 limit: 30
) { ) {
numTotalItems numTotalItems
@ -144,6 +155,7 @@ function ItemSearchPageResults({ query: latestQuery }) {
itemKind: query.filterToItemKind, itemKind: query.filterToItemKind,
currentUserOwnsOrWants: query.filterToCurrentUserOwnsOrWants, currentUserOwnsOrWants: query.filterToCurrentUserOwnsOrWants,
zoneIds: filterToZoneIds, zoneIds: filterToZoneIds,
offset,
}, },
context: { sendAuth: true }, context: { sendAuth: true },
skip: searchQueryIsEmpty(query), skip: searchQueryIsEmpty(query),