diff --git a/src/WardrobePage.js b/src/WardrobePage.js index 13b8e40..876b683 100644 --- a/src/WardrobePage.js +++ b/src/WardrobePage.js @@ -21,9 +21,10 @@ import { useToast, } from "@chakra-ui/core"; -import ItemList, { ItemListSkeleton } from "./ItemList"; -import useOutfitState from "./useOutfitState.js"; import { ITEMS } from "./data"; +import ItemList, { ItemListSkeleton } from "./ItemList"; +import useItemData from "./useItemData"; +import useOutfitState from "./useOutfitState.js"; function WardrobePage() { const { loading, error, data, wearItem } = useOutfitState(); @@ -174,20 +175,47 @@ function SearchToolbar({ query, onChange }) { } function SearchPanel({ query, wornItemIds, onWearItem }) { + const { loading, error, itemsById } = useItemData(ITEMS.map((i) => i.id)); + const normalize = (s) => s.toLowerCase(); - const results = ITEMS.filter((item) => + const results = Object.values(itemsById).filter((item) => normalize(item.name).includes(normalize(query)) ); results.sort((a, b) => a.name.localeCompare(b.name)); - const resultsSection = - results.length > 0 ? ( - + Searching for "{query}" + - ) : ( + + ); +} + +function SearchResults({ loading, error, results, wornItemIds, onWearItem }) { + if (loading) { + return ; + } + + if (error) { + return ( + + We hit an error trying to load your search results + + 😓 + {" "} + Try again? + + ); + } + + if (results.length === 0) { + return ( We couldn't find any matching items{" "} @@ -196,12 +224,14 @@ function SearchPanel({ query, wornItemIds, onWearItem }) { Try again? ); + } return ( - - Searching for "{query}" - {resultsSection} - + ); }