2020-09-04 05:59:35 -07:00
|
|
|
import React from "react";
|
|
|
|
import { Box, Image, 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 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
|
|
|
|
itemsTheyOwn {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{ variables: { userId } }
|
|
|
|
);
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return (
|
2020-09-06 18:12:34 -07:00
|
|
|
<Box display="flex" justifyContent="center">
|
2020-09-04 05:59:35 -07:00
|
|
|
<HangerSpinner boxSize="48px" />
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2020-09-06 18:12:34 -07:00
|
|
|
return <Box color="red.400">{error.message}</Box>;
|
2020-09-04 05:59:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-09-06 18:45:20 -07:00
|
|
|
<Box>
|
2020-09-06 18:12:34 -07:00
|
|
|
<Heading1 marginBottom="8">
|
2020-09-04 05:59:35 -07:00
|
|
|
{isCurrentUser ? "Items you own" : `Items ${data.user.username} owns`}
|
|
|
|
</Heading1>
|
|
|
|
<Wrap justify="center">
|
|
|
|
{data.user.itemsTheyOwn.map((item) => (
|
|
|
|
<Box key={item.id} width="100px" textAlign="center">
|
|
|
|
<Image
|
|
|
|
src={item.thumbnailUrl}
|
|
|
|
alt=""
|
|
|
|
height="80px"
|
|
|
|
width="80px"
|
|
|
|
boxShadow="md"
|
|
|
|
margin="0 auto"
|
|
|
|
/>
|
|
|
|
{item.name}
|
|
|
|
</Box>
|
|
|
|
))}
|
|
|
|
</Wrap>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ItemsPage;
|