refactor to use new query object in SearchPanel

not actually including zone in the GraphQL query yet, but it's available in the right location now!
This commit is contained in:
Emi Matchu 2020-09-01 19:53:38 -07:00
parent 479f43ed22
commit 821d05c141
3 changed files with 15 additions and 7 deletions

View file

@ -51,7 +51,7 @@ function ItemsAndSearchPanels({ loading, outfitState, dispatchToOutfit }) {
> >
<Box px="4" py="2"> <Box px="4" py="2">
<SearchPanel <SearchPanel
query={searchQuery.value} query={searchQuery}
outfitState={outfitState} outfitState={outfitState}
dispatchToOutfit={dispatchToOutfit} dispatchToOutfit={dispatchToOutfit}
scrollContainerRef={scrollContainerRef} scrollContainerRef={scrollContainerRef}

View file

@ -193,7 +193,10 @@ function useSearchResults(query, outfitState) {
// We debounce the search query, so that we don't resend a new query whenever // We debounce the search query, so that we don't resend a new query whenever
// the user types anything. // the user types anything.
const debouncedQuery = useDebounce(query, 300, { waitForFirstPause: true }); const debouncedQuery = useDebounce(query, 300, {
waitForFirstPause: true,
initialValue: { value: "", filterToZoneLabel: null },
});
// When the query changes, we should update our impression of whether we've // When the query changes, we should update our impression of whether we've
// reached the end! // reached the end!
@ -254,8 +257,8 @@ function useSearchResults(query, outfitState) {
${itemAppearanceFragment} ${itemAppearanceFragment}
`, `,
{ {
variables: { query: debouncedQuery, speciesId, colorId, offset: 0 }, variables: { query: debouncedQuery.value, speciesId, colorId, offset: 0 },
skip: debouncedQuery === null, skip: !debouncedQuery.value && !debouncedQuery.filterToZoneLabel,
notifyOnNetworkStatusChange: true, notifyOnNetworkStatusChange: true,
onCompleted: (d) => { onCompleted: (d) => {
// This is called each time the query completes, including on // This is called each time the query completes, including on

View file

@ -78,10 +78,15 @@ export function safeImageUrl(url) {
* *
* Adapted from https://usehooks.com/useDebounce/ * Adapted from https://usehooks.com/useDebounce/
*/ */
export function useDebounce(value, delay, { waitForFirstPause = false } = {}) { export function useDebounce(
value,
delay,
{ waitForFirstPause = false, initialValue = null } = {}
) {
// State and setters for debounced value // State and setters for debounced value
const initialValue = waitForFirstPause ? null : value; const [debouncedValue, setDebouncedValue] = React.useState(
const [debouncedValue, setDebouncedValue] = React.useState(initialValue); waitForFirstPause ? initialValue : value
);
React.useEffect( React.useEffect(
() => { () => {