From 93507cc777a024cb0527c9cfbed4486c9b2ee599 Mon Sep 17 00:00:00 2001 From: Matt Dunn-Rankin Date: Wed, 22 Apr 2020 16:36:08 -0700 Subject: [PATCH] oh oops, use gql for search prototype too! --- src/WardrobePage.js | 54 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 12 deletions(-) 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} - + ); }