Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request update for league when the prediction pages are opened #732

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/[league]/[user]/predictions/[matchday].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
number,
] = await Promise.all([
// Gets the latest predictions for the user
get_predictions(connection, user, league, currentMatchday),
get_predictions(connection, user, league, undefined, currentMatchday),
// Gets the username of the user
connection
.query("SELECT username FROM users WHERE id=?", [user])
Expand Down
3 changes: 2 additions & 1 deletion pages/[league]/[user]/predictions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FormLabel, Grid, Pagination, PaginationItem } from "@mui/material";
import { useRouter } from "next/router";
import { Game, get_predictions, predictions } from "../../predictions";
import { useSession } from "next-auth/react";
import { checkUpdate } from "#/scripts/checkUpdate";

export default function HistoricalView({
user,
Expand Down Expand Up @@ -94,7 +95,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
number,
] = await Promise.all([
// Gets the latest predictions for the user
get_predictions(connection, user, league),
get_predictions(connection, user, league, checkUpdate),
// Gets the username of the user
connection
.query("SELECT username FROM users WHERE id=?", [user])
Expand Down
14 changes: 14 additions & 0 deletions pages/[league]/predictions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import redirect from "#/Modules/league";
import { GetServerSidePropsContext } from "next";
import { getSession } from "next-auth/react";
import { Alert, AlertTitle, Grid, TextField } from "@mui/material";
import { checkUpdate } from "#/scripts/checkUpdate";
export interface predictions {
home_team: string;
home_team_name: string | undefined;
Expand Down Expand Up @@ -212,15 +213,27 @@ export default function Home({
</>
);
}
/**
* Retrieves predictions for a given league and matchday.
*
* @param {connection} connection - The database connection.
* @param {number} user - The user ID.
* @param {number} league - The league ID.
* @param {(league: string) => Promise<void>} [checkUpdate] - The optional checkUpdate function located at #/scripts/checkUpdate. Doesn't like being imported in here.
* @param {number} [matchday] - The optional matchday number.
* @return {Promise<predictions[]>} An array of all the predictions that the user had
*/
export const get_predictions = async (
connection: connection,
user: number,
league: number,
checkUpdate?: (league: string) => Promise<void>,
matchday?: number,
): Promise<predictions[]> => {
const leagueType: string = await connection
.query("SELECT league FROM leagueSettings WHERE leagueID=?", [league])
.then((e) => (e.length > 0 ? e[0].league : ""));
if (checkUpdate) checkUpdate(leagueType);
if (matchday) {
const time = await connection
.query("SELECT time FROM points WHERE matchday=? AND leagueID=?", [
Expand Down Expand Up @@ -294,6 +307,7 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
connection,
user_id as number,
parseInt(String(ctx.query.league)),
checkUpdate,
);
connection.end();
return await redirect(ctx, { games });
Expand Down
6 changes: 6 additions & 0 deletions scripts/checkUpdate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import connect from "../Modules/database";
import { data } from "#types/database";

/**
* Checks if matchdays are currently happening and if it is a matchday checks if the update time has passed to request a new update.
*
* @param {string} league - The league to check for updates.
* @return {Promise<void>} - A Promise that resolves when the update check is complete.
*/
export async function checkUpdate(league: string) {
const connection = await connect();
// Checks if matchdays are currently happening and if it is a matchday checks if the update time has passed
Expand Down