impress-2020/src/app/ItemsPage.js

128 lines
3 KiB
JavaScript
Raw Normal View History

import React from "react";
import { Box, Wrap } from "@chakra-ui/core";
import gql from "graphql-tag";
import { useParams } from "react-router-dom";
import { useQuery } from "@apollo/client";
import HangerSpinner from "./components/HangerSpinner";
import { Heading1 } from "./util";
import ItemCard, {
ItemBadgeList,
NcBadge,
NpBadge,
YouOwnThisBadge,
YouWantThisBadge,
} from "./components/ItemCard";
import useCurrentUser from "./components/useCurrentUser";
function ItemsPage() {
const { userId } = useParams();
const currentUser = useCurrentUser();
const isCurrentUser = currentUser.id === userId;
const { loading, error, data } = useQuery(
gql`
query ItemsPage($userId: ID!) {
user(id: $userId) {
id
username
2020-09-11 21:34:28 -07:00
itemsTheyOwn {
id
isNc
name
thumbnailUrl
}
2020-09-11 21:34:28 -07:00
itemsTheyWant {
id
isNc
name
thumbnailUrl
}
}
currentUser {
itemsTheyOwn {
id
}
itemsTheyWant {
id
}
}
}
`,
{ variables: { userId } }
);
if (loading) {
return (
2020-09-06 18:12:34 -07:00
<Box display="flex" justifyContent="center">
<HangerSpinner />
</Box>
);
}
if (error) {
2020-09-06 18:12:34 -07:00
return <Box color="red.400">{error.message}</Box>;
}
const itemIdsYouOwn = new Set(data.currentUser.itemsTheyOwn.map((i) => i.id));
const itemIdsYouWant = new Set(
data.currentUser.itemsTheyWant.map((i) => i.id)
);
return (
2020-09-06 18:45:20 -07:00
<Box>
2020-09-06 18:12:34 -07:00
<Heading1 marginBottom="8">
{isCurrentUser ? "Items you own" : `Items ${data.user.username} owns`}
</Heading1>
<Wrap justify="center">
{data.user.itemsTheyOwn.map((item) => (
<ItemCard
key={item.id}
item={item}
badges={
<ItemBadgeList>
{item.isNc ? <NcBadge /> : <NpBadge />}
{
// This helps you compare your owns/wants to other users.
!isCurrentUser && itemIdsYouWant.has(item.id) && (
<YouWantThisBadge />
)
}
</ItemBadgeList>
}
/>
))}
</Wrap>
2020-09-11 21:34:28 -07:00
<Heading1 marginBottom="8" marginTop="8">
{isCurrentUser ? "Items you want" : `Items ${data.user.username} wants`}
</Heading1>
<Wrap justify="center">
{data.user.itemsTheyWant.map((item) => (
<ItemCard
key={item.id}
item={item}
badges={
<ItemBadgeList>
{item.isNc ? <NcBadge /> : <NpBadge />}
{
// This helps you compare your owns/wants to other users.
!isCurrentUser && itemIdsYouOwn.has(item.id) && (
<YouOwnThisBadge />
)
}
2020-09-11 21:34:28 -07:00
</ItemBadgeList>
}
/>
))}
</Wrap>
</Box>
);
}
export default ItemsPage;