diff --git a/components/LoadingBoundary.tsx b/components/LoadingBoundary.tsx deleted file mode 100644 index 91e4b2fdd..000000000 --- a/components/LoadingBoundary.tsx +++ /dev/null @@ -1,38 +0,0 @@ -import { ApolloError } from "@apollo/client"; -import { Alert, AlertDescription, AlertIcon, Spinner } from "@chakra-ui/core"; -import { useRouter } from "next/router"; -import { useEffect, useState } from "react"; - -interface Props { - children: React.ReactNode; - loading?: boolean; - error?: ApolloError; -} - -const LoadingBoundary: React.FC = ({ children, loading, error }) => { - const [showSpinner, setShowSpinner] = useState(false); - const router = useRouter(); - - useEffect(() => { - const timer = setTimeout(() => setShowSpinner(true), 1000); - - return () => clearTimeout(timer); - }, []); - - if (loading || router.isFallback) { - return showSpinner ? : null; - } - - if (error) { - return ( - - - {error.message} - - ); - } - - return <>{children}; -}; - -export default LoadingBoundary; diff --git a/pages/player/[id].tsx b/pages/player/[id].tsx index 83f4f95b8..fda0fcb34 100644 --- a/pages/player/[id].tsx +++ b/pages/player/[id].tsx @@ -1,12 +1,11 @@ +import { t } from "@lingui/macro"; import { PrismaClient } from "@prisma/client"; -import LoadingBoundary from "components/LoadingBoundary"; -import { - GetPlayersXRankPlacementsDocument, - useGetPlayersXRankPlacementsQuery, -} from "generated/graphql"; -import { initializeApollo } from "lib/apollo"; +import Breadcrumbs from "components/Breadcrumbs"; import { GetStaticPaths, GetStaticProps } from "next"; -import { useRouter } from "next/router"; +import { + GetPlayersTop500Placements, + getPlayersTop500Placements, +} from "prisma/queries/getPlayersTop500Placements"; import Player from "scenes/Player"; const prisma = new PrismaClient(); @@ -20,41 +19,38 @@ export const getStaticPaths: GetStaticPaths = async () => { }; }; -export const getStaticProps: GetStaticProps = async ({ params }) => { - const apolloClient = initializeApollo(null, { prisma: new PrismaClient() }); +interface Props { + placements: GetPlayersTop500Placements; +} - const data = await apolloClient.query({ - query: GetPlayersXRankPlacementsDocument, - variables: { - // FIXME: why ! needed? - switchAccountId: params!.id, - }, +export const getStaticProps: GetStaticProps = async ({ params }) => { + const placements = await getPlayersTop500Placements({ + prisma, + switchAccountId: params!.id as string, }); - console.log({ params }); - console.log({ data }); - return { props: { - initialApolloState: apolloClient.cache.extract(), - switchAccountId: params!.id, + placements, }, - //notfound + notFound: !placements.length, }; }; -const PlayerPage = ({ switchAccountId }: { switchAccountId: string }) => { - const router = useRouter(); - - const { data } = useGetPlayersXRankPlacementsQuery({ - variables: { switchAccountId }, - skip: router.isFallback, - }); +const PlayerPage = ({ placements }: Props) => { + // FIXME: spinner + if (!placements) return null; return ( - - - + <> + + + ); }; diff --git a/pages/u/[identifier].tsx b/pages/u/[identifier].tsx index bff0d0da3..635fb2dbc 100644 --- a/pages/u/[identifier].tsx +++ b/pages/u/[identifier].tsx @@ -61,6 +61,7 @@ const ProfilePage = (props: Props) => { const [loggedInUser] = useUser(); // same as router.isFallback + // FIXME: return null if (!user) return null; return ( diff --git a/prisma/queries/getPlayersTop500Placements.ts b/prisma/queries/getPlayersTop500Placements.ts new file mode 100644 index 000000000..97891152d --- /dev/null +++ b/prisma/queries/getPlayersTop500Placements.ts @@ -0,0 +1,19 @@ +import { PrismaClient } from "@prisma/client"; +import { Unwrap } from "lib/types"; + +export type GetPlayersTop500Placements = Unwrap< + ReturnType +>; + +export const getPlayersTop500Placements = async ({ + prisma, + switchAccountId, +}: { + prisma: PrismaClient; + switchAccountId: string; +}) => { + return prisma.xRankPlacement.findMany({ + where: { switchAccountId }, + orderBy: [{ month: "desc" }, { year: "desc" }], + }); +}; diff --git a/scenes/Player/index.tsx b/scenes/Player/index.tsx index 6c7ba96ae..d61d032a6 100644 --- a/scenes/Player/index.tsx +++ b/scenes/Player/index.tsx @@ -9,21 +9,18 @@ import { TableRow, } from "components/Table"; import WeaponImage from "components/WeaponImage"; -import { GetPlayersXRankPlacementsQuery } from "generated/graphql"; import { getRankingString } from "lib/strings"; import { useTranslation } from "lib/useMockT"; import { useMyTheme } from "lib/useMyTheme"; +import { GetPlayersTop500Placements } from "prisma/queries/getPlayersTop500Placements"; interface Props { - placements: NonNullable< - GetPlayersXRankPlacementsQuery["getPlayersXRankPlacements"] - >; + placements: GetPlayersTop500Placements; } const PlayerPage: React.FC = ({ placements }) => { const { t } = useTranslation(); const { gray } = useMyTheme(); - console.log({ placements }); return ( <>