Skip to content

Commit

Permalink
more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lowtorola committed Jun 25, 2023
1 parent 64ff80d commit 4f6d35d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
14 changes: 12 additions & 2 deletions frontend2/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="App">
Expand All @@ -36,8 +41,13 @@ const App: React.FC = () => {
>
Login!
</button>
<button onClick={() => console.log(getId())}>ID</button>
<button onClick={() => console.log(getLoggedIn())}>Logged In?</button>
<button onClick={async () => console.log(getId())}>ID</button>
<button onClick={async () => console.log(getLoggedIn())}>
Logged In?
</button>
<button onClick={async () => console.log(searchTeams())}>
Search Teams
</button>
</div>
);
};
Expand Down
12 changes: 5 additions & 7 deletions frontend2/src/__test__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion frontend2/src/utils/Warning.md
Original file line number Diff line number Diff line change
@@ -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).
49 changes: 40 additions & 9 deletions frontend2/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -50,7 +51,7 @@ export class ApiUnsafe {
episodeId: string
): Promise<models.ModelMap[]> => {
return (
(await $.get(`${URL}/api/episode/${episodeId}/map/`)) ??
(await $.get(`${baseUrl}/api/episode/${episodeId}/map/`)) ??
([] as models.ModelMap[])
);
};
Expand Down Expand Up @@ -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
Expand All @@ -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<models.PaginatedTeamPublicList> => {
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 --//

/**
Expand All @@ -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;
Expand Down Expand Up @@ -258,7 +290,7 @@ export class ApiUnsafe {
page?: number
): Promise<models.PaginatedSubmissionList> => {
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"),
Expand Down Expand Up @@ -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", "");
Expand Down

0 comments on commit 4f6d35d

Please sign in to comment.