From eae19bb1b047b3568e7a9a624b50e80886e56331 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:47:50 +0300 Subject: feat/frontend: optimizing imports, file extensions (#230) Co-authored-by: FifthWit --- frontend/src/api/Api.ts | 56 ++++++++++++++++++++++ frontend/src/api/Api.tsx | 56 ---------------------- frontend/src/api/Auth.ts | 14 ++++++ frontend/src/api/Auth.tsx | 14 ------ frontend/src/api/Games.ts | 31 ++++++++++++ frontend/src/api/Games.tsx | 31 ------------ frontend/src/api/Maps.ts | 106 ++++++++++++++++++++++++++++++++++++++++++ frontend/src/api/Maps.tsx | 106 ------------------------------------------ frontend/src/api/Mod.ts | 58 +++++++++++++++++++++++ frontend/src/api/Mod.tsx | 58 ----------------------- frontend/src/api/Rankings.ts | 13 ++++++ frontend/src/api/Rankings.tsx | 13 ------ frontend/src/api/User.ts | 25 ++++++++++ frontend/src/api/User.tsx | 25 ---------- 14 files changed, 303 insertions(+), 303 deletions(-) create mode 100644 frontend/src/api/Api.ts delete mode 100644 frontend/src/api/Api.tsx create mode 100644 frontend/src/api/Auth.ts delete mode 100644 frontend/src/api/Auth.tsx create mode 100644 frontend/src/api/Games.ts delete mode 100644 frontend/src/api/Games.tsx create mode 100644 frontend/src/api/Maps.ts delete mode 100644 frontend/src/api/Maps.tsx create mode 100644 frontend/src/api/Mod.ts delete mode 100644 frontend/src/api/Mod.tsx create mode 100644 frontend/src/api/Rankings.ts delete mode 100644 frontend/src/api/Rankings.tsx create mode 100644 frontend/src/api/User.ts delete mode 100644 frontend/src/api/User.tsx (limited to 'frontend/src/api') diff --git a/frontend/src/api/Api.ts b/frontend/src/api/Api.ts new file mode 100644 index 0000000..2e55ab4 --- /dev/null +++ b/frontend/src/api/Api.ts @@ -0,0 +1,56 @@ +import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '@customTypes/Content'; +import { delete_token, get_token } from '@api/Auth'; +import { get_user, get_profile, post_profile } from '@api/User'; +import { get_games, get_chapters, get_games_chapters, get_game_maps, get_search } from '@api/Games'; +import { get_official_rankings, get_unofficial_rankings } from '@api/Rankings'; +import { get_map_summary, get_map_leaderboard, get_map_discussions, get_map_discussion, post_map_discussion, post_map_discussion_comment, delete_map_discussion, post_record, delete_map_record } from '@api/Maps'; +import { delete_map_summary, post_map_summary, put_map_image, put_map_summary } from '@api/Mod'; +import { UploadRunContent } from '@customTypes/Content'; + +// add new api call function entries here +// example usage: API.get_games(); +export const API = { + // Auth + get_token: () => get_token(), + + delete_token: () => delete_token(), + // User + get_user: (user_id: string) => get_user(user_id), + get_profile: (token: string) => get_profile(token), + post_profile: (token: string) => post_profile(token), + // Games + get_games: () => get_games(), + get_chapters: (chapter_id: string) => get_chapters(chapter_id), + get_games_chapters: (game_id: string) => get_games_chapters(game_id), + get_game_maps: (game_id: string) => get_game_maps(game_id), + get_search: (q: string) => get_search(q), + // Rankings + get_official_rankings: () => get_official_rankings(), + get_unofficial_rankings: () => get_unofficial_rankings(), + // Maps + get_map_summary: (map_id: string) => get_map_summary(map_id), + get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id), + get_map_discussions: (map_id: string) => get_map_discussions(map_id), + get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id), + + post_map_discussion: (token: string, map_id: string, content: MapDiscussionContent) => post_map_discussion(token, map_id, content), + post_map_discussion_comment: (token: string, map_id: string, discussion_id: number, comment: string) => post_map_discussion_comment(token, map_id, discussion_id, comment), + post_record: (token: string, run: UploadRunContent) => post_record(token, run), + + delete_map_discussion: (token: string, map_id: string, discussion_id: number) => delete_map_discussion(token, map_id, discussion_id), + + delete_map_record: (token: string, map_id: number, record_id: number) => delete_map_record(token, map_id, record_id), + // Mod + post_map_summary: (token: string, map_id: string, content: ModMenuContent) => post_map_summary(token, map_id, content), + + put_map_image: (token: string, map_id: string, image: string) => put_map_image(token, map_id, image), + put_map_summary: (token: string, map_id: string, content: ModMenuContent) => put_map_summary(token, map_id, content), + + delete_map_summary: (token: string, map_id: string, route_id: number) => delete_map_summary(token, map_id, route_id), +}; + +const BASE_API_URL: string = "/api/v1/" + +export function url(path: string): string { + return BASE_API_URL + path; +}; diff --git a/frontend/src/api/Api.tsx b/frontend/src/api/Api.tsx deleted file mode 100644 index 053e920..0000000 --- a/frontend/src/api/Api.tsx +++ /dev/null @@ -1,56 +0,0 @@ -import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; -import { delete_token, get_token } from './Auth'; -import { get_user, get_profile, post_profile } from './User'; -import { get_games, get_chapters, get_games_chapters, get_game_maps, get_search } from './Games'; -import { get_official_rankings, get_unofficial_rankings } from './Rankings'; -import { get_map_summary, get_map_leaderboard, get_map_discussions, get_map_discussion, post_map_discussion, post_map_discussion_comment, delete_map_discussion, post_record, delete_map_record } from './Maps'; -import { delete_map_summary, post_map_summary, put_map_image, put_map_summary } from './Mod'; -import { UploadRunContent } from '../types/Content'; - -// add new api call function entries here -// example usage: API.get_games(); -export const API = { - // Auth - get_token: () => get_token(), - - delete_token: () => delete_token(), - // User - get_user: (user_id: string) => get_user(user_id), - get_profile: (token: string) => get_profile(token), - post_profile: (token: string) => post_profile(token), - // Games - get_games: () => get_games(), - get_chapters: (chapter_id: string) => get_chapters(chapter_id), - get_games_chapters: (game_id: string) => get_games_chapters(game_id), - get_game_maps: (game_id: string) => get_game_maps(game_id), - get_search: (q: string) => get_search(q), - // Rankings - get_official_rankings: () => get_official_rankings(), - get_unofficial_rankings: () => get_unofficial_rankings(), - // Maps - get_map_summary: (map_id: string) => get_map_summary(map_id), - get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id), - get_map_discussions: (map_id: string) => get_map_discussions(map_id), - get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id), - - post_map_discussion: (token: string, map_id: string, content: MapDiscussionContent) => post_map_discussion(token, map_id, content), - post_map_discussion_comment: (token: string, map_id: string, discussion_id: number, comment: string) => post_map_discussion_comment(token, map_id, discussion_id, comment), - post_record: (token: string, run: UploadRunContent) => post_record(token, run), - - delete_map_discussion: (token: string, map_id: string, discussion_id: number) => delete_map_discussion(token, map_id, discussion_id), - - delete_map_record: (token: string, map_id: number, record_id: number) => delete_map_record(token, map_id, record_id), - // Mod - post_map_summary: (token: string, map_id: string, content: ModMenuContent) => post_map_summary(token, map_id, content), - - put_map_image: (token: string, map_id: string, image: string) => put_map_image(token, map_id, image), - put_map_summary: (token: string, map_id: string, content: ModMenuContent) => put_map_summary(token, map_id, content), - - delete_map_summary: (token: string, map_id: string, route_id: number) => delete_map_summary(token, map_id, route_id), -}; - -const BASE_API_URL: string = "/api/v1/" - -export function url(path: string): string { - return BASE_API_URL + path; -}; diff --git a/frontend/src/api/Auth.ts b/frontend/src/api/Auth.ts new file mode 100644 index 0000000..875c7e5 --- /dev/null +++ b/frontend/src/api/Auth.ts @@ -0,0 +1,14 @@ +import axios from "axios"; +import { url } from "@api/Api"; + +export const get_token = async (): Promise => { + const response = await axios.get(url(`token`)) + if (!response.data.success) { + return undefined; + } + return response.data.data.token; +}; + +export const delete_token = async () => { + await axios.delete(url("token")); +}; diff --git a/frontend/src/api/Auth.tsx b/frontend/src/api/Auth.tsx deleted file mode 100644 index 09269e6..0000000 --- a/frontend/src/api/Auth.tsx +++ /dev/null @@ -1,14 +0,0 @@ -import axios from "axios"; -import { url } from "./Api"; - -export const get_token = async (): Promise => { - const response = await axios.get(url(`token`)) - if (!response.data.success) { - return undefined; - } - return response.data.data.token; -}; - -export const delete_token = async () => { - await axios.delete(url("token")); -}; diff --git a/frontend/src/api/Games.ts b/frontend/src/api/Games.ts new file mode 100644 index 0000000..72bb4b3 --- /dev/null +++ b/frontend/src/api/Games.ts @@ -0,0 +1,31 @@ +import axios from "axios"; +import { url } from "@api/Api"; +import { GameChapter, GamesChapters } from "@customTypes/Chapters"; +import { Game } from "@customTypes/Game"; +import { Map } from "@customTypes/Map"; +import { Search } from "@customTypes/Search"; + +export const get_games = async (): Promise => { + const response = await axios.get(url(`games`)) + return response.data.data; +}; + +export const get_chapters = async (chapter_id: string): Promise => { + const response = await axios.get(url(`chapters/${chapter_id}`)); + return response.data.data; +} + +export const get_games_chapters = async (game_id: string): Promise => { + const response = await axios.get(url(`games/${game_id}`)); + return response.data.data; +}; + +export const get_game_maps = async (game_id: string): Promise => { + const response = await axios.get(url(`games/${game_id}/maps`)) + return response.data.data.maps; +}; + +export const get_search = async (q: string): Promise => { + const response = await axios.get(url(`search?q=${q}`)) + return response.data.data; +}; diff --git a/frontend/src/api/Games.tsx b/frontend/src/api/Games.tsx deleted file mode 100644 index 84b5f74..0000000 --- a/frontend/src/api/Games.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import axios from "axios"; -import { url } from "./Api"; -import { GameChapter, GamesChapters } from "../types/Chapters"; -import { Game } from "../types/Game"; -import { Map } from "../types/Map"; -import { Search } from "../types/Search"; - -export const get_games = async (): Promise => { - const response = await axios.get(url(`games`)) - return response.data.data; -}; - -export const get_chapters = async (chapter_id: string): Promise => { - const response = await axios.get(url(`chapters/${chapter_id}`)); - return response.data.data; -} - -export const get_games_chapters = async (game_id: string): Promise => { - const response = await axios.get(url(`games/${game_id}`)); - return response.data.data; -}; - -export const get_game_maps = async (game_id: string): Promise => { - const response = await axios.get(url(`games/${game_id}/maps`)) - return response.data.data.maps; -}; - -export const get_search = async (q: string): Promise => { - const response = await axios.get(url(`search?q=${q}`)) - return response.data.data; -}; diff --git a/frontend/src/api/Maps.ts b/frontend/src/api/Maps.ts new file mode 100644 index 0000000..89657b5 --- /dev/null +++ b/frontend/src/api/Maps.ts @@ -0,0 +1,106 @@ +import axios from "axios"; +import { url } from "@api/Api"; +import { MapDiscussionContent, UploadRunContent } from "@customTypes/Content"; +import { MapSummary, MapLeaderboard, MapDiscussions, MapDiscussion } from "@customTypes/Map"; + +export const get_map_summary = async (map_id: string): Promise => { + const response = await axios.get(url(`maps/${map_id}/summary`)) + return response.data.data; +}; + +export const get_map_leaderboard = async (map_id: string): Promise => { + const response = await axios.get(url(`maps/${map_id}/leaderboards`)); + if (!response.data.success) { + return undefined; + } + const data = response.data.data; + // map the kind of leaderboard + data.records = data.records.map((record: any) => { + if (record.host && record.partner) { + return { ...record, kind: 'multiplayer' }; + } else { + return { ...record, kind: 'singleplayer' }; + } + }); + return data; +}; + +export const get_map_discussions = async (map_id: string): Promise => { + const response = await axios.get(url(`maps/${map_id}/discussions`)); + if (!response.data.data.discussions) { + return undefined; + } + return response.data.data; +}; + +export const get_map_discussion = async (map_id: string, discussion_id: number): Promise => { + const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`)); + if (!response.data.data.discussion) { + return undefined; + } + return response.data.data; +}; + +export const post_map_discussion = async (token: string, map_id: string, content: MapDiscussionContent): Promise => { + const response = await axios.post(url(`maps/${map_id}/discussions`), { + "title": content.title, + "content": content.content, + }, { + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; + +export const post_map_discussion_comment = async (token: string, map_id: string, discussion_id: number, comment: string): Promise => { + const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), { + "comment": comment, + }, { + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; + +export const delete_map_discussion = async (token: string, map_id: string, discussion_id: number): Promise => { + const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`), { + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; + +export const post_record = async (token: string, run: UploadRunContent): Promise<[boolean, string]> => { + if (run.partner_demo) { + const response = await axios.postForm(url(`maps/${run.map_id}/record`), { + "host_demo": run.host_demo, + "partner_demo": run.partner_demo, + }, { + headers: { + "Authorization": token, + } + }); + return [ response.data.success, response.data.message ]; + } else { + const response = await axios.postForm(url(`maps/${run.map_id}/record`), { + "host_demo": run.host_demo, + }, { + headers: { + "Authorization": token, + } + }); + return [ response.data.success, response.data.message ]; + } +} + +export const delete_map_record = async (token: string, map_id: number, record_id: number): Promise => { + const response = await axios.delete(url(`maps/${map_id}/record/${record_id}`), { + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; diff --git a/frontend/src/api/Maps.tsx b/frontend/src/api/Maps.tsx deleted file mode 100644 index fc50293..0000000 --- a/frontend/src/api/Maps.tsx +++ /dev/null @@ -1,106 +0,0 @@ -import axios from "axios"; -import { url } from "./Api"; -import { MapDiscussionContent, UploadRunContent } from "../types/Content"; -import { MapSummary, MapLeaderboard, MapDiscussions, MapDiscussion } from "../types/Map"; - -export const get_map_summary = async (map_id: string): Promise => { - const response = await axios.get(url(`maps/${map_id}/summary`)) - return response.data.data; -}; - -export const get_map_leaderboard = async (map_id: string): Promise => { - const response = await axios.get(url(`maps/${map_id}/leaderboards`)); - if (!response.data.success) { - return undefined; - } - const data = response.data.data; - // map the kind of leaderboard - data.records = data.records.map((record: any) => { - if (record.host && record.partner) { - return { ...record, kind: 'multiplayer' }; - } else { - return { ...record, kind: 'singleplayer' }; - } - }); - return data; -}; - -export const get_map_discussions = async (map_id: string): Promise => { - const response = await axios.get(url(`maps/${map_id}/discussions`)); - if (!response.data.data.discussions) { - return undefined; - } - return response.data.data; -}; - -export const get_map_discussion = async (map_id: string, discussion_id: number): Promise => { - const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`)); - if (!response.data.data.discussion) { - return undefined; - } - return response.data.data; -}; - -export const post_map_discussion = async (token: string, map_id: string, content: MapDiscussionContent): Promise => { - const response = await axios.post(url(`maps/${map_id}/discussions`), { - "title": content.title, - "content": content.content, - }, { - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; - -export const post_map_discussion_comment = async (token: string, map_id: string, discussion_id: number, comment: string): Promise => { - const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), { - "comment": comment, - }, { - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; - -export const delete_map_discussion = async (token: string, map_id: string, discussion_id: number): Promise => { - const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`), { - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; - -export const post_record = async (token: string, run: UploadRunContent): Promise<[boolean, string]> => { - if (run.partner_demo) { - const response = await axios.postForm(url(`maps/${run.map_id}/record`), { - "host_demo": run.host_demo, - "partner_demo": run.partner_demo, - }, { - headers: { - "Authorization": token, - } - }); - return [ response.data.success, response.data.message ]; - } else { - const response = await axios.postForm(url(`maps/${run.map_id}/record`), { - "host_demo": run.host_demo, - }, { - headers: { - "Authorization": token, - } - }); - return [ response.data.success, response.data.message ]; - } -} - -export const delete_map_record = async (token: string, map_id: number, record_id: number): Promise => { - const response = await axios.delete(url(`maps/${map_id}/record/${record_id}`), { - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; diff --git a/frontend/src/api/Mod.ts b/frontend/src/api/Mod.ts new file mode 100644 index 0000000..1511f8b --- /dev/null +++ b/frontend/src/api/Mod.ts @@ -0,0 +1,58 @@ +import axios from "axios"; +import { url } from "@api/Api"; +import { ModMenuContent } from "@customTypes/Content"; + +export const put_map_image = async (token: string, map_id: string, image: string): Promise => { + const response = await axios.put(url(`maps/${map_id}/image`), { + "image": image, + }, { + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; + +export const post_map_summary = async (token: string, map_id: string, content: ModMenuContent): Promise => { + const response = await axios.post(url(`maps/${map_id}/summary`), { + "category_id": content.category_id, + "user_name": content.name, + "score_count": content.score, + "record_date": content.date, + "showcase": content.showcase, + "description": content.description, + }, { + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; + +export const put_map_summary = async (token: string, map_id: string, content: ModMenuContent): Promise => { + const response = await axios.put(url(`maps/${map_id}/summary`), { + "route_id": content.id, + "user_name": content.name, + "score_count": content.score, + "record_date": content.date, + "showcase": content.showcase, + "description": content.description, + }, { + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; + +export const delete_map_summary = async (token: string, map_id: string, route_id: number): Promise => { + const response = await axios.delete(url(`maps/${map_id}/summary`), { + data: { + "route_id": route_id, + }, + headers: { + "Authorization": token, + } + }); + return response.data.success; +}; diff --git a/frontend/src/api/Mod.tsx b/frontend/src/api/Mod.tsx deleted file mode 100644 index 9091379..0000000 --- a/frontend/src/api/Mod.tsx +++ /dev/null @@ -1,58 +0,0 @@ -import axios from "axios"; -import { url } from "./Api"; -import { ModMenuContent } from "../types/Content"; - -export const put_map_image = async (token: string, map_id: string, image: string): Promise => { - const response = await axios.put(url(`maps/${map_id}/image`), { - "image": image, - }, { - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; - -export const post_map_summary = async (token: string, map_id: string, content: ModMenuContent): Promise => { - const response = await axios.post(url(`maps/${map_id}/summary`), { - "category_id": content.category_id, - "user_name": content.name, - "score_count": content.score, - "record_date": content.date, - "showcase": content.showcase, - "description": content.description, - }, { - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; - -export const put_map_summary = async (token: string, map_id: string, content: ModMenuContent): Promise => { - const response = await axios.put(url(`maps/${map_id}/summary`), { - "route_id": content.id, - "user_name": content.name, - "score_count": content.score, - "record_date": content.date, - "showcase": content.showcase, - "description": content.description, - }, { - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; - -export const delete_map_summary = async (token: string, map_id: string, route_id: number): Promise => { - const response = await axios.delete(url(`maps/${map_id}/summary`), { - data: { - "route_id": route_id, - }, - headers: { - "Authorization": token, - } - }); - return response.data.success; -}; diff --git a/frontend/src/api/Rankings.ts b/frontend/src/api/Rankings.ts new file mode 100644 index 0000000..b8d9bec --- /dev/null +++ b/frontend/src/api/Rankings.ts @@ -0,0 +1,13 @@ +import axios from "axios"; +import { url } from "@api/Api"; +import { Ranking, SteamRanking } from "@customTypes/Ranking"; + +export const get_official_rankings = async (): Promise => { + const response = await axios.get(url(`rankings/lphub`)); + return response.data.data; +}; + +export const get_unofficial_rankings = async (): Promise => { + const response = await axios.get(url(`rankings/steam`)); + return response.data.data; +}; diff --git a/frontend/src/api/Rankings.tsx b/frontend/src/api/Rankings.tsx deleted file mode 100644 index 384f826..0000000 --- a/frontend/src/api/Rankings.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import axios from "axios"; -import { url } from "./Api"; -import { Ranking, SteamRanking } from "../types/Ranking"; - -export const get_official_rankings = async (): Promise => { - const response = await axios.get(url(`rankings/lphub`)); - return response.data.data; -}; - -export const get_unofficial_rankings = async (): Promise => { - const response = await axios.get(url(`rankings/steam`)); - return response.data.data; -}; diff --git a/frontend/src/api/User.ts b/frontend/src/api/User.ts new file mode 100644 index 0000000..88da0f2 --- /dev/null +++ b/frontend/src/api/User.ts @@ -0,0 +1,25 @@ +import axios from "axios"; +import { url } from "@api/Api"; +import { UserProfile } from "@customTypes/Profile"; + +export const get_user = async (user_id: string): Promise => { + const response = await axios.get(url(`users/${user_id}`)); + return response.data.data; +}; + +export const get_profile = async (token: string): Promise => { + const response = await axios.get(url(`profile`), { + headers: { + "Authorization": token, + } + }); + return response.data.data; +}; + +export const post_profile = async (token: string) => { + const _ = await axios.post(url(`profile`), {}, { + headers: { + "Authorization": token, + } + }); +}; diff --git a/frontend/src/api/User.tsx b/frontend/src/api/User.tsx deleted file mode 100644 index c4d1944..0000000 --- a/frontend/src/api/User.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import axios from "axios"; -import { url } from "./Api"; -import { UserProfile } from "../types/Profile"; - -export const get_user = async (user_id: string): Promise => { - const response = await axios.get(url(`users/${user_id}`)); - return response.data.data; -}; - -export const get_profile = async (token: string): Promise => { - const response = await axios.get(url(`profile`), { - headers: { - "Authorization": token, - } - }); - return response.data.data; -}; - -export const post_profile = async (token: string) => { - const _ = await axios.post(url(`profile`), {}, { - headers: { - "Authorization": token, - } - }); -}; -- cgit v1.2.3