From a7c282ca348c1e8e60559e5c064caee28ba11eec Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Wed, 9 Oct 2024 16:34:12 +0300 Subject: refactor: so much shit --- frontend/src/api/Api.tsx | 205 +++++++---------------------------------------- 1 file changed, 30 insertions(+), 175 deletions(-) (limited to 'frontend/src/api/Api.tsx') diff --git a/frontend/src/api/Api.tsx b/frontend/src/api/Api.tsx index 30c0ad6..6731cb3 100644 --- a/frontend/src/api/Api.tsx +++ b/frontend/src/api/Api.tsx @@ -1,197 +1,52 @@ -import axios from 'axios'; - -import { GameChapter, GamesChapters } from '../types/Chapters'; -import { Game } from '../types/Game'; -import { Map, MapDiscussion, MapDiscussions, MapLeaderboard, MapSummary } from '../types/Map'; import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; -import { Search } from '../types/Search'; -import { UserProfile } from '../types/Profile'; -import { Ranking } from '../types/Ranking'; +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 } from './Maps'; +import { delete_map_summary, post_map_summary, put_map_image, put_map_summary } from './Mod'; // add new api call function entries here // example usage: API.get_games(); export const API = { - user_logout: () => user_logout(), - + // 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_rankings: () => get_rankings(), 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_summary: (map_id: string, content: ModMenuContent) => post_map_summary(map_id, content), - post_map_discussion: (map_id: string, content: MapDiscussionContent) => post_map_discussion(map_id, content), - post_map_discussion_comment: (map_id: string, discussion_id: number, content: MapDiscussionCommentContent) => post_map_discussion_comment(map_id, discussion_id, content), - - put_map_image: (map_id: string, image: string) => put_map_image(map_id, image), - put_map_summary: (map_id: string, content: ModMenuContent) => put_map_summary(map_id, content), + 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), - delete_map_summary: (map_id: string, route_id: number) => delete_map_summary(map_id, route_id), - delete_map_discussion: (map_id: string, discussion_id: number) => delete_map_discussion(map_id, discussion_id), + delete_map_discussion: (token: string, map_id: string, discussion_id: number) => delete_map_discussion(token, map_id, discussion_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 = "https://lp.ardapektezol.com/api/v1/" +const BASE_API_URL: string = "/api/v1/" -function url(path: string): string { +export function url(path: string): string { return BASE_API_URL + path; -} - -// USER - -const user_logout = async () => { - await axios.delete(url("token")); -}; - -const get_user = async (user_id: string): Promise => { - const response = await axios.get(url(`users/${user_id}`)) - return response.data.data; -}; - - -// GAMES - -const get_games = async (): Promise => { - const response = await axios.get(url("games")) - return response.data.data; -}; - -const get_chapters = async (chapter_id: string): Promise => { - const response = await axios.get(url(`chapters/${chapter_id}`)); - return response.data.data; -} - -const get_games_chapters = async (game_id: string): Promise => { - const response = await axios.get(url(`games/${game_id}`)); - return response.data.data; -}; - -const get_game_maps = async (game_id: string): Promise => { - const response = await axios.get(url(`games/${game_id}/maps`)) - return response.data.data.maps; -}; - - -// RANKINGS -const get_rankings = async (): Promise => { - const response = await axios.get(url(`rankings`)); - return response.data.data; -} - -// SEARCH - -const get_search = async (q: string): Promise => { - const response = await axios.get(url(`search?q=${q}`)) - return response.data.data; -}; - -// MAP SUMMARY - -const put_map_image = async (map_id: string, image: string): Promise => { - const response = await axios.put(url(`maps/${map_id}/image`), { - "image": image, - }); - return response.data.success; -}; - -const get_map_summary = async (map_id: string): Promise => { - const response = await axios.get(url(`maps/${map_id}/summary`)) - return response.data.data; -}; - -const post_map_summary = async (map_id: string, content: ModMenuContent): Promise => { - const response = await axios.post(url(`maps/${map_id}/summary`), { - "user_name": content.name, - "score_count": content.score, - "record_date": content.date, - "showcase": content.showcase, - "description": content.description, - }); - return response.data.success; -}; - -const put_map_summary = async (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, - }); - return response.data.success; -}; - -const delete_map_summary = async (map_id: string, route_id: number): Promise => { - const response = await axios.delete(url(`maps/${map_id}/summary`), { - data: { - "route_id": route_id, - } - }); - return response.data.success; -}; - -// MAP LEADERBOARDS - -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' }; - } - }); - // should be unreachable - return undefined; -}; - -// MAP DISCUSSIONS - -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; -}; - -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; -}; - -const post_map_discussion = async (map_id: string, content: MapDiscussionContent): Promise => { - const response = await axios.post(url(`maps/${map_id}/discussions`), { - "title": content.title, - "content": content.content, - }); - return response.data.success; -}; - -const post_map_discussion_comment = async (map_id: string, discussion_id: number, content: MapDiscussionCommentContent): Promise => { - const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), { - "comment": content.comment, - }); - return response.data.success; -}; - -const delete_map_discussion = async (map_id: string, discussion_id: number): Promise => { - const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`)); - return response.data.success; }; -- cgit v1.2.3