diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-09 16:34:12 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-10-09 16:34:12 +0300 |
| commit | a7c282ca348c1e8e60559e5c064caee28ba11eec (patch) | |
| tree | 43bd7bdb2bbc80b92b96a14b36c33f0b7df622c9 /frontend/src/api/Api.tsx | |
| parent | Rankings page (diff) | |
| download | lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.tar.gz lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.tar.bz2 lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.zip | |
refactor: so much shit
Diffstat (limited to 'frontend/src/api/Api.tsx')
| -rw-r--r-- | frontend/src/api/Api.tsx | 205 |
1 files changed, 30 insertions, 175 deletions
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 @@ | |||
| 1 | import axios from 'axios'; | ||
| 2 | |||
| 3 | import { GameChapter, GamesChapters } from '../types/Chapters'; | ||
| 4 | import { Game } from '../types/Game'; | ||
| 5 | import { Map, MapDiscussion, MapDiscussions, MapLeaderboard, MapSummary } from '../types/Map'; | ||
| 6 | import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; | 1 | import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; |
| 7 | import { Search } from '../types/Search'; | 2 | import { delete_token, get_token } from './Auth'; |
| 8 | import { UserProfile } from '../types/Profile'; | 3 | import { get_user, get_profile, post_profile } from './User'; |
| 9 | import { Ranking } from '../types/Ranking'; | 4 | import { get_games, get_chapters, get_games_chapters, get_game_maps, get_search } from './Games'; |
| 5 | import { get_official_rankings, get_unofficial_rankings } from './Rankings'; | ||
| 6 | 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'; | ||
| 7 | import { delete_map_summary, post_map_summary, put_map_image, put_map_summary } from './Mod'; | ||
| 10 | 8 | ||
| 11 | // add new api call function entries here | 9 | // add new api call function entries here |
| 12 | // example usage: API.get_games(); | 10 | // example usage: API.get_games(); |
| 13 | export const API = { | 11 | export const API = { |
| 14 | user_logout: () => user_logout(), | 12 | // Auth |
| 15 | 13 | get_token: () => get_token(), | |
| 14 | |||
| 15 | delete_token: () => delete_token(), | ||
| 16 | // User | ||
| 16 | get_user: (user_id: string) => get_user(user_id), | 17 | get_user: (user_id: string) => get_user(user_id), |
| 18 | get_profile: (token: string) => get_profile(token), | ||
| 19 | post_profile: (token: string) => post_profile(token), | ||
| 20 | // Games | ||
| 17 | get_games: () => get_games(), | 21 | get_games: () => get_games(), |
| 18 | |||
| 19 | get_chapters: (chapter_id: string) => get_chapters(chapter_id), | 22 | get_chapters: (chapter_id: string) => get_chapters(chapter_id), |
| 20 | get_games_chapters: (game_id: string) => get_games_chapters(game_id), | 23 | get_games_chapters: (game_id: string) => get_games_chapters(game_id), |
| 21 | get_game_maps: (game_id: string) => get_game_maps(game_id), | 24 | get_game_maps: (game_id: string) => get_game_maps(game_id), |
| 22 | get_rankings: () => get_rankings(), | ||
| 23 | get_search: (q: string) => get_search(q), | 25 | get_search: (q: string) => get_search(q), |
| 24 | 26 | // Rankings | |
| 27 | get_official_rankings: () => get_official_rankings(), | ||
| 28 | get_unofficial_rankings: () => get_unofficial_rankings(), | ||
| 29 | // Maps | ||
| 25 | get_map_summary: (map_id: string) => get_map_summary(map_id), | 30 | get_map_summary: (map_id: string) => get_map_summary(map_id), |
| 26 | get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id), | 31 | get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id), |
| 27 | get_map_discussions: (map_id: string) => get_map_discussions(map_id), | 32 | get_map_discussions: (map_id: string) => get_map_discussions(map_id), |
| 28 | get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id), | 33 | get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id), |
| 29 | 34 | ||
| 30 | post_map_summary: (map_id: string, content: ModMenuContent) => post_map_summary(map_id, content), | 35 | post_map_discussion: (token: string, map_id: string, content: MapDiscussionContent) => post_map_discussion(token, map_id, content), |
| 31 | post_map_discussion: (map_id: string, content: MapDiscussionContent) => post_map_discussion(map_id, content), | 36 | post_map_discussion_comment: (token: string, map_id: string, discussion_id: number, comment: string) => post_map_discussion_comment(token, map_id, discussion_id, comment), |
| 32 | post_map_discussion_comment: (map_id: string, discussion_id: number, content: MapDiscussionCommentContent) => post_map_discussion_comment(map_id, discussion_id, content), | ||
| 33 | |||
| 34 | put_map_image: (map_id: string, image: string) => put_map_image(map_id, image), | ||
| 35 | put_map_summary: (map_id: string, content: ModMenuContent) => put_map_summary(map_id, content), | ||
| 36 | 37 | ||
| 37 | delete_map_summary: (map_id: string, route_id: number) => delete_map_summary(map_id, route_id), | 38 | delete_map_discussion: (token: string, map_id: string, discussion_id: number) => delete_map_discussion(token, map_id, discussion_id), |
| 38 | delete_map_discussion: (map_id: string, discussion_id: number) => delete_map_discussion(map_id, discussion_id), | 39 | // Mod |
| 40 | post_map_summary: (token: string, map_id: string, content: ModMenuContent) => post_map_summary(token, map_id, content), | ||
| 41 | |||
| 42 | put_map_image: (token: string, map_id: string, image: string) => put_map_image(token, map_id, image), | ||
| 43 | put_map_summary: (token: string, map_id: string, content: ModMenuContent) => put_map_summary(token, map_id, content), | ||
| 44 | |||
| 45 | delete_map_summary: (token: string, map_id: string, route_id: number) => delete_map_summary(token, map_id, route_id), | ||
| 39 | }; | 46 | }; |
| 40 | 47 | ||
| 41 | const BASE_API_URL: string = "https://lp.ardapektezol.com/api/v1/" | 48 | const BASE_API_URL: string = "/api/v1/" |
| 42 | 49 | ||
| 43 | function url(path: string): string { | 50 | export function url(path: string): string { |
| 44 | return BASE_API_URL + path; | 51 | return BASE_API_URL + path; |
| 45 | } | ||
| 46 | |||
| 47 | // USER | ||
| 48 | |||
| 49 | const user_logout = async () => { | ||
| 50 | await axios.delete(url("token")); | ||
| 51 | }; | ||
| 52 | |||
| 53 | const get_user = async (user_id: string): Promise<UserProfile> => { | ||
| 54 | const response = await axios.get(url(`users/${user_id}`)) | ||
| 55 | return response.data.data; | ||
| 56 | }; | ||
| 57 | |||
| 58 | |||
| 59 | // GAMES | ||
| 60 | |||
| 61 | const get_games = async (): Promise<Game[]> => { | ||
| 62 | const response = await axios.get(url("games")) | ||
| 63 | return response.data.data; | ||
| 64 | }; | ||
| 65 | |||
| 66 | const get_chapters = async (chapter_id: string): Promise<GameChapter> => { | ||
| 67 | const response = await axios.get(url(`chapters/${chapter_id}`)); | ||
| 68 | return response.data.data; | ||
| 69 | } | ||
| 70 | |||
| 71 | const get_games_chapters = async (game_id: string): Promise<GamesChapters> => { | ||
| 72 | const response = await axios.get(url(`games/${game_id}`)); | ||
| 73 | return response.data.data; | ||
| 74 | }; | ||
| 75 | |||
| 76 | const get_game_maps = async (game_id: string): Promise<Map[]> => { | ||
| 77 | const response = await axios.get(url(`games/${game_id}/maps`)) | ||
| 78 | return response.data.data.maps; | ||
| 79 | }; | ||
| 80 | |||
| 81 | |||
| 82 | // RANKINGS | ||
| 83 | const get_rankings = async (): Promise<Ranking> => { | ||
| 84 | const response = await axios.get(url(`rankings`)); | ||
| 85 | return response.data.data; | ||
| 86 | } | ||
| 87 | |||
| 88 | // SEARCH | ||
| 89 | |||
| 90 | const get_search = async (q: string): Promise<Search> => { | ||
| 91 | const response = await axios.get(url(`search?q=${q}`)) | ||
| 92 | return response.data.data; | ||
| 93 | }; | ||
| 94 | |||
| 95 | // MAP SUMMARY | ||
| 96 | |||
| 97 | const put_map_image = async (map_id: string, image: string): Promise<boolean> => { | ||
| 98 | const response = await axios.put(url(`maps/${map_id}/image`), { | ||
| 99 | "image": image, | ||
| 100 | }); | ||
| 101 | return response.data.success; | ||
| 102 | }; | ||
| 103 | |||
| 104 | const get_map_summary = async (map_id: string): Promise<MapSummary> => { | ||
| 105 | const response = await axios.get(url(`maps/${map_id}/summary`)) | ||
| 106 | return response.data.data; | ||
| 107 | }; | ||
| 108 | |||
| 109 | const post_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => { | ||
| 110 | const response = await axios.post(url(`maps/${map_id}/summary`), { | ||
| 111 | "user_name": content.name, | ||
| 112 | "score_count": content.score, | ||
| 113 | "record_date": content.date, | ||
| 114 | "showcase": content.showcase, | ||
| 115 | "description": content.description, | ||
| 116 | }); | ||
| 117 | return response.data.success; | ||
| 118 | }; | ||
| 119 | |||
| 120 | const put_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => { | ||
| 121 | const response = await axios.put(url(`maps/${map_id}/summary`), { | ||
| 122 | "route_id": content.id, | ||
| 123 | "user_name": content.name, | ||
| 124 | "score_count": content.score, | ||
| 125 | "record_date": content.date, | ||
| 126 | "showcase": content.showcase, | ||
| 127 | "description": content.description, | ||
| 128 | }); | ||
| 129 | return response.data.success; | ||
| 130 | }; | ||
| 131 | |||
| 132 | const delete_map_summary = async (map_id: string, route_id: number): Promise<boolean> => { | ||
| 133 | const response = await axios.delete(url(`maps/${map_id}/summary`), { | ||
| 134 | data: { | ||
| 135 | "route_id": route_id, | ||
| 136 | } | ||
| 137 | }); | ||
| 138 | return response.data.success; | ||
| 139 | }; | ||
| 140 | |||
| 141 | // MAP LEADERBOARDS | ||
| 142 | |||
| 143 | const get_map_leaderboard = async (map_id: string): Promise<MapLeaderboard | undefined> => { | ||
| 144 | const response = await axios.get(url(`maps/${map_id}/leaderboards`)) | ||
| 145 | if (!response.data.success) { | ||
| 146 | return undefined; | ||
| 147 | } | ||
| 148 | const data = response.data.data; | ||
| 149 | // map the kind of leaderboard | ||
| 150 | data.records = data.records.map((record: any) => { | ||
| 151 | if (record.host && record.partner) { | ||
| 152 | return { ...record, kind: 'multiplayer' }; | ||
| 153 | } else { | ||
| 154 | return { ...record, kind: 'singleplayer' }; | ||
| 155 | } | ||
| 156 | }); | ||
| 157 | // should be unreachable | ||
| 158 | return undefined; | ||
| 159 | }; | ||
| 160 | |||
| 161 | // MAP DISCUSSIONS | ||
| 162 | |||
| 163 | const get_map_discussions = async (map_id: string): Promise<MapDiscussions | undefined> => { | ||
| 164 | const response = await axios.get(url(`maps/${map_id}/discussions`)); | ||
| 165 | if (!response.data.data.discussions) { | ||
| 166 | return undefined; | ||
| 167 | } | ||
| 168 | return response.data.data; | ||
| 169 | }; | ||
| 170 | |||
| 171 | const get_map_discussion = async (map_id: string, discussion_id: number): Promise<MapDiscussion | undefined> => { | ||
| 172 | const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`)); | ||
| 173 | if (!response.data.data.discussion) { | ||
| 174 | return undefined; | ||
| 175 | } | ||
| 176 | return response.data.data; | ||
| 177 | }; | ||
| 178 | |||
| 179 | const post_map_discussion = async (map_id: string, content: MapDiscussionContent): Promise<boolean> => { | ||
| 180 | const response = await axios.post(url(`maps/${map_id}/discussions`), { | ||
| 181 | "title": content.title, | ||
| 182 | "content": content.content, | ||
| 183 | }); | ||
| 184 | return response.data.success; | ||
| 185 | }; | ||
| 186 | |||
| 187 | const post_map_discussion_comment = async (map_id: string, discussion_id: number, content: MapDiscussionCommentContent): Promise<boolean> => { | ||
| 188 | const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), { | ||
| 189 | "comment": content.comment, | ||
| 190 | }); | ||
| 191 | return response.data.success; | ||
| 192 | }; | ||
| 193 | |||
| 194 | const delete_map_discussion = async (map_id: string, discussion_id: number): Promise<boolean> => { | ||
| 195 | const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`)); | ||
| 196 | return response.data.success; | ||
| 197 | }; | 52 | }; |