impress/src/useItemData.js

39 lines
922 B
JavaScript
Raw Normal View History

2020-04-22 14:55:12 -07:00
import gql from "graphql-tag";
import { useQuery } from "@apollo/react-hooks";
2020-04-24 18:39:38 -07:00
function useItemData(itemIds, speciesId, colorId) {
2020-04-22 14:55:12 -07:00
const { loading, error, data } = useQuery(
gql`
2020-04-24 18:39:38 -07:00
query($itemIds: [ID!]!, $speciesId: ID!, $colorId: ID!) {
2020-04-22 14:55:12 -07:00
items(ids: $itemIds) {
id
name
thumbnailUrl
2020-04-24 18:39:38 -07:00
2020-04-24 19:16:24 -07:00
# This is used to group items by zone, and to detect conflicts when
# wearing a new item.
2020-04-24 18:39:38 -07:00
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
layers {
zone {
id
2020-04-24 19:16:24 -07:00
label
2020-04-24 18:39:38 -07:00
}
}
}
2020-04-22 14:55:12 -07:00
}
}
`,
2020-04-24 18:39:38 -07:00
{ variables: { itemIds, speciesId, colorId } }
2020-04-22 14:55:12 -07:00
);
const items = (data && data.items) || [];
const itemsById = {};
for (const item of items) {
2020-04-24 19:16:24 -07:00
itemsById[item.id] = item;
2020-04-22 14:55:12 -07:00
}
return { loading, error, itemsById };
}
export default useItemData;