From a65d6d9127c3fa7f6a8ecaec5d1ffd1f47c2bc98 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:08:53 +0300 Subject: refactor: port to typescript --- frontend/src/api/Api.tsx | 157 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 frontend/src/api/Api.tsx (limited to 'frontend/src/api') 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 @@ +import axios from 'axios'; + +import { Game } from '../types/Game'; +import { MapDiscussion, MapDiscussions, MapLeaderboard, MapSummary } from '../types/Map'; +import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; +import { Search } from '../types/Search'; +import { UserProfile } from '../types/Profile'; + +// add new api call function entries here +// example usage: API.get_games(); +export const API = { + user_logout: () => user_logout(), + + get_user: (user_id: string) => get_user(user_id), + get_games: () => get_games(), + get_search: (q: string) => get_search(q), + 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), + + 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), +}; + +const BASE_API_URL: string = "https://lp.ardapektezol.com/api/v1/" + +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; +}; + +// 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; + } + return response.data.data; +}; + +// 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