2020-09-04 05:59:35 -07:00
|
|
|
import React from "react";
|
2020-10-24 00:08:22 -07:00
|
|
|
import { Badge, Box, Center, Wrap } from "@chakra-ui/core";
|
2020-10-24 01:06:55 -07:00
|
|
|
import { CheckIcon, EmailIcon, StarIcon } from "@chakra-ui/icons";
|
2020-09-04 05:59:35 -07:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { useQuery } from "@apollo/client";
|
|
|
|
|
|
|
|
import HangerSpinner from "./components/HangerSpinner";
|
2020-10-23 23:00:53 -07:00
|
|
|
import { Heading1, Heading2 } from "./util";
|
2020-09-11 20:41:39 -07:00
|
|
|
import ItemCard, {
|
|
|
|
ItemBadgeList,
|
2020-09-11 21:53:57 -07:00
|
|
|
ItemCardList,
|
2020-09-11 20:41:39 -07:00
|
|
|
NcBadge,
|
|
|
|
NpBadge,
|
2020-09-11 21:45:38 -07:00
|
|
|
YouOwnThisBadge,
|
|
|
|
YouWantThisBadge,
|
2020-10-23 23:29:50 -07:00
|
|
|
ZoneBadgeList,
|
2020-09-11 20:41:39 -07:00
|
|
|
} from "./components/ItemCard";
|
2020-09-04 05:59:35 -07:00
|
|
|
import useCurrentUser from "./components/useCurrentUser";
|
2020-10-10 02:07:49 -07:00
|
|
|
import WIPCallout from "./components/WIPCallout";
|
2020-09-04 05:59:35 -07:00
|
|
|
|
2020-09-11 23:20:06 -07:00
|
|
|
function UserItemsPage() {
|
2020-09-04 05:59:35 -07:00
|
|
|
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-10-23 23:00:53 -07:00
|
|
|
contactNeopetsUsername
|
2020-09-11 21:34:28 -07:00
|
|
|
|
2020-09-04 05:59:35 -07:00
|
|
|
itemsTheyOwn {
|
|
|
|
id
|
2020-09-11 20:41:39 -07:00
|
|
|
isNc
|
2020-09-04 05:59:35 -07:00
|
|
|
name
|
|
|
|
thumbnailUrl
|
2020-10-24 01:35:37 -07:00
|
|
|
currentUserWantsThis
|
2020-10-23 23:29:50 -07:00
|
|
|
allOccupiedZones {
|
|
|
|
id
|
|
|
|
label @client
|
|
|
|
}
|
2020-09-04 05:59:35 -07:00
|
|
|
}
|
2020-09-11 21:34:28 -07:00
|
|
|
|
|
|
|
itemsTheyWant {
|
|
|
|
id
|
|
|
|
isNc
|
|
|
|
name
|
|
|
|
thumbnailUrl
|
2020-10-24 01:35:37 -07:00
|
|
|
currentUserOwnsThis
|
2020-10-23 23:29:50 -07:00
|
|
|
allOccupiedZones {
|
|
|
|
id
|
|
|
|
label @client
|
|
|
|
}
|
2020-09-11 21:34:28 -07:00
|
|
|
}
|
2020-09-04 05:59:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
{ variables: { userId } }
|
|
|
|
);
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return (
|
2020-09-11 23:27:23 -07:00
|
|
|
<Center>
|
2020-09-10 03:06:44 -07:00
|
|
|
<HangerSpinner />
|
2020-09-11 23:27:23 -07:00
|
|
|
</Center>
|
2020-09-04 05:59:35 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-11 22:04:37 -07:00
|
|
|
// This helps you compare your owns/wants to other users! If they own
|
2020-09-11 22:36:38 -07:00
|
|
|
// something, and you want it, we say "You want this!". And if they want
|
|
|
|
// something, and you own it, we say "You own this!".
|
2020-09-11 22:04:37 -07:00
|
|
|
const showYouOwnThisBadge = (item) =>
|
2020-10-24 01:35:37 -07:00
|
|
|
!isCurrentUser && item.currentUserOwnsThis;
|
2020-09-11 22:04:37 -07:00
|
|
|
const showYouWantThisBadge = (item) =>
|
2020-10-24 01:35:37 -07:00
|
|
|
!isCurrentUser && item.currentUserWantsThis;
|
2020-09-11 22:04:37 -07:00
|
|
|
|
2020-10-24 01:06:55 -07:00
|
|
|
const numYouOwnThisBadges = data.user.itemsTheyWant.filter(
|
|
|
|
showYouOwnThisBadge
|
|
|
|
).length;
|
|
|
|
const numYouWantThisBadges = data.user.itemsTheyOwn.filter(
|
|
|
|
showYouWantThisBadge
|
|
|
|
).length;
|
|
|
|
|
2020-09-11 22:04:37 -07:00
|
|
|
const sortedItemsTheyOwn = [...data.user.itemsTheyOwn].sort((a, b) => {
|
|
|
|
// This is a cute sort hack. We sort first by, bringing "You want this!" to
|
|
|
|
// the top, and then sorting by name _within_ those two groups.
|
|
|
|
const aName = `${showYouWantThisBadge(a) ? "000" : "999"} ${a.name}`;
|
|
|
|
const bName = `${showYouWantThisBadge(b) ? "000" : "999"} ${b.name}`;
|
|
|
|
return aName.localeCompare(bName);
|
|
|
|
});
|
|
|
|
|
|
|
|
const sortedItemsTheyWant = [...data.user.itemsTheyWant].sort((a, b) => {
|
|
|
|
// This is a cute sort hack. We sort first by, bringing "You own this!" to
|
|
|
|
// the top, and then sorting by name _within_ those two groups.
|
|
|
|
const aName = `${showYouOwnThisBadge(a) ? "000" : "999"} ${a.name}`;
|
|
|
|
const bName = `${showYouOwnThisBadge(b) ? "000" : "999"} ${b.name}`;
|
|
|
|
return aName.localeCompare(bName);
|
|
|
|
});
|
|
|
|
|
2020-09-04 05:59:35 -07:00
|
|
|
return (
|
2020-09-06 18:45:20 -07:00
|
|
|
<Box>
|
2020-10-10 02:07:49 -07:00
|
|
|
<Box float="right">
|
|
|
|
<WIPCallout details="These lists are simplified and read-only for now. Full power coming soon!" />
|
|
|
|
</Box>
|
2020-10-24 00:08:22 -07:00
|
|
|
<Heading1>
|
2020-10-23 23:00:53 -07:00
|
|
|
{isCurrentUser ? "Your items" : `${data.user.username}'s items`}
|
2020-09-04 05:59:35 -07:00
|
|
|
</Heading1>
|
2020-10-24 01:06:55 -07:00
|
|
|
<Wrap spacing="2" opacity="0.7">
|
|
|
|
{data.user.contactNeopetsUsername && (
|
2020-10-24 00:08:22 -07:00
|
|
|
<Badge
|
|
|
|
as="a"
|
|
|
|
href={`http://www.neopets.com/userlookup.phtml?user=${data.user.contactNeopetsUsername}`}
|
2020-10-24 01:06:55 -07:00
|
|
|
display="flex"
|
2020-10-24 00:08:22 -07:00
|
|
|
alignItems="center"
|
|
|
|
>
|
|
|
|
<NeopetsStarIcon marginRight="1" />
|
|
|
|
{data.user.contactNeopetsUsername}
|
|
|
|
</Badge>
|
2020-10-24 01:06:55 -07:00
|
|
|
)}
|
|
|
|
{data.user.contactNeopetsUsername && (
|
2020-10-24 00:08:22 -07:00
|
|
|
<Badge
|
|
|
|
as="a"
|
|
|
|
href={`http://www.neopets.com/neomessages.phtml?type=send&recipient=${data.user.contactNeopetsUsername}`}
|
2020-10-24 01:06:55 -07:00
|
|
|
display="flex"
|
2020-10-24 00:08:22 -07:00
|
|
|
alignItems="center"
|
|
|
|
>
|
|
|
|
<EmailIcon marginRight="1" />
|
|
|
|
Neomail
|
|
|
|
</Badge>
|
2020-10-24 01:06:55 -07:00
|
|
|
)}
|
2020-10-27 21:43:57 -07:00
|
|
|
{/* Usually I put "Own" before "Want", but this matches the natural
|
|
|
|
* order on the page: the _matches_ for things you want are things
|
|
|
|
* _this user_ owns, so they come first. I think it's also probably a
|
|
|
|
* more natural train of thought: you come to someone's list _wanting_
|
|
|
|
* something, and _then_ thinking about what you can offer. */}
|
2020-10-24 01:06:55 -07:00
|
|
|
{numYouWantThisBadges > 0 && (
|
|
|
|
<Badge
|
|
|
|
as="a"
|
|
|
|
href="#owned-items"
|
|
|
|
colorScheme="blue"
|
|
|
|
display="flex"
|
|
|
|
alignItems="center"
|
|
|
|
>
|
|
|
|
<StarIcon marginRight="1" />
|
|
|
|
{numYouWantThisBadges > 1
|
|
|
|
? `${numYouWantThisBadges} items you want`
|
|
|
|
: "1 item you want"}
|
|
|
|
</Badge>
|
|
|
|
)}
|
2020-10-27 21:43:57 -07:00
|
|
|
{numYouOwnThisBadges > 0 && (
|
|
|
|
<Badge
|
|
|
|
as="a"
|
|
|
|
href="#wanted-items"
|
|
|
|
colorScheme="green"
|
|
|
|
display="flex"
|
|
|
|
alignItems="center"
|
|
|
|
>
|
|
|
|
<CheckIcon marginRight="1" />
|
|
|
|
{numYouOwnThisBadges > 1
|
|
|
|
? `${numYouOwnThisBadges} items you own`
|
|
|
|
: "1 item you own"}
|
|
|
|
</Badge>
|
|
|
|
)}
|
2020-10-24 01:06:55 -07:00
|
|
|
</Wrap>
|
|
|
|
<Heading2 id="owned-items" marginTop="4" marginBottom="6">
|
2020-10-23 23:00:53 -07:00
|
|
|
{isCurrentUser ? "Items you own" : `Items ${data.user.username} owns`}
|
|
|
|
</Heading2>
|
2020-09-11 21:53:57 -07:00
|
|
|
<ItemCardList>
|
2020-10-23 23:29:50 -07:00
|
|
|
{sortedItemsTheyOwn.map((item) => {
|
|
|
|
return (
|
|
|
|
<ItemCard
|
|
|
|
key={item.id}
|
|
|
|
item={item}
|
|
|
|
badges={
|
|
|
|
<ItemBadgeList>
|
|
|
|
{item.isNc ? <NcBadge /> : <NpBadge />}
|
|
|
|
{showYouWantThisBadge(item) && <YouWantThisBadge />}
|
|
|
|
<ZoneBadgeList
|
|
|
|
zones={item.allOccupiedZones}
|
|
|
|
variant="occupies"
|
|
|
|
/>
|
|
|
|
</ItemBadgeList>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
2020-09-11 21:53:57 -07:00
|
|
|
</ItemCardList>
|
2020-09-11 21:34:28 -07:00
|
|
|
|
2020-10-24 01:06:55 -07:00
|
|
|
<Heading2 id="wanted-items" marginBottom="6" marginTop="8">
|
2020-09-11 21:34:28 -07:00
|
|
|
{isCurrentUser ? "Items you want" : `Items ${data.user.username} wants`}
|
2020-10-23 23:00:53 -07:00
|
|
|
</Heading2>
|
2020-09-11 21:53:57 -07:00
|
|
|
<ItemCardList>
|
2020-09-11 22:04:37 -07:00
|
|
|
{sortedItemsTheyWant.map((item) => (
|
2020-09-11 21:34:28 -07:00
|
|
|
<ItemCard
|
|
|
|
key={item.id}
|
|
|
|
item={item}
|
|
|
|
badges={
|
|
|
|
<ItemBadgeList>
|
|
|
|
{item.isNc ? <NcBadge /> : <NpBadge />}
|
2020-09-11 22:04:37 -07:00
|
|
|
{showYouOwnThisBadge(item) && <YouOwnThisBadge />}
|
2020-10-23 23:29:50 -07:00
|
|
|
<ZoneBadgeList
|
|
|
|
zones={item.allOccupiedZones}
|
|
|
|
variant="occupies"
|
|
|
|
/>
|
2020-09-11 21:34:28 -07:00
|
|
|
</ItemBadgeList>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
))}
|
2020-09-11 21:53:57 -07:00
|
|
|
</ItemCardList>
|
2020-09-04 05:59:35 -07:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-24 00:08:22 -07:00
|
|
|
function NeopetsStarIcon(props) {
|
2020-10-24 00:14:42 -07:00
|
|
|
// Converted from the Neopets favicon with https://www.vectorizer.io/.
|
2020-10-24 00:08:22 -07:00
|
|
|
return (
|
|
|
|
<Box {...props}>
|
|
|
|
<svg
|
|
|
|
version="1.0"
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
width="1.2em"
|
|
|
|
height="1.2em"
|
|
|
|
viewBox="0 0 160 160"
|
|
|
|
preserveAspectRatio="xMidYMid meet"
|
|
|
|
>
|
|
|
|
<path
|
|
|
|
fill="currentColor"
|
|
|
|
fill-rule="evenodd"
|
|
|
|
d="M85 129 L60 108 40 119 C11 135,7 132,24 108 L39 86 23 68 L6 50 32 50 L58 50 73 29 L88 8 94 29 L101 50 128 50 L155 50 131 68 L107 86 113 118 C121 155,118 156,85 129 "
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-11 23:20:06 -07:00
|
|
|
export default UserItemsPage;
|