Compare commits
3 commits
87782767f8
...
6a0afb330b
Author | SHA1 | Date | |
---|---|---|---|
6a0afb330b | |||
4f069a5742 | |||
ece7a02902 |
3 changed files with 14 additions and 145 deletions
|
@ -1,10 +1,6 @@
|
|||
import React from "react";
|
||||
import gql from "graphql-tag";
|
||||
import { useQuery } from "@apollo/client";
|
||||
import { useDebounce, useLocalStorage } from "../util";
|
||||
import { useDebounce } from "../util";
|
||||
import { useItemSearch } from "../loaders/items";
|
||||
import { emptySearchQuery, searchQueryIsEmpty } from "./SearchToolbar";
|
||||
import { itemAppearanceFragment } from "../components/useOutfitAppearance";
|
||||
import { SEARCH_PER_PAGE } from "./SearchPanel";
|
||||
|
||||
/**
|
||||
|
@ -25,154 +21,21 @@ export function useSearchResults(
|
|||
initialValue: emptySearchQuery,
|
||||
});
|
||||
|
||||
// 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 currentPageIndex = currentPageNumber - 1;
|
||||
const offset = currentPageIndex * SEARCH_PER_PAGE;
|
||||
|
||||
// Until the new item search is ready, we can toggle between them! Use
|
||||
// `setItemSearchQueryMode` in the JS console to choose "gql" or "new".
|
||||
const [queryMode, setQueryMode] = useLocalStorage(
|
||||
"DTIItemSearchQueryMode",
|
||||
"gql",
|
||||
);
|
||||
React.useEffect(() => {
|
||||
window.setItemSearchQueryMode = setQueryMode;
|
||||
}, [setQueryMode]);
|
||||
|
||||
const {
|
||||
loading: loadingGQL,
|
||||
error: errorGQL,
|
||||
data: dataGQL,
|
||||
} = useQuery(
|
||||
gql`
|
||||
query SearchPanel(
|
||||
$query: String!
|
||||
$fitsPet: FitsPetSearchFilter
|
||||
$itemKind: ItemKindSearchFilter
|
||||
$currentUserOwnsOrWants: OwnsOrWants
|
||||
$zoneIds: [ID!]!
|
||||
$speciesId: ID!
|
||||
$colorId: ID!
|
||||
$altStyleId: ID
|
||||
$offset: Int!
|
||||
$perPage: Int!
|
||||
) {
|
||||
itemSearch: itemSearchV2(
|
||||
query: $query
|
||||
fitsPet: $fitsPet
|
||||
itemKind: $itemKind
|
||||
currentUserOwnsOrWants: $currentUserOwnsOrWants
|
||||
zoneIds: $zoneIds
|
||||
) {
|
||||
id
|
||||
numTotalItems
|
||||
items(offset: $offset, limit: $perPage) {
|
||||
# TODO: De-dupe this from useOutfitState?
|
||||
id
|
||||
name
|
||||
thumbnailUrl
|
||||
isNc
|
||||
isPb
|
||||
currentUserOwnsThis
|
||||
currentUserWantsThis
|
||||
|
||||
appearanceOn(
|
||||
speciesId: $speciesId
|
||||
colorId: $colorId
|
||||
altStyleId: $altStyleId
|
||||
) {
|
||||
# This enables us to quickly show the item when the user clicks it!
|
||||
...ItemAppearanceForOutfitPreview
|
||||
|
||||
# This is used to group items by zone, and to detect conflicts when
|
||||
# wearing a new item.
|
||||
layers {
|
||||
zone {
|
||||
id
|
||||
label
|
||||
}
|
||||
}
|
||||
restrictedZones {
|
||||
id
|
||||
label
|
||||
isCommonlyUsedByItems
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
${itemAppearanceFragment}
|
||||
`,
|
||||
{
|
||||
variables: {
|
||||
query: debouncedQuery.value,
|
||||
fitsPet: { speciesId, colorId, altStyleId },
|
||||
itemKind: debouncedQuery.filterToItemKind,
|
||||
currentUserOwnsOrWants: debouncedQuery.filterToCurrentUserOwnsOrWants,
|
||||
zoneIds: filterToZoneIds,
|
||||
speciesId,
|
||||
colorId,
|
||||
altStyleId,
|
||||
offset,
|
||||
perPage: SEARCH_PER_PAGE,
|
||||
},
|
||||
context: { sendAuth: true },
|
||||
skip:
|
||||
skip ||
|
||||
queryMode !== "gql" ||
|
||||
(!debouncedQuery.value &&
|
||||
!debouncedQuery.filterToItemKind &&
|
||||
!debouncedQuery.filterToZoneLabel &&
|
||||
!debouncedQuery.filterToCurrentUserOwnsOrWants),
|
||||
onError: (e) => {
|
||||
console.error("Error loading search results", e);
|
||||
},
|
||||
// Return `numTotalItems` from the GQL cache while waiting for next page!
|
||||
returnPartialData: true,
|
||||
},
|
||||
);
|
||||
|
||||
const {
|
||||
isLoading: loadingQuery,
|
||||
error: errorQuery,
|
||||
data: dataQuery,
|
||||
} = useItemSearch(
|
||||
const { isLoading, error, data } = useItemSearch(
|
||||
{
|
||||
filters: buildSearchFilters(debouncedQuery, outfitState),
|
||||
withAppearancesFor: { speciesId, colorId, altStyleId },
|
||||
page: currentPageIndex + 1,
|
||||
page: currentPageNumber,
|
||||
perPage: SEARCH_PER_PAGE,
|
||||
},
|
||||
{
|
||||
enabled:
|
||||
!skip && queryMode === "new" && !searchQueryIsEmpty(debouncedQuery),
|
||||
enabled: !skip && !searchQueryIsEmpty(debouncedQuery),
|
||||
},
|
||||
);
|
||||
|
||||
const loading =
|
||||
debouncedQuery !== query ||
|
||||
(queryMode === "gql" ? loadingGQL : loadingQuery);
|
||||
const error = queryMode === "gql" ? errorGQL : errorQuery;
|
||||
const items =
|
||||
(queryMode === "gql" ? dataGQL?.itemSearch?.items : dataQuery?.items) ?? [];
|
||||
const numTotalPages =
|
||||
(queryMode === "gql"
|
||||
? Math.ceil((dataGQL?.itemSearch?.numTotalItems ?? 0) / SEARCH_PER_PAGE)
|
||||
: dataQuery?.numTotalPages) ?? 0;
|
||||
const loading = debouncedQuery !== query || isLoading;
|
||||
const items = data?.items ?? [];
|
||||
const numTotalPages = data?.numTotalPages ?? 0;
|
||||
|
||||
return { loading, error, items, numTotalPages };
|
||||
}
|
||||
|
|
|
@ -98,7 +98,6 @@ async function loadItemSearch(searchOptions) {
|
|||
|
||||
return result;
|
||||
}
|
||||
window.loadItemSearch = loadItemSearch;
|
||||
|
||||
/**
|
||||
* writeItemToApolloCache is one last important bridge between our loaders and
|
||||
|
|
|
@ -6,6 +6,13 @@
|
|||
current_user_lists: @current_user_lists,
|
||||
current_user_quantities: @current_user_quantities}
|
||||
|
||||
- if @item.name.include? "Baby Body Paint"
|
||||
%p.warning
|
||||
The Baby Body Paint items seem to have new zone restriction rules that our
|
||||
system doesn't support yet, whuh oh! This might require major changes to
|
||||
how we handle zones. Until then, these items will be <em>very</em> buggy,
|
||||
sorry!
|
||||
|
||||
#outfit-preview-root{'data-item-id': @item.id}
|
||||
|
||||
- unless @contributors_with_counts.empty?
|
||||
|
|
Loading…
Reference in a new issue