import Head from "next/head"; import Menu from "../../../../components/Menu"; import redirect from "../../../../Modules/league"; import { Player, HistoricalPlayer } from "../../../../components/Player"; import { useRouter } from "next/router"; import { Box, FormLabel, Pagination, PaginationItem } from "@mui/material"; import { useContext } from "react"; import { TranslateContext } from "../../../../Modules/context"; import { sql } from "kysely"; import db from "#database"; export default function HistoricalView({ user, username, squad, transfers, league, latestMatchday, currentMatchday, // This is the matchday requested to be seen by the user time, // Stores the time this is if historical player data needs to be gotten money, value, leagueSettings, }) { const leagueType = leagueSettings.league; const leagueName = leagueSettings.leagueName; const router = useRouter(); const t = useContext(TranslateContext); return ( <> {t("{username}'s squad {matchday} from {leagueName}", { username, matchday: currentMatchday === 0 ? "" : t("on matchday {currentMatchday}", { currentMatchday }), leagueName, })}

{t("{username}'s squad {matchday} from {leagueName}", { username, matchday: currentMatchday === 0 ? "" : t("on matchday {currentMatchday}", { currentMatchday }), leagueName, })}

{t("Money left: {amount} M", { amount: money / 1000000 })}

{value && (

{t("Team value: {teamValue} M", { teamValue: value / 1000000 })}

)} {t("Select matchday")} { router.push( `/${league}/${user}/fantasy/${v === latestMatchday + 1 ? "" : v}`, ); }} renderItem={(item) => { if (item.page > latestMatchday) item.page = t("Latest"); return ; }} >

{t("Attackers")}

{squad .filter((e) => e.position === "att") .map((e) => { if (currentMatchday === 0) return ( ); return ( ); })}

{t("Midfielders")}

{squad .filter((e) => e.position === "mid") .map((e) => { if (currentMatchday === 0) return ( ); return ( ); })}

{t("Defenders")}

{squad .filter((e) => e.position === "def") .map((e) => { if (currentMatchday === 0) return ( ); return ( ); })}

{t("Goalkeeper")}

{squad .filter((e) => e.position === "gk") .map((e) => { if (currentMatchday === 0) return ( ); return ( ); })}

{t("Bench")}

{squad .filter((e) => e.position === "bench") .map((e) => { if (currentMatchday === 0) return ( ); return ( ); })}

{t("Transfers")}

{t("Buying")}

{transfers .filter((e) => e.buyer == user) .map((e) => { if (currentMatchday === 0) return (

{t("Buying for {amount} M", { amount: e.value / 1000000, })}

); return (

{t("Bought for {amount} M", { amount: e.value / 1000000 })}

); })}

{t("Selling")}

{transfers .filter((e) => e.seller == user) .map((e) => { if (currentMatchday === 0) return (

{t("Selling for {amount} M", { amount: e.value / 1000000, })}

); return (

{t("Sold for {amount} M", { amount: e.value / 1000000 })}

); })}
); } export async function getServerSideProps(ctx) { const user = ctx.params.user; const league = ctx.params.league; const [squad, transfers, username, latestMatchday, money] = await Promise.all( [ // Gets the latest squad of the user sql`SELECT * FROM squad WHERE leagueID=${league} AND user=${user}` .execute(db) .then((e) => e.rows), // Gets all transfers at the moment from the user sql`SELECT * FROM transfers WHERE leagueID=${league} AND (buyer=${user} OR seller=${user})` .execute(db) .then((e) => e.rows), // Gets the username of the user sql`SELECT username FROM users WHERE id=${user}` .execute(db) .then((e) => e.rows) .then((e) => (e.length > 0 ? e[0].username : "")), // Gets the latest matchday in that league sql`SELECT matchday FROM points WHERE leagueID=${league} and user=${user} ORDER BY matchday DESC` .execute(db) .then((e) => e.rows) .then((res) => (res.length > 0 ? res[0].matchday : 0)), sql`SELECT money FROM leagueUsers WHERE leagueID=${league} and user=${user}` .execute(db) .then((e) => e.rows) .then((res) => (res.length > 0 ? res[0].money : 0)), ], ); // Checks if the user exists if (username === "") { return { notFound: true, }; } return await redirect(ctx, { user, username, squad, transfers, league, latestMatchday, currentMatchday: 0, money, }); }