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 itemsTheyOwn { id isNc name thumbnailUrl } itemsTheyWant { id isNc name thumbnailUrl } } currentUser { itemsTheyOwn { id } itemsTheyWant { id } } } `, { variables: { userId } } ); if (loading) { return ( ); } if (error) { return {error.message}; } const itemIdsYouOwn = new Set(data.currentUser.itemsTheyOwn.map((i) => i.id)); const itemIdsYouWant = new Set( data.currentUser.itemsTheyWant.map((i) => i.id) ); return ( {isCurrentUser ? "Items you own" : `Items ${data.user.username} owns`} {data.user.itemsTheyOwn.map((item) => ( {item.isNc ? : } { // This helps you compare your owns/wants to other users. !isCurrentUser && itemIdsYouWant.has(item.id) && ( ) } } /> ))} {isCurrentUser ? "Items you want" : `Items ${data.user.username} wants`} {data.user.itemsTheyWant.map((item) => ( {item.isNc ? : } { // This helps you compare your owns/wants to other users. !isCurrentUser && itemIdsYouOwn.has(item.id) && ( ) } } /> ))} ); } export default ItemsPage;