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/Maps.tsx | 76 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 frontend/src/api/Maps.tsx

(limited to 'frontend/src/api/Maps.tsx')

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 @@
+import axios from "axios";
+import { url } from "./Api";
+import { MapDiscussionContent } from "../types/Content";
+import { MapSummary, MapLeaderboard, MapDiscussions, MapDiscussion } from "../types/Map";
+
+export const get_map_summary = async (map_id: string): Promise<MapSummary> => {
+  const response = await axios.get(url(`maps/${map_id}/summary`))
+  return response.data.data;
+};
+
+export const get_map_leaderboard = async (map_id: string): Promise<MapLeaderboard | undefined> => {
+  const response = await axios.get(url(`maps/${map_id}/leaderboards`));
+  console.log(response)
+  if (!response.data.success) {
+    return undefined;
+  }
+  const data = response.data.data;
+  console.log(data.records)
+  // 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' };
+    }
+  });
+  return data;
+};
+
+export const get_map_discussions = async (map_id: string): Promise<MapDiscussions | undefined> => {
+  const response = await axios.get(url(`maps/${map_id}/discussions`));
+  if (!response.data.data.discussions) {
+    return undefined;
+  }
+  return response.data.data;
+};
+
+export const get_map_discussion = async (map_id: string, discussion_id: number): Promise<MapDiscussion | undefined> => {
+  const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`));
+  if (!response.data.data.discussion) {
+    return undefined;
+  }
+  return response.data.data;
+};
+
+export const post_map_discussion = async (token: string, map_id: string, content: MapDiscussionContent): Promise<boolean> => {
+  const response = await axios.post(url(`maps/${map_id}/discussions`), {
+    "title": content.title,
+    "content": content.content,
+  }, {
+    headers: {
+      "Authorization": token,
+    }
+  });
+  return response.data.success;
+};
+
+export const post_map_discussion_comment = async (token: string, map_id: string, discussion_id: number, comment: string): Promise<boolean> => {
+  const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), {
+    "comment": comment,
+  }, {
+    headers: {
+      "Authorization": token,
+    }
+  });
+  return response.data.success;
+};
+
+export const delete_map_discussion = async (token: string, map_id: string, discussion_id: number): Promise<boolean> => {
+  const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`), {
+    headers: {
+      "Authorization": token,
+    }
+  });
+  return response.data.success;
+};
-- 
cgit v1.2.3

