From 4f6d35da42f4de6fb222f0dffedf64f48904f7fa Mon Sep 17 00:00:00 2001 From: Lowell Torola Date: Sun, 25 Jun 2023 11:21:34 -0400 Subject: [PATCH] more functions --- frontend2/src/App.tsx | 14 ++++++-- frontend2/src/__test__/utils.test.ts | 12 +++---- frontend2/src/utils/Warning.md | 2 +- frontend2/src/utils/api.ts | 49 +++++++++++++++++++++++----- 4 files changed, 58 insertions(+), 19 deletions(-) diff --git a/frontend2/src/App.tsx b/frontend2/src/App.tsx index d4cfa01c8..8defe3f92 100644 --- a/frontend2/src/App.tsx +++ b/frontend2/src/App.tsx @@ -11,6 +11,11 @@ async function getLoggedIn() { return res; } +async function searchTeams() { + const res = await ApiUnsafe.searchTeams("bc22", "team", false, 1); + return res; +} + const App: React.FC = () => { return (
@@ -36,8 +41,13 @@ const App: React.FC = () => { > Login! - - + + +
); }; diff --git a/frontend2/src/__test__/utils.test.ts b/frontend2/src/__test__/utils.test.ts index eee4359e4..4226924c7 100644 --- a/frontend2/src/__test__/utils.test.ts +++ b/frontend2/src/__test__/utils.test.ts @@ -41,13 +41,11 @@ test("API: Generate/Verify API Access Token (STABLE)", async () => { //---- USER ----// // TEST 1: Get current user's info (authed) -// test("API: Get current user's info (authed) (UNSTABLE)", async () => { -// await Auth.login("lmtorola_test", "pass"); -// Auth.setLoginHeader(); -// await ApiUnsafe.getUserProfile().then((res) => { -// expect(res.username).toEqual("lmtorola_test"); -// }); -// }); +test("API: Get current user's info (authed) (UNSTABLE)", async () => { + await Auth.login("lmtorola_test", "pass"); + Auth.setLoginHeader(); + expect(API.apiUserUMeRetrieve().then((res) => res.body.id)).toBeTruthy(); +}); // TEST 2: Get current user's info (unauthed) diff --git a/frontend2/src/utils/Warning.md b/frontend2/src/utils/Warning.md index 811e621b9..f261dfebb 100644 --- a/frontend2/src/utils/Warning.md +++ b/frontend2/src/utils/Warning.md @@ -1,2 +1,2 @@ **WARNING!** Everything in the types folder is automatically generated! Do not modify anything in that folder! -You can re-generate the Typescript types by running `call generate_types.bat` in the `frontend2` folder. +You can re-generate the Typescript types by running `call generate_types.bat` in the `frontend2` folder (.sh script coming soon). diff --git a/frontend2/src/utils/api.ts b/frontend2/src/utils/api.ts index 3b68adc75..f1c323684 100644 --- a/frontend2/src/utils/api.ts +++ b/frontend2/src/utils/api.ts @@ -6,6 +6,7 @@ import * as models from "./types/model/models"; // hacky, fall back to localhost for now const baseUrl = process.env.REACT_APP_BACKEND_URL || "http://localhost:8000"; const LEAGUE = 0; +const PAGE_SIZE = 10; // Safe vs. unsafe APIs... safe API has been tested, unsafe has NOT // TODO: how does url work? @index.tsx? @@ -50,7 +51,7 @@ export class ApiUnsafe { episodeId: string ): Promise => { return ( - (await $.get(`${URL}/api/episode/${episodeId}/map/`)) ?? + (await $.get(`${baseUrl}/api/episode/${episodeId}/map/`)) ?? ([] as models.ModelMap[]) ); }; @@ -127,11 +128,16 @@ export class ApiUnsafe { // return await $.get(`${baseUrl}/api/${LEAGUE}/team/${teamId}/history/`); // }; - // /** - // * Get the Mu history of the currently logged in user's team. - // */ - // public static getUserTeamMuHistory = async () => { - // }; + /** + * Get the Mu history of the currently logged in user's team. + */ + public static getTeamMuHistoryByTeam = async (teamId: number) => { + $.get(`${baseUrl}/api/${LEAGUE}/team/${teamId}/history/`).done( + (data, status) => { + console.log(data); + } + ); + }; /** * getTeamWinStatsByTeam @@ -149,6 +155,32 @@ export class ApiUnsafe { * getTeamRankingByTeam */ + //-- SEARCHING --// + + /** + * Search team, ordering the result by ranking. + * @param episodeId The current episode's ID. + * @param searchQuery The search query. + * @param requireActiveSubmission Whether to require an active submission. + * @param page The page number. + */ + public static searchTeams = async ( + episodeId: string, + searchQuery: string, + requireActiveSubmission: boolean, + page?: number + ): Promise => { + const apiURL = `${baseUrl}/api/team/${episodeId}/t`; + const encQuery = encodeURIComponent(searchQuery); + const teamUrl = + `${apiURL}/?ordering=-rating,name&search=${encQuery}&page=${page ?? 1}` + + (requireActiveSubmission ? `&has_active_submission=true` : ``); + // return (await $.get(teamUrl, (teamData) => { + // const pageLimit = Math.ceil(teamData.count / PAGE_SIZE); + // })); + return await $.get(teamUrl); + }; + //-- GENERAL INFO --// /** @@ -166,7 +198,7 @@ export class ApiUnsafe { * TODO: No idea how this is supposed to work! */ public static getUpdates = async () => { - $.get(`${URL}/api/league/${LEAGUE}/`, (data) => { + $.get(`${baseUrl}/api/league/${LEAGUE}/`, (data) => { for (let i = 0; i < data.updates.length; i++) { const d = new Date(data.updates[i].time); data.updates[i].dateObj = d; @@ -258,7 +290,7 @@ export class ApiUnsafe { page?: number ): Promise => { const res = await $.get( - `${URL}/api/compete/${episodeId}/submission/tournament/?page=${page}` + `${baseUrl}/api/compete/${episodeId}/submission/tournament/?page=${page}` ); return { count: parseInt(res.length ?? "0"), @@ -610,7 +642,6 @@ export class ApiUnsafe { export class Auth { /** * Clear the access and refresh tokens from the browser's cookies. - * UNSAFE!!! Needs to be tested. */ public static logout = () => { Cookies.set("access", "");