From 6ef252a5e63d7a766c1234a01fb7be86c6061e8b Mon Sep 17 00:00:00 2001
From: Zara Mudassar <zara.1310@hotmail.com>
Date: Mon, 2 May 2022 14:14:13 +0200
Subject: [PATCH] Functionality added for leaving a community

---
 .../CommunityHamburger.vue                    | 19 ++++++++++++++++++-
 src/utils/apiutil.js                          | 14 ++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/components/CommunityComponents/CommunityHamburger.vue b/src/components/CommunityComponents/CommunityHamburger.vue
index 2ff2171..704b199 100644
--- a/src/components/CommunityComponents/CommunityHamburger.vue
+++ b/src/components/CommunityComponents/CommunityHamburger.vue
@@ -27,7 +27,8 @@
       </li>
       <li id="leaveGroup">
         <div
-          class="cursor-pointer block py-2 px-4 text-sm text-red-600 hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
+          class="cursor-pointer block py-2 px-4 text-sm text-error hover:bg-gray-100 dark:hover:bg-gray-600 dark:text-gray-200 dark:hover:text-white"
+          @click="leaveCommunity"
         >
           Forlat Gruppe
         </div>
@@ -37,10 +38,26 @@
 </template>
 
 <script>
+
+import { LeaveCommunity } from "@/utils/apiutil";
+
 export default {
   name: "CommunityHamburger",
   props: {
     communityID: Number,
   },
+  data(){
+    return{
+      id: -1,
+    }
+  },
+
+  methods:{
+    leaveCommunity: async function(){
+      this.id = await this.$router.currentRoute.value.params.communityID;
+      await LeaveCommunity(this.id);
+      this.$router.push('/');
+    }
+  }
 };
 </script>
diff --git a/src/utils/apiutil.js b/src/utils/apiutil.js
index 5f6a226..fd412f2 100644
--- a/src/utils/apiutil.js
+++ b/src/utils/apiutil.js
@@ -223,3 +223,17 @@ export async function GetIfUserAlreadyInCommunity(communityID) {
             return error;
         });
 }
+
+export async function LeaveCommunity(communityID) {
+    return axios
+        .patch(API_URL + "communities/" + communityID + "/leave", communityID, {
+            headers: tokenHeader(),
+        })
+        .then((response) => {
+            return response.data;
+        })
+        .catch((error) => {
+            console.log(error.data);
+            return error;
+        });
+}
-- 
GitLab