aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/api
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-10-09 16:34:12 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-10-09 16:34:12 +0300
commita7c282ca348c1e8e60559e5c064caee28ba11eec (patch)
tree43bd7bdb2bbc80b92b96a14b36c33f0b7df622c9 /frontend/src/api
parentRankings page (diff)
downloadlphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.tar.gz
lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.tar.bz2
lphub-a7c282ca348c1e8e60559e5c064caee28ba11eec.zip
refactor: so much shit
Diffstat (limited to 'frontend/src/api')
-rw-r--r--frontend/src/api/Api.tsx205
-rw-r--r--frontend/src/api/Auth.tsx14
-rw-r--r--frontend/src/api/Games.tsx31
-rw-r--r--frontend/src/api/Maps.tsx76
-rw-r--r--frontend/src/api/Mod.tsx58
-rw-r--r--frontend/src/api/Rankings.tsx13
-rw-r--r--frontend/src/api/User.tsx25
7 files changed, 247 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 @@
1import axios from 'axios';
2
3import { GameChapter, GamesChapters } from '../types/Chapters';
4import { Game } from '../types/Game';
5import { Map, MapDiscussion, MapDiscussions, MapLeaderboard, MapSummary } from '../types/Map';
6import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; 1import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content';
7import { Search } from '../types/Search'; 2import { delete_token, get_token } from './Auth';
8import { UserProfile } from '../types/Profile'; 3import { get_user, get_profile, post_profile } from './User';
9import { Ranking } from '../types/Ranking'; 4import { get_games, get_chapters, get_games_chapters, get_game_maps, get_search } from './Games';
5import { get_official_rankings, get_unofficial_rankings } from './Rankings';
6import { get_map_summary, get_map_leaderboard, get_map_discussions, get_map_discussion, post_map_discussion, post_map_discussion_comment, delete_map_discussion } from './Maps';
7import { 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();
13export const API = { 11export 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
41const BASE_API_URL: string = "https://lp.ardapektezol.com/api/v1/" 48const BASE_API_URL: string = "/api/v1/"
42 49
43function url(path: string): string { 50export function url(path: string): string {
44 return BASE_API_URL + path; 51 return BASE_API_URL + path;
45}
46
47// USER
48
49const user_logout = async () => {
50 await axios.delete(url("token"));
51};
52
53const 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
61const get_games = async (): Promise<Game[]> => {
62 const response = await axios.get(url("games"))
63 return response.data.data;
64};
65
66const 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
71const 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
76const 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
83const get_rankings = async (): Promise<Ranking> => {
84 const response = await axios.get(url(`rankings`));
85 return response.data.data;
86}
87
88// SEARCH
89
90const 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
97const 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
104const 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
109const 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
120const 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
132const 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
143const 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
163const 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
171const 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
179const 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
187const 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
194const 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};
diff --git a/frontend/src/api/Auth.tsx b/frontend/src/api/Auth.tsx
new file mode 100644
index 0000000..09269e6
--- /dev/null
+++ b/frontend/src/api/Auth.tsx
@@ -0,0 +1,14 @@
1import axios from "axios";
2import { url } from "./Api";
3
4export const get_token = async (): Promise<string | undefined> => {
5 const response = await axios.get(url(`token`))
6 if (!response.data.success) {
7 return undefined;
8 }
9 return response.data.data.token;
10};
11
12export const delete_token = async () => {
13 await axios.delete(url("token"));
14};
diff --git a/frontend/src/api/Games.tsx b/frontend/src/api/Games.tsx
new file mode 100644
index 0000000..84b5f74
--- /dev/null
+++ b/frontend/src/api/Games.tsx
@@ -0,0 +1,31 @@
1import axios from "axios";
2import { url } from "./Api";
3import { GameChapter, GamesChapters } from "../types/Chapters";
4import { Game } from "../types/Game";
5import { Map } from "../types/Map";
6import { Search } from "../types/Search";
7
8export const get_games = async (): Promise<Game[]> => {
9 const response = await axios.get(url(`games`))
10 return response.data.data;
11};
12
13export const get_chapters = async (chapter_id: string): Promise<GameChapter> => {
14 const response = await axios.get(url(`chapters/${chapter_id}`));
15 return response.data.data;
16}
17
18export const get_games_chapters = async (game_id: string): Promise<GamesChapters> => {
19 const response = await axios.get(url(`games/${game_id}`));
20 return response.data.data;
21};
22
23export const get_game_maps = async (game_id: string): Promise<Map[]> => {
24 const response = await axios.get(url(`games/${game_id}/maps`))
25 return response.data.data.maps;
26};
27
28export const get_search = async (q: string): Promise<Search> => {
29 const response = await axios.get(url(`search?q=${q}`))
30 return response.data.data;
31};
diff --git a/frontend/src/api/Maps.tsx b/frontend/src/api/Maps.tsx
new file mode 100644
index 0000000..fbad78c
--- /dev/null
+++ b/frontend/src/api/Maps.tsx
@@ -0,0 +1,76 @@
1import axios from "axios";
2import { url } from "./Api";
3import { MapDiscussionContent } from "../types/Content";
4import { MapSummary, MapLeaderboard, MapDiscussions, MapDiscussion } from "../types/Map";
5
6export const get_map_summary = async (map_id: string): Promise<MapSummary> => {
7 const response = await axios.get(url(`maps/${map_id}/summary`))
8 return response.data.data;
9};
10
11export const get_map_leaderboard = async (map_id: string): Promise<MapLeaderboard | undefined> => {
12 const response = await axios.get(url(`maps/${map_id}/leaderboards`));
13 console.log(response)
14 if (!response.data.success) {
15 return undefined;
16 }
17 const data = response.data.data;
18 console.log(data.records)
19 // map the kind of leaderboard
20 data.records = data.records.map((record: any) => {
21 if (record.host && record.partner) {
22 return { ...record, kind: 'multiplayer' };
23 } else {
24 return { ...record, kind: 'singleplayer' };
25 }
26 });
27 return data;
28};
29
30export const get_map_discussions = async (map_id: string): Promise<MapDiscussions | undefined> => {
31 const response = await axios.get(url(`maps/${map_id}/discussions`));
32 if (!response.data.data.discussions) {
33 return undefined;
34 }
35 return response.data.data;
36};
37
38export const get_map_discussion = async (map_id: string, discussion_id: number): Promise<MapDiscussion | undefined> => {
39 const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`));
40 if (!response.data.data.discussion) {
41 return undefined;
42 }
43 return response.data.data;
44};
45
46export const post_map_discussion = async (token: string, map_id: string, content: MapDiscussionContent): Promise<boolean> => {
47 const response = await axios.post(url(`maps/${map_id}/discussions`), {
48 "title": content.title,
49 "content": content.content,
50 }, {
51 headers: {
52 "Authorization": token,
53 }
54 });
55 return response.data.success;
56};
57
58export const post_map_discussion_comment = async (token: string, map_id: string, discussion_id: number, comment: string): Promise<boolean> => {
59 const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), {
60 "comment": comment,
61 }, {
62 headers: {
63 "Authorization": token,
64 }
65 });
66 return response.data.success;
67};
68
69export const delete_map_discussion = async (token: string, map_id: string, discussion_id: number): Promise<boolean> => {
70 const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`), {
71 headers: {
72 "Authorization": token,
73 }
74 });
75 return response.data.success;
76};
diff --git a/frontend/src/api/Mod.tsx b/frontend/src/api/Mod.tsx
new file mode 100644
index 0000000..9091379
--- /dev/null
+++ b/frontend/src/api/Mod.tsx
@@ -0,0 +1,58 @@
1import axios from "axios";
2import { url } from "./Api";
3import { ModMenuContent } from "../types/Content";
4
5export const put_map_image = async (token: string, map_id: string, image: string): Promise<boolean> => {
6 const response = await axios.put(url(`maps/${map_id}/image`), {
7 "image": image,
8 }, {
9 headers: {
10 "Authorization": token,
11 }
12 });
13 return response.data.success;
14};
15
16export const post_map_summary = async (token: string, map_id: string, content: ModMenuContent): Promise<boolean> => {
17 const response = await axios.post(url(`maps/${map_id}/summary`), {
18 "category_id": content.category_id,
19 "user_name": content.name,
20 "score_count": content.score,
21 "record_date": content.date,
22 "showcase": content.showcase,
23 "description": content.description,
24 }, {
25 headers: {
26 "Authorization": token,
27 }
28 });
29 return response.data.success;
30};
31
32export const put_map_summary = async (token: string, map_id: string, content: ModMenuContent): Promise<boolean> => {
33 const response = await axios.put(url(`maps/${map_id}/summary`), {
34 "route_id": content.id,
35 "user_name": content.name,
36 "score_count": content.score,
37 "record_date": content.date,
38 "showcase": content.showcase,
39 "description": content.description,
40 }, {
41 headers: {
42 "Authorization": token,
43 }
44 });
45 return response.data.success;
46};
47
48export const delete_map_summary = async (token: string, map_id: string, route_id: number): Promise<boolean> => {
49 const response = await axios.delete(url(`maps/${map_id}/summary`), {
50 data: {
51 "route_id": route_id,
52 },
53 headers: {
54 "Authorization": token,
55 }
56 });
57 return response.data.success;
58};
diff --git a/frontend/src/api/Rankings.tsx b/frontend/src/api/Rankings.tsx
new file mode 100644
index 0000000..384f826
--- /dev/null
+++ b/frontend/src/api/Rankings.tsx
@@ -0,0 +1,13 @@
1import axios from "axios";
2import { url } from "./Api";
3import { Ranking, SteamRanking } from "../types/Ranking";
4
5export const get_official_rankings = async (): Promise<Ranking> => {
6 const response = await axios.get(url(`rankings/lphub`));
7 return response.data.data;
8};
9
10export const get_unofficial_rankings = async (): Promise<SteamRanking> => {
11 const response = await axios.get(url(`rankings/steam`));
12 return response.data.data;
13};
diff --git a/frontend/src/api/User.tsx b/frontend/src/api/User.tsx
new file mode 100644
index 0000000..c4d1944
--- /dev/null
+++ b/frontend/src/api/User.tsx
@@ -0,0 +1,25 @@
1import axios from "axios";
2import { url } from "./Api";
3import { UserProfile } from "../types/Profile";
4
5export const get_user = async (user_id: string): Promise<UserProfile> => {
6 const response = await axios.get(url(`users/${user_id}`));
7 return response.data.data;
8};
9
10export const get_profile = async (token: string): Promise<UserProfile> => {
11 const response = await axios.get(url(`profile`), {
12 headers: {
13 "Authorization": token,
14 }
15 });
16 return response.data.data;
17};
18
19export const post_profile = async (token: string) => {
20 const _ = await axios.post(url(`profile`), {}, {
21 headers: {
22 "Authorization": token,
23 }
24 });
25};