2021-01-18 15:56:24 -08:00
|
|
|
import React from "react";
|
2021-02-10 14:23:25 -08:00
|
|
|
import { Box, Button, Flex, Wrap, WrapItem } from "@chakra-ui/react";
|
2021-01-18 15:56:24 -08:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery } from "@apollo/client";
|
2021-02-10 14:23:25 -08:00
|
|
|
import { Link, useHistory, useLocation, useParams } from "react-router-dom";
|
2021-01-18 15:56:24 -08:00
|
|
|
|
|
|
|
import SearchToolbar, {
|
|
|
|
emptySearchQuery,
|
|
|
|
searchQueryIsEmpty,
|
|
|
|
} from "./WardrobePage/SearchToolbar";
|
|
|
|
import SquareItemCard, {
|
|
|
|
SquareItemCardSkeleton,
|
|
|
|
} from "./components/SquareItemCard";
|
|
|
|
import { Delay, ErrorMessage, useCommonStyles, useDebounce } from "./util";
|
|
|
|
|
|
|
|
function ItemSearchPage() {
|
2021-02-06 21:54:49 -08:00
|
|
|
const [query, offset, setQuery] = useSearchQueryInUrl();
|
2021-01-18 15:56:24 -08:00
|
|
|
const { brightBackground } = useCommonStyles();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
|
|
|
<SearchToolbar
|
|
|
|
query={query}
|
|
|
|
onChange={setQuery}
|
|
|
|
showItemsLabel
|
|
|
|
background={brightBackground}
|
|
|
|
boxShadow="md"
|
2021-01-21 16:38:08 -08:00
|
|
|
autoFocus
|
2021-01-18 15:56:24 -08:00
|
|
|
/>
|
|
|
|
<Box height="6" />
|
2021-02-06 21:54:49 -08:00
|
|
|
<ItemSearchPageResults query={query} offset={offset} />
|
2021-01-18 15:56:24 -08:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useSearchQueryInUrl provides an API like useState, but stores the search
|
2021-02-06 21:54:49 -08:00
|
|
|
* query in the URL! It also parses out the offset for us.
|
2021-01-18 15:56:24 -08:00
|
|
|
*/
|
|
|
|
function useSearchQueryInUrl() {
|
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
const { query: value } = useParams();
|
|
|
|
const { search } = useLocation();
|
|
|
|
const searchParams = new URLSearchParams(search);
|
|
|
|
|
|
|
|
const query = {
|
2021-03-12 07:44:59 -08:00
|
|
|
value: decodeURIComponent(value || ""),
|
2021-01-18 15:56:24 -08:00
|
|
|
filterToZoneLabel: searchParams.get("zone") || null,
|
|
|
|
filterToItemKind: searchParams.get("kind") || null,
|
2021-01-21 15:58:24 -08:00
|
|
|
filterToCurrentUserOwnsOrWants: searchParams.get("user") || null,
|
2021-01-18 15:56:24 -08:00
|
|
|
};
|
2021-02-06 21:54:49 -08:00
|
|
|
|
|
|
|
const offset = parseInt(searchParams.get("offset")) || 0;
|
|
|
|
|
2021-01-18 15:56:24 -08:00
|
|
|
const setQuery = React.useCallback(
|
|
|
|
(newQuery) => {
|
|
|
|
let url = `/items/search`;
|
|
|
|
|
|
|
|
if (newQuery.value) {
|
|
|
|
url += "/" + encodeURIComponent(newQuery.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newParams = new URLSearchParams();
|
|
|
|
if (newQuery.filterToItemKind) {
|
|
|
|
newParams.append("kind", newQuery.filterToItemKind);
|
|
|
|
}
|
|
|
|
if (newQuery.filterToZoneLabel) {
|
|
|
|
newParams.append("zone", newQuery.filterToZoneLabel);
|
|
|
|
}
|
2021-01-21 15:58:24 -08:00
|
|
|
if (newQuery.filterToCurrentUserOwnsOrWants) {
|
|
|
|
newParams.append("user", newQuery.filterToCurrentUserOwnsOrWants);
|
|
|
|
}
|
2021-02-06 21:54:49 -08:00
|
|
|
|
|
|
|
// NOTE: We omit `offset`, because changing the query should reset us
|
|
|
|
// back to the first page!
|
|
|
|
|
2021-01-18 15:56:24 -08:00
|
|
|
const search = newParams.toString();
|
|
|
|
if (search) {
|
|
|
|
url += "?" + search;
|
|
|
|
}
|
|
|
|
|
|
|
|
history.replace(url);
|
|
|
|
},
|
|
|
|
[history]
|
|
|
|
);
|
|
|
|
|
2021-02-06 21:54:49 -08:00
|
|
|
// NOTE: We don't provide a `setOffset`, because that's handled via
|
|
|
|
// pagination links.
|
|
|
|
|
|
|
|
return [query, offset, setQuery];
|
2021-01-18 15:56:24 -08:00
|
|
|
}
|
|
|
|
|
2021-02-06 21:54:49 -08:00
|
|
|
function ItemSearchPageResults({ query: latestQuery, offset }) {
|
2021-01-18 15:56:24 -08:00
|
|
|
// 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
|
|
|
|
// SearchPanel pagination is a bit of a mess and will need refactoring.
|
|
|
|
|
|
|
|
// We debounce the search query, so that we don't resend a new query whenever
|
|
|
|
// the user types anything.
|
|
|
|
const query = useDebounce(latestQuery, 300, {
|
|
|
|
waitForFirstPause: true,
|
|
|
|
initialValue: emptySearchQuery,
|
|
|
|
|
2021-01-21 16:03:14 -08:00
|
|
|
// When the query is empty, clear the debounced query immediately too! That
|
|
|
|
// will give us fast feedback when the search field clears.
|
|
|
|
forceReset: searchQueryIsEmpty(latestQuery),
|
|
|
|
});
|
2021-01-18 15:56:24 -08:00
|
|
|
|
|
|
|
// NOTE: This query should always load ~instantly, from the client cache.
|
|
|
|
const { data: zoneData } = useQuery(gql`
|
|
|
|
query SearchPanelZones {
|
|
|
|
allZones {
|
|
|
|
id
|
|
|
|
label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
const allZones = zoneData?.allZones || [];
|
|
|
|
const filterToZones = query.filterToZoneLabel
|
|
|
|
? allZones.filter((z) => z.label === query.filterToZoneLabel)
|
|
|
|
: [];
|
|
|
|
const filterToZoneIds = filterToZones.map((z) => z.id);
|
|
|
|
|
|
|
|
const { loading, error, data } = useQuery(
|
|
|
|
gql`
|
|
|
|
query ItemSearchPageResults(
|
|
|
|
$query: String!
|
|
|
|
$itemKind: ItemKindSearchFilter
|
2021-01-21 15:58:24 -08:00
|
|
|
$currentUserOwnsOrWants: OwnsOrWants
|
2021-01-18 15:56:24 -08:00
|
|
|
$zoneIds: [ID!]!
|
2021-02-06 21:54:49 -08:00
|
|
|
$offset: Int!
|
2021-01-18 15:56:24 -08:00
|
|
|
) {
|
|
|
|
itemSearch(
|
|
|
|
query: $query
|
|
|
|
itemKind: $itemKind
|
2021-01-21 15:58:24 -08:00
|
|
|
currentUserOwnsOrWants: $currentUserOwnsOrWants
|
2021-01-18 15:56:24 -08:00
|
|
|
zoneIds: $zoneIds
|
2021-02-06 21:54:49 -08:00
|
|
|
offset: $offset
|
2021-01-18 15:56:24 -08:00
|
|
|
limit: 30
|
|
|
|
) {
|
2021-02-06 21:50:52 -08:00
|
|
|
numTotalItems
|
2021-01-18 15:56:24 -08:00
|
|
|
items {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
variables: {
|
|
|
|
query: query.value,
|
|
|
|
itemKind: query.filterToItemKind,
|
2021-01-21 15:58:24 -08:00
|
|
|
currentUserOwnsOrWants: query.filterToCurrentUserOwnsOrWants,
|
2021-01-18 15:56:24 -08:00
|
|
|
zoneIds: filterToZoneIds,
|
2021-02-06 21:54:49 -08:00
|
|
|
offset,
|
2021-01-18 15:56:24 -08:00
|
|
|
},
|
2021-01-21 15:58:24 -08:00
|
|
|
context: { sendAuth: true },
|
2021-01-21 16:03:14 -08:00
|
|
|
skip: searchQueryIsEmpty(query),
|
2021-01-18 15:56:24 -08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-01-21 16:03:14 -08:00
|
|
|
if (searchQueryIsEmpty(query)) {
|
2021-01-18 15:56:24 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return (
|
2021-02-10 14:23:25 -08:00
|
|
|
<Box>
|
|
|
|
<ItemSearchPagePagination isLoading marginBottom="6" />
|
|
|
|
<Delay>
|
|
|
|
<ItemSearchPageResultsLoading />
|
|
|
|
</Delay>
|
|
|
|
<ItemSearchPagePagination isLoading marginTop="4" />
|
|
|
|
</Box>
|
2021-01-18 15:56:24 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<ErrorMessage>
|
|
|
|
Oops, we couldn't load the search results. Check your connection and try
|
|
|
|
again!
|
|
|
|
</ErrorMessage>
|
|
|
|
);
|
2021-01-21 16:39:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data.itemSearch.items.length === 0) {
|
|
|
|
return (
|
|
|
|
<Box>
|
|
|
|
We couldn't find any matching items{" "}
|
|
|
|
<span role="img" aria-label="(thinking emoji)">
|
|
|
|
🤔
|
|
|
|
</span>{" "}
|
|
|
|
Try again?
|
|
|
|
</Box>
|
|
|
|
);
|
2021-01-18 15:56:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
2021-02-10 14:23:25 -08:00
|
|
|
<ItemSearchPagePagination
|
|
|
|
numTotalItems={data.itemSearch.numTotalItems}
|
|
|
|
marginBottom="6"
|
|
|
|
/>
|
2021-01-18 15:56:24 -08:00
|
|
|
<Wrap justify="center" spacing="4">
|
|
|
|
{data.itemSearch.items.map((item) => (
|
|
|
|
<WrapItem key={item.id}>
|
|
|
|
<SquareItemCard item={item} />
|
|
|
|
</WrapItem>
|
|
|
|
))}
|
|
|
|
</Wrap>
|
2021-02-10 14:23:25 -08:00
|
|
|
<ItemSearchPagePagination
|
|
|
|
numTotalItems={data.itemSearch.numTotalItems}
|
|
|
|
marginTop="4"
|
|
|
|
/>
|
2021-01-18 15:56:24 -08:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-10 14:23:25 -08:00
|
|
|
function ItemSearchPagePagination({ isLoading, numTotalItems, ...props }) {
|
|
|
|
const { search } = useLocation();
|
|
|
|
|
|
|
|
const currentOffset =
|
|
|
|
parseInt(new URLSearchParams(search).get("offset")) || 0;
|
|
|
|
|
|
|
|
const prevPageSearchParams = new URLSearchParams(search);
|
|
|
|
const prevPageOffset = currentOffset - 30;
|
|
|
|
prevPageSearchParams.set("offset", prevPageOffset);
|
|
|
|
const prevPageUrl = "?" + prevPageSearchParams.toString();
|
|
|
|
const prevPageIsDisabled = isLoading || prevPageOffset < 0;
|
|
|
|
|
|
|
|
const nextPageSearchParams = new URLSearchParams(search);
|
|
|
|
const nextPageOffset = currentOffset + 30;
|
|
|
|
nextPageSearchParams.set("offset", nextPageOffset);
|
|
|
|
const nextPageUrl = "?" + nextPageSearchParams.toString();
|
|
|
|
const nextPageIsDisabled = isLoading || nextPageOffset >= numTotalItems;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Flex justify="space-between" {...props}>
|
|
|
|
<Button
|
|
|
|
as={prevPageIsDisabled ? "button" : Link}
|
|
|
|
to={prevPageIsDisabled ? undefined : prevPageUrl}
|
|
|
|
_disabled={{ cursor: isLoading ? "wait" : "not-allowed", opacity: 0.4 }}
|
|
|
|
isDisabled={prevPageIsDisabled}
|
|
|
|
>
|
|
|
|
← Prev
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
as={nextPageIsDisabled ? "button" : Link}
|
|
|
|
to={nextPageIsDisabled ? undefined : nextPageUrl}
|
|
|
|
_disabled={{ cursor: isLoading ? "wait" : "not-allowed", opacity: 0.4 }}
|
|
|
|
isDisabled={nextPageIsDisabled}
|
|
|
|
>
|
|
|
|
Next →
|
|
|
|
</Button>
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:56:24 -08:00
|
|
|
function ItemSearchPageResultsLoading() {
|
|
|
|
return (
|
|
|
|
<Wrap justify="center" spacing="4">
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton minHeightNumLines={3} />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
<WrapItem>
|
|
|
|
<SquareItemCardSkeleton />
|
|
|
|
</WrapItem>
|
|
|
|
</Wrap>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ItemSearchPage;
|