impress-2020/src/app/ModelingPage.js

110 lines
2.4 KiB
JavaScript
Raw Normal View History

import React from "react";
2020-09-11 23:53:57 -07:00
import { Badge, Box } from "@chakra-ui/core";
import gql from "graphql-tag";
import { useQuery } from "@apollo/client";
import { Delay } from "./util";
import HangerSpinner from "./components/HangerSpinner";
2020-09-11 23:53:57 -07:00
import { Heading1, Heading2, usePageTitle } from "./util";
import ItemCard, {
ItemBadgeList,
ItemCardList,
YouOwnThisBadge,
} from "./components/ItemCard";
function ModelingPage() {
2020-09-11 23:53:57 -07:00
usePageTitle("Modeling Hub");
return (
<Box>
<Heading1 marginBottom="2">Modeling Hub</Heading1>
2020-09-06 23:49:04 -07:00
<Heading2 marginBottom="2">Item models we need</Heading2>
<ItemModelsList />
</Box>
);
}
function ItemModelsList() {
const { loading, error, data } = useQuery(gql`
query ModelingPage {
itemsThatNeedModels {
id
name
thumbnailUrl
speciesThatNeedModels {
id
name
}
}
2020-09-06 23:58:18 -07:00
currentUser {
itemsTheyOwn {
id
}
}
}
`);
if (loading) {
return (
<Box
display="flex"
flexDirection="column"
alignItems="center"
marginTop="16"
>
<HangerSpinner />
<Box fontSize="xs" marginTop="1">
<Delay ms={2500}>Checking all the items</Delay>
</Box>
</Box>
);
}
if (error) {
return <Box color="red.400">{error.message}</Box>;
}
2020-09-06 23:49:04 -07:00
const items = data.itemsThatNeedModels
// enough MMEs are broken that I just don't want to deal right now!
.filter((item) => !item.name.includes("MME"))
.sort((a, b) => a.name.localeCompare(b.name));
2020-09-06 23:58:18 -07:00
const ownedItemIds = new Set(
data.currentUser?.itemsTheyOwn?.map((item) => item.id)
);
return (
<ItemCardList>
{items.map((item) => (
2020-09-06 23:58:18 -07:00
<ItemModelCard
key={item.id}
item={item}
currentUserOwnsItem={ownedItemIds.has(item.id)}
/>
))}
</ItemCardList>
);
}
2020-09-06 23:58:18 -07:00
function ItemModelCard({ item, currentUserOwnsItem, ...props }) {
const badges = (
<ItemModelBadges item={item} currentUserOwnsItem={currentUserOwnsItem} />
);
return <ItemCard item={item} badges={badges} {...props} />;
}
2020-09-06 23:58:18 -07:00
function ItemModelBadges({ item, currentUserOwnsItem }) {
2020-09-06 23:49:04 -07:00
return (
<ItemBadgeList>
{currentUserOwnsItem && <YouOwnThisBadge />}
2020-09-06 23:49:04 -07:00
{item.speciesThatNeedModels.map((species) => (
<Badge>{species.name}</Badge>
))}
</ItemBadgeList>
2020-09-06 23:49:04 -07:00
);
}
export default ModelingPage;