2021-05-21 00:50:55 -07:00
|
|
|
import { Box, 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-09-30 20:04:50 -07:00
|
|
|
import {
|
2021-01-18 15:56:24 -08:00
|
|
|
emptySearchQuery,
|
|
|
|
searchQueryIsEmpty,
|
|
|
|
} from "./WardrobePage/SearchToolbar";
|
|
|
|
import SquareItemCard, {
|
|
|
|
SquareItemCardSkeleton,
|
|
|
|
} from "./components/SquareItemCard";
|
2021-09-30 20:04:50 -07:00
|
|
|
import { Delay, MajorErrorMessage, useDebounce } from "./util";
|
2021-05-21 00:50:55 -07:00
|
|
|
import PaginationToolbar from "./components/PaginationToolbar";
|
2021-09-30 20:04:50 -07:00
|
|
|
import { useSearchQueryInUrl } from "./components/ItemSearchPageToolbar";
|
2021-01-18 15:56:24 -08:00
|
|
|
|
|
|
|
function ItemSearchPage() {
|
2021-09-30 20:04:50 -07:00
|
|
|
const { query: latestQuery, offset } = useSearchQueryInUrl();
|
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.
|
2021-05-04 18:56:00 -07:00
|
|
|
forceReset: () => searchQueryIsEmpty(latestQuery),
|
2021-01-21 16:03:14 -08:00
|
|
|
});
|
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
|
|
|
) {
|
2021-06-21 10:31:55 -07:00
|
|
|
itemSearch: itemSearchV2(
|
2021-01-18 15:56:24 -08:00
|
|
|
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-06-21 13:48:45 -07:00
|
|
|
id
|
2021-02-06 21:50:52 -08:00
|
|
|
numTotalItems
|
2021-06-21 10:31:55 -07:00
|
|
|
items(offset: $offset, limit: 30) {
|
2021-01-18 15:56:24 -08:00
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
2021-04-01 20:08:35 -07:00
|
|
|
isNc
|
2021-05-13 16:24:24 -07:00
|
|
|
isPb
|
2021-06-08 01:03:09 -07:00
|
|
|
currentUserOwnsThis
|
|
|
|
currentUserWantsThis
|
2021-01-18 15:56:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
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-06-21 10:31:55 -07:00
|
|
|
// This will give us the cached numTotalItems while we wait for the
|
|
|
|
// next item page!
|
|
|
|
returnPartialData: true,
|
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 (error) {
|
2021-06-21 10:21:53 -07:00
|
|
|
return <MajorErrorMessage error={error} variant="network" />;
|
2021-01-21 16:39:43 -08:00
|
|
|
}
|
|
|
|
|
2021-06-21 10:31:55 -07:00
|
|
|
if (data?.itemSearch?.numTotalItems === 0) {
|
2021-01-21 16:39:43 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-21 10:31:55 -07:00
|
|
|
const items = data?.itemSearch?.items || [];
|
|
|
|
const numTotalItems = data?.itemSearch?.numTotalItems || null;
|
|
|
|
|
2021-01-18 15:56:24 -08:00
|
|
|
return (
|
|
|
|
<Box>
|
2021-05-21 00:50:55 -07:00
|
|
|
<PaginationToolbar
|
2021-06-21 10:31:55 -07:00
|
|
|
totalCount={numTotalItems}
|
|
|
|
isLoading={loading}
|
2021-02-10 14:23:25 -08:00
|
|
|
marginBottom="6"
|
|
|
|
/>
|
2021-06-21 10:31:55 -07:00
|
|
|
{items.length > 0 && (
|
|
|
|
<Wrap justify="center" spacing="4">
|
|
|
|
{items.map((item) => (
|
|
|
|
<WrapItem key={item.id}>
|
|
|
|
<SquareItemCard item={item} />
|
|
|
|
</WrapItem>
|
|
|
|
))}
|
|
|
|
</Wrap>
|
|
|
|
)}
|
|
|
|
{loading && items.length === 0 && (
|
|
|
|
<Delay>
|
|
|
|
<ItemSearchPageResultsLoading />
|
|
|
|
</Delay>
|
|
|
|
)}
|
2021-05-21 00:50:55 -07:00
|
|
|
<PaginationToolbar
|
2021-06-21 10:31:55 -07:00
|
|
|
totalCount={numTotalItems}
|
|
|
|
isLoading={loading}
|
2021-02-10 14:23:25 -08:00
|
|
|
marginTop="4"
|
|
|
|
/>
|
2021-01-18 15:56:24 -08:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|