1
0
Fork 0
forked from OpenNeo/impress
impress/src/useItemData.js

44 lines
1 KiB
JavaScript
Raw Normal View History

2020-04-22 14:55:12 -07:00
import gql from "graphql-tag";
import { useQuery } from "@apollo/react-hooks";
import { ITEMS } from "./data";
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
# This is used for wearItem actions, to resolve conflicts. We don't
# use it directly; we just expect it to be in the cache!
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
layers {
zone {
id
}
}
}
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) {
const hardcodedItem = ITEMS.find((i) => i.id === item.id);
itemsById[item.id] = {
...hardcodedItem,
...item,
};
}
return { loading, error, itemsById };
}
export default useItemData;