import React from "react";
import { Box, Center, Flex, Wrap, WrapItem } from "@chakra-ui/react";
import { ClassNames } from "@emotion/react";
import gql from "graphql-tag";
import { useQuery } from "@apollo/client";
import { Link, useLocation } from "react-router-dom";
import { Heading1, MajorErrorMessage, useCommonStyles } from "./util";
import HangerSpinner from "./components/HangerSpinner";
import OutfitThumbnail from "./components/OutfitThumbnail";
import PaginationToolbar from "./components/PaginationToolbar";
import useCurrentUser from "./components/useCurrentUser";
function UserOutfitsPage() {
return (
Your outfits
);
}
const USER_OUTFITS_PAGE_QUERY = gql`
query UserOutfitsPageContent($offset: Int!) {
currentUser {
id
numTotalOutfits
outfits(limit: 20, offset: $offset) {
id
name
updatedAt
# For alt text
petAppearance {
species {
id
name
}
color {
id
name
}
}
wornItems {
id
name
}
}
}
}
`;
const PER_PAGE = 20;
function UserOutfitsPageContent() {
const { isLoggedIn, isLoading: userLoading } = useCurrentUser();
const { search } = useLocation();
const offset = parseInt(new URLSearchParams(search).get("offset")) || 0;
const { loading: queryLoading, error, data } = useQuery(
USER_OUTFITS_PAGE_QUERY,
{
variables: { offset },
context: { sendAuth: true },
skip: !isLoggedIn,
// This will give us the cached numTotalOutfits while we wait for the
// next page!
returnPartialData: true,
}
);
const numTotalOutfits = data?.currentUser?.numTotalOutfits || null;
// Preload the previous and next pages. (Sigh, if we were doing cool Next.js
// stuff, this would already be happening by next/link magic I think!)
const prevPageOffset = offset - PER_PAGE;
const nextPageOffset = offset + PER_PAGE;
useQuery(USER_OUTFITS_PAGE_QUERY, {
variables: { offset: prevPageOffset },
context: { sendAuth: true },
skip: userLoading || offset === 0 || prevPageOffset < 0,
});
useQuery(USER_OUTFITS_PAGE_QUERY, {
variables: { offset: nextPageOffset },
context: { sendAuth: true },
skip:
userLoading ||
numTotalOutfits == null ||
nextPageOffset >= numTotalOutfits,
});
const isLoading = userLoading || queryLoading;
if (error) {
return ;
}
const outfits = data?.currentUser?.outfits || [];
return (
{isLoading ? (
) : !isLoggedIn ? (
You can see your list of saved outfits here, once you create an
account and log in!
) : outfits.length === 0 ? (
You don't have any outfits yet. Maybe you can create some!
) : (
{outfits.map((outfit) => (
))}
)}
);
}
function OutfitCard({ outfit }) {
const image = (
{({ css }) => (
)}
);
return (
);
}
function OutfitCardLayout({ image, caption }) {
const { brightBackground } = useCommonStyles();
return (
{image}
{caption}
);
}
function buildOutfitAltText(outfit) {
const { petAppearance, wornItems } = outfit;
const { species, color } = petAppearance;
let altText = "";
const petDescription = `${color.name} ${species.name}`;
altText += petDescription;
if (wornItems.length > 0) {
const itemNames = wornItems
.map((item) => item.name)
.sort()
.join(", ");
altText += ` wearing ${itemNames}`;
}
return altText;
}
export default UserOutfitsPage;