oh oops, use gql for search prototype too!

This commit is contained in:
Matt Dunn-Rankin 2020-04-22 16:36:08 -07:00
parent 8e8eea2b39
commit 93507cc777

View file

@ -21,9 +21,10 @@ import {
useToast, useToast,
} from "@chakra-ui/core"; } from "@chakra-ui/core";
import ItemList, { ItemListSkeleton } from "./ItemList";
import useOutfitState from "./useOutfitState.js";
import { ITEMS } from "./data"; import { ITEMS } from "./data";
import ItemList, { ItemListSkeleton } from "./ItemList";
import useItemData from "./useItemData";
import useOutfitState from "./useOutfitState.js";
function WardrobePage() { function WardrobePage() {
const { loading, error, data, wearItem } = useOutfitState(); const { loading, error, data, wearItem } = useOutfitState();
@ -174,20 +175,47 @@ function SearchToolbar({ query, onChange }) {
} }
function SearchPanel({ query, wornItemIds, onWearItem }) { function SearchPanel({ query, wornItemIds, onWearItem }) {
const { loading, error, itemsById } = useItemData(ITEMS.map((i) => i.id));
const normalize = (s) => s.toLowerCase(); const normalize = (s) => s.toLowerCase();
const results = ITEMS.filter((item) => const results = Object.values(itemsById).filter((item) =>
normalize(item.name).includes(normalize(query)) normalize(item.name).includes(normalize(query))
); );
results.sort((a, b) => a.name.localeCompare(b.name)); results.sort((a, b) => a.name.localeCompare(b.name));
const resultsSection = return (
results.length > 0 ? ( <Box color="green.800">
<ItemList <Heading1 mb="6">Searching for "{query}"</Heading1>
items={results} <SearchResults
loading={loading}
error={error}
results={results}
wornItemIds={wornItemIds} wornItemIds={wornItemIds}
onWearItem={onWearItem} onWearItem={onWearItem}
/> />
) : ( </Box>
);
}
function SearchResults({ loading, error, results, wornItemIds, onWearItem }) {
if (loading) {
return <ItemListSkeleton />;
}
if (error) {
return (
<Text color="green.500">
We hit an error trying to load your search results
<span role="img" aria-label="(sweat emoji)">
😓
</span>{" "}
Try again?
</Text>
);
}
if (results.length === 0) {
return (
<Text color="green.500"> <Text color="green.500">
We couldn't find any matching items{" "} We couldn't find any matching items{" "}
<span role="img" aria-label="(thinking emoji)"> <span role="img" aria-label="(thinking emoji)">
@ -196,12 +224,14 @@ function SearchPanel({ query, wornItemIds, onWearItem }) {
Try again? Try again?
</Text> </Text>
); );
}
return ( return (
<Box color="green.800"> <ItemList
<Heading1 mb="6">Searching for "{query}"</Heading1> items={results}
{resultsSection} wornItemIds={wornItemIds}
</Box> onWearItem={onWearItem}
/>
); );
} }