import db from "#/Modules/database"; import redirect from "#/Modules/league"; import Menu from "../../../../components/Menu"; import { leagueSettings } from "#/types/database"; import { GetServerSideProps } from "next"; import Head from "next/head"; import { TranslateContext } from "#/Modules/context"; import { useContext } from "react"; import { Alert, AlertTitle, FormLabel, Grid, Pagination, PaginationItem, } from "@mui/material"; import { useRouter } from "next/router"; import { useSession } from "next-auth/react"; import { checkUpdate } from "#/scripts/checkUpdate"; import { Game, predictions } from "#/components/Prediction"; import { get_predictions } from "#Modules/predictions"; export default function HistoricalView({ user, predictions, username, latestMatchday, leagueSettings, currentMatchday, }: { user: number; predictions: predictions[]; username: string; latestMatchday: number; leagueSettings: leagueSettings; currentMatchday: number; }) { const t = useContext(TranslateContext); const router = useRouter(); const { leagueName, leagueID, archived, predictionsEnabled } = leagueSettings; const session = useSession(); const current_userid = session.data?.user?.id; if (!predictionsEnabled) { return ( <> {t("Predictions for {leagueName}", { leagueName, })}

{t("Predictions for {leagueName}", { leagueName, })}

{t("Predictions are Disabled")}

{t("Predictions must be enabled in the league to use this. ")}

); } let title_text = t("{username}'s predictions {matchday} from {leagueName}", { username, matchday: currentMatchday === 0 ? "" : t("on matchday {currentMatchday}", { currentMatchday }), leagueName, }); if (currentMatchday === -1) { title_text = t("{username}'s future predictions for {leagueName}", { username, leagueName, }); } return ( <> {title_text}

{title_text}

{predictions.map((e) => ( ))} {t("Select matchday")} { if (v === latestMatchday + 1) { router.push(`/${leagueID}/${user}/predictions`); } else if (v > latestMatchday + 1) { router.push(`/${leagueID}/${user}/predictions/future`); } else { router.push(`/${leagueID}/${user}/predictions/${v}`); } }} renderItem={(item) => { let page: number | string | null = item.page; if (item.page === null || item.page == latestMatchday + 1) { page = t("Latest"); } else if (item.page === null || item.page > latestMatchday + 1) { page = t("Future"); } return ; }} > ); } export const getServerSideProps: GetServerSideProps = async (ctx) => { const user = parseInt(String(ctx.params?.user)); const league = parseInt(String(ctx.params?.league)); const [predictions, username, latestMatchday] = await Promise.all([ // Gets the latest predictions for the user get_predictions(user, league, checkUpdate), // Gets the username of the user db .selectFrom("users") .select("username") .where("id", "=", user) .executeTakeFirst() .then((e) => (e ? e.username : "")), // Gets the latest matchday in that league db .selectFrom("points") .select("matchday") .where("leagueID", "=", league) .where("user", "=", user) .where("time", "is not", null) .orderBy("matchday", "desc") .executeTakeFirst() .then((e) => (e ? e.matchday : 0)), ]); // Checks if the user exists if (username === "") { return { notFound: true, }; } return await redirect(ctx, { user, predictions, username, latestMatchday, currentMatchday: 0, }); };