{announcements.map((e: announcements, idx) => (
{e.title}
{!!admin && (
{
deleteAnouncement(idx);
}}
sx={{
position: "absolute",
right: 8,
top: 8,
color: (theme) => theme.palette.grey[500],
}}
>
close
)}
{e.description}
))}
{!!admin && (
<>
{
// Used to change the title
setAnouncementTitle(val.target.value);
}}
value={announcementTitle}
/>
{
// Used to change the description
setAnouncementDescription(val.target.value);
}}
value={announcementDescription}
/>
{t("Announcement Priority: ")}
>
)}
{t("Invite Links")}
{t("Tap to copy a link")}
{invites.map((val) => (
{
setInvites(invites.filter((e) => e != val));
}}
/>
))}
{/* Used to create a new invite link */}
{
// Used to change the invite link
setnewInvite(val.target.value);
}}
value={newInvite}
/>
>
);
}
type HistoricalDataTypes = "totalPoints" | "fantasyPoints" | "predictionPoints";
interface historialData {
[Key: string]: {
totalPoints: number;
fantasyPoints: number;
predictionPoints: number;
}[];
}
interface standingsData {
user: number;
points: number;
fantasyPoints: number;
predictionPoints: number;
}
// Gets the users session
export const getServerSideProps: GetServerSideProps = async (
ctx: GetServerSidePropsContext,
) => {
// Gets the user id
const session = await getServerSession(ctx.req, ctx.res, authOptions);
const user = session ? session.user.id : -1;
const leagueID = parseInt(String(ctx.params?.league));
// Gets the leaderboard for the league
const standings: standingsData[] = await db
.selectFrom("leagueUsers")
.select(["user", "points", "fantasyPoints", "predictionPoints"])
.where("leagueID", "=", leagueID)
.execute();
// Gets the historical amount of points for every matchday in the league
const historicalPoints = new Promise(async (resolve) => {
const results = await db
.selectFrom("points")
.where("leagueID", "=", leagueID)
.orderBy("matchday", "asc")
.selectAll()
.execute();
// Reformats the result into a dictionary that has an entry for each user and each entry for that user is an array of all the points the user has earned in chronological order.
const points: historialData = {};
results.forEach((element) => {
if (points[element.user]) {
points[String(element.user)].push({
totalPoints: element.points,
fantasyPoints: element.fantasyPoints,
predictionPoints: element.predictionPoints,
});
} else {
points[String(element.user)] = [
{
totalPoints: element.points,
fantasyPoints: element.fantasyPoints,
predictionPoints: element.predictionPoints,
},
];
}
});
resolve(points);
});
const inviteLinks = db
.selectFrom("invite")
.select("inviteID")
.where("leagueID", "=", leagueID)
.execute()
.then((e) => e.map((val) => val.inviteID));
// Checks if the user is an admin
const data = db
.selectFrom("leagueUsers")
.where("leagueID", "=", leagueID)
.where("user", "=", user)
.select(["admin", "tutorial"])
.executeTakeFirst()
.then((e) => {
return {
admin: e ? e.admin : false,
tutorial: e ? e.tutorial : false,
};
});
const announcements = db
.selectFrom("announcements")
.selectAll()
.where("leagueID", "=", leagueID)
.execute();
const adminUsers: Promise = db
.selectFrom("leagueUsers")
.select(["user", "admin"])
.where("leagueID", "=", leagueID)
.execute();
let startFilter: HistoricalDataTypes = "totalPoints";
const standings_user = standings.filter((val) => val.user === user);
if (standings_user.length > 0) {
if (
standings_user[0].fantasyPoints === 0 &&
standings_user[0].predictionPoints !== 0
) {
startFilter = "predictionPoints";
}
if (
standings_user[0].fantasyPoints !== 0 &&
standings_user[0].predictionPoints === 0
) {
startFilter = "fantasyPoints";
}
}
return redirect(ctx, {
OGannouncement: await announcements,
admin: (await data).admin,
tutorial: (await data).tutorial,
standings: standings,
historicalPoints: await historicalPoints,
inviteLinks: await inviteLinks,
adminUsers: await adminUsers,
host: ctx.req.headers.host,
startFilter,
});
};