add a badge to items the user owns

This commit is contained in:
Emi Matchu 2020-09-06 23:58:18 -07:00
parent 6a923a55a0
commit 626b36319a

View file

@ -1,5 +1,6 @@
import React from "react"; import React from "react";
import { Badge, Box, SimpleGrid } from "@chakra-ui/core"; import { Badge, Box, SimpleGrid } from "@chakra-ui/core";
import { StarIcon } from "@chakra-ui/icons";
import gql from "graphql-tag"; import gql from "graphql-tag";
import { useQuery } from "@apollo/client"; import { useQuery } from "@apollo/client";
@ -29,6 +30,12 @@ function ItemModelsList() {
name name
} }
} }
currentUser {
itemsTheyOwn {
id
}
}
} }
`); `);
@ -49,16 +56,24 @@ function ItemModelsList() {
.filter((item) => !item.name.includes("MME")) .filter((item) => !item.name.includes("MME"))
.sort((a, b) => a.name.localeCompare(b.name)); .sort((a, b) => a.name.localeCompare(b.name));
const ownedItemIds = new Set(
data.currentUser?.itemsTheyOwn?.map((item) => item.id)
);
return ( return (
<SimpleGrid columns={{ sm: 1, lg: 2 }} spacing="6"> <SimpleGrid columns={{ sm: 1, lg: 2 }} spacing="6">
{items.map((item) => ( {items.map((item) => (
<ItemModelCard key={item.id} item={item} /> <ItemModelCard
key={item.id}
item={item}
currentUserOwnsItem={ownedItemIds.has(item.id)}
/>
))} ))}
</SimpleGrid> </SimpleGrid>
); );
} }
function ItemModelCard({ item, ...props }) { function ItemModelCard({ item, currentUserOwnsItem, ...props }) {
return ( return (
<Box <Box
as="a" as="a"
@ -69,14 +84,28 @@ function ItemModelCard({ item, ...props }) {
width="400px" width="400px"
{...props} {...props}
> >
<ItemSummary item={item} badges={<ItemModelBadges item={item} />} /> <ItemSummary
item={item}
badges={
<ItemModelBadges
item={item}
currentUserOwnsItem={currentUserOwnsItem}
/>
}
/>
</Box> </Box>
); );
} }
function ItemModelBadges({ item }) { function ItemModelBadges({ item, currentUserOwnsItem }) {
return ( return (
<ItemSummaryBadgeList> <ItemSummaryBadgeList>
{currentUserOwnsItem && (
<Badge colorScheme="yellow" display="flex" alignItems="center">
<StarIcon aria-label="Star" marginRight="1" />
You own this!
</Badge>
)}
{item.speciesThatNeedModels.map((species) => ( {item.speciesThatNeedModels.map((species) => (
<Badge>{species.name}</Badge> <Badge>{species.name}</Badge>
))} ))}