diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-09-03 00:08:53 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-09-03 00:08:53 +0300 |
| commit | a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98 (patch) | |
| tree | edf8630e9d6426124dd49854af0cb703ebc5b710 /frontend/src/api | |
| parent | fix: revert to static homepage (#195) (diff) | |
| download | lphub-a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98.tar.gz lphub-a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98.tar.bz2 lphub-a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98.zip | |
refactor: port to typescript
Diffstat (limited to 'frontend/src/api')
| -rw-r--r-- | frontend/src/api/Api.tsx | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/frontend/src/api/Api.tsx b/frontend/src/api/Api.tsx new file mode 100644 index 0000000..9e45bc4 --- /dev/null +++ b/frontend/src/api/Api.tsx | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | import axios from 'axios'; | ||
| 2 | |||
| 3 | import { Game } from '../types/Game'; | ||
| 4 | import { MapDiscussion, MapDiscussions, MapLeaderboard, MapSummary } from '../types/Map'; | ||
| 5 | import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; | ||
| 6 | import { Search } from '../types/Search'; | ||
| 7 | import { UserProfile } from '../types/Profile'; | ||
| 8 | |||
| 9 | // add new api call function entries here | ||
| 10 | // example usage: API.get_games(); | ||
| 11 | export const API = { | ||
| 12 | user_logout: () => user_logout(), | ||
| 13 | |||
| 14 | get_user: (user_id: string) => get_user(user_id), | ||
| 15 | get_games: () => get_games(), | ||
| 16 | get_search: (q: string) => get_search(q), | ||
| 17 | get_map_summary: (map_id: string) => get_map_summary(map_id), | ||
| 18 | get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id), | ||
| 19 | get_map_discussions: (map_id: string) => get_map_discussions(map_id), | ||
| 20 | get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id), | ||
| 21 | |||
| 22 | post_map_summary: (map_id: string, content: ModMenuContent) => post_map_summary(map_id, content), | ||
| 23 | post_map_discussion: (map_id: string, content: MapDiscussionContent) => post_map_discussion(map_id, content), | ||
| 24 | post_map_discussion_comment: (map_id: string, discussion_id: number, content: MapDiscussionCommentContent) => post_map_discussion_comment(map_id, discussion_id, content), | ||
| 25 | |||
| 26 | put_map_image: (map_id: string, image: string) => put_map_image(map_id, image), | ||
| 27 | put_map_summary: (map_id: string, content: ModMenuContent) => put_map_summary(map_id, content), | ||
| 28 | |||
| 29 | delete_map_summary: (map_id: string, route_id: number) => delete_map_summary(map_id, route_id), | ||
| 30 | delete_map_discussion: (map_id: string, discussion_id: number) => delete_map_discussion(map_id, discussion_id), | ||
| 31 | }; | ||
| 32 | |||
| 33 | const BASE_API_URL: string = "https://lp.ardapektezol.com/api/v1/" | ||
| 34 | |||
| 35 | function url(path: string): string { | ||
| 36 | return BASE_API_URL + path; | ||
| 37 | } | ||
| 38 | |||
| 39 | // USER | ||
| 40 | |||
| 41 | const user_logout = async () => { | ||
| 42 | await axios.delete(url("token")); | ||
| 43 | }; | ||
| 44 | |||
| 45 | const get_user = async (user_id: string): Promise<UserProfile> => { | ||
| 46 | const response = await axios.get(url(`users/${user_id}`)) | ||
| 47 | return response.data.data; | ||
| 48 | }; | ||
| 49 | |||
| 50 | |||
| 51 | // GAMES | ||
| 52 | |||
| 53 | const get_games = async (): Promise<Game[]> => { | ||
| 54 | const response = await axios.get(url("games")) | ||
| 55 | return response.data.data; | ||
| 56 | }; | ||
| 57 | |||
| 58 | // SEARCH | ||
| 59 | |||
| 60 | const get_search = async (q: string): Promise<Search> => { | ||
| 61 | const response = await axios.get(url(`search?q=${q}`)) | ||
| 62 | return response.data.data; | ||
| 63 | }; | ||
| 64 | |||
| 65 | // MAP SUMMARY | ||
| 66 | |||
| 67 | const put_map_image = async (map_id: string, image: string): Promise<boolean> => { | ||
| 68 | const response = await axios.put(url(`maps/${map_id}/image`), { | ||
| 69 | "image": image, | ||
| 70 | }); | ||
| 71 | return response.data.success; | ||
| 72 | }; | ||
| 73 | |||
| 74 | const get_map_summary = async (map_id: string): Promise<MapSummary> => { | ||
| 75 | const response = await axios.get(url(`maps/${map_id}/summary`)) | ||
| 76 | return response.data.data; | ||
| 77 | }; | ||
| 78 | |||
| 79 | const post_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => { | ||
| 80 | const response = await axios.post(url(`maps/${map_id}/summary`), { | ||
| 81 | "user_name": content.name, | ||
| 82 | "score_count": content.score, | ||
| 83 | "record_date": content.date, | ||
| 84 | "showcase": content.showcase, | ||
| 85 | "description": content.description, | ||
| 86 | }); | ||
| 87 | return response.data.success; | ||
| 88 | }; | ||
| 89 | |||
| 90 | const put_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => { | ||
| 91 | const response = await axios.put(url(`maps/${map_id}/summary`), { | ||
| 92 | "route_id": content.id, | ||
| 93 | "user_name": content.name, | ||
| 94 | "score_count": content.score, | ||
| 95 | "record_date": content.date, | ||
| 96 | "showcase": content.showcase, | ||
| 97 | "description": content.description, | ||
| 98 | }); | ||
| 99 | return response.data.success; | ||
| 100 | }; | ||
| 101 | |||
| 102 | const delete_map_summary = async (map_id: string, route_id: number): Promise<boolean> => { | ||
| 103 | const response = await axios.delete(url(`maps/${map_id}/summary`), { | ||
| 104 | data: { | ||
| 105 | "route_id": route_id, | ||
| 106 | } | ||
| 107 | }); | ||
| 108 | return response.data.success; | ||
| 109 | }; | ||
| 110 | |||
| 111 | // MAP LEADERBOARDS | ||
| 112 | |||
| 113 | const get_map_leaderboard = async (map_id: string): Promise<MapLeaderboard | undefined> => { | ||
| 114 | const response = await axios.get(url(`maps/${map_id}/leaderboards`)) | ||
| 115 | if (!response.data.success) { | ||
| 116 | return undefined; | ||
| 117 | } | ||
| 118 | return response.data.data; | ||
| 119 | }; | ||
| 120 | |||
| 121 | // MAP DISCUSSIONS | ||
| 122 | |||
| 123 | const get_map_discussions = async (map_id: string): Promise<MapDiscussions | undefined> => { | ||
| 124 | const response = await axios.get(url(`maps/${map_id}/discussions`)); | ||
| 125 | if (!response.data.data.discussions) { | ||
| 126 | return undefined; | ||
| 127 | } | ||
| 128 | return response.data.data; | ||
| 129 | }; | ||
| 130 | |||
| 131 | const get_map_discussion = async (map_id: string, discussion_id: number): Promise<MapDiscussion | undefined> => { | ||
| 132 | const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`)); | ||
| 133 | if (!response.data.data.discussion) { | ||
| 134 | return undefined; | ||
| 135 | } | ||
| 136 | return response.data.data; | ||
| 137 | }; | ||
| 138 | |||
| 139 | const post_map_discussion = async (map_id: string, content: MapDiscussionContent): Promise<boolean> => { | ||
| 140 | const response = await axios.post(url(`maps/${map_id}/discussions`), { | ||
| 141 | "title": content.title, | ||
| 142 | "content": content.content, | ||
| 143 | }); | ||
| 144 | return response.data.success; | ||
| 145 | }; | ||
| 146 | |||
| 147 | const post_map_discussion_comment = async (map_id: string, discussion_id: number, content: MapDiscussionCommentContent): Promise<boolean> => { | ||
| 148 | const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), { | ||
| 149 | "comment": content.comment, | ||
| 150 | }); | ||
| 151 | return response.data.success; | ||
| 152 | }; | ||
| 153 | |||
| 154 | const delete_map_discussion = async (map_id: string, discussion_id: number): Promise<boolean> => { | ||
| 155 | const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`)); | ||
| 156 | return response.data.success; | ||
| 157 | }; | ||