import React from "react"; import { Badge, Box, Center, Wrap } from "@chakra-ui/core"; import { CheckIcon, EmailIcon, StarIcon } from "@chakra-ui/icons"; import gql from "graphql-tag"; import { useParams } from "react-router-dom"; import { useQuery } from "@apollo/client"; import HangerSpinner from "./components/HangerSpinner"; import { Heading1, Heading2 } from "./util"; import ItemCard, { ItemBadgeList, ItemCardList, NcBadge, NpBadge, YouOwnThisBadge, YouWantThisBadge, ZoneBadgeList, } from "./components/ItemCard"; import useCurrentUser from "./components/useCurrentUser"; import WIPCallout from "./components/WIPCallout"; function UserItemsPage() { 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 contactNeopetsUsername itemsTheyOwn { id isNc name thumbnailUrl currentUserWantsThis allOccupiedZones { id label @client } } itemsTheyWant { id isNc name thumbnailUrl currentUserOwnsThis allOccupiedZones { id label @client } } } } `, { variables: { userId } } ); if (loading) { return (
); } if (error) { return {error.message}; } // This helps you compare your owns/wants to other users! If they own // something, and you want it, we say "You want this!". And if they want // something, and you own it, we say "You own this!". const showYouOwnThisBadge = (item) => !isCurrentUser && item.currentUserOwnsThis; const showYouWantThisBadge = (item) => !isCurrentUser && item.currentUserWantsThis; const numYouOwnThisBadges = data.user.itemsTheyWant.filter( showYouOwnThisBadge ).length; const numYouWantThisBadges = data.user.itemsTheyOwn.filter( showYouWantThisBadge ).length; 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); }); return ( {isCurrentUser ? "Your items" : `${data.user.username}'s items`} {data.user.contactNeopetsUsername && ( {data.user.contactNeopetsUsername} )} {data.user.contactNeopetsUsername && ( Neomail )} {numYouOwnThisBadges > 0 && ( {numYouOwnThisBadges > 1 ? `${numYouOwnThisBadges} items you own` : "1 item you own"} )} {numYouWantThisBadges > 0 && ( {numYouWantThisBadges > 1 ? `${numYouWantThisBadges} items you want` : "1 item you want"} )} {isCurrentUser ? "Items you own" : `Items ${data.user.username} owns`} {sortedItemsTheyOwn.map((item) => { return ( {item.isNc ? : } {showYouWantThisBadge(item) && } } /> ); })} {isCurrentUser ? "Items you want" : `Items ${data.user.username} wants`} {sortedItemsTheyWant.map((item) => ( {item.isNc ? : } {showYouOwnThisBadge(item) && } } /> ))} ); } function NeopetsStarIcon(props) { // Converted from the Neopets favicon with https://www.vectorizer.io/. return ( ); } export default UserItemsPage;