From 29c5f03e6cd0abe628f14fb1afc9b41e1861018f Mon Sep 17 00:00:00 2001
From: Ingrid <ingridmegge@gmail.com>
Date: Wed, 26 Apr 2023 17:56:58 +0200
Subject: [PATCH] la inn sletting av profil

---
 src/components/EditProfile.vue | 44 ++++++++++++++++++++++------------
 src/util/API.js                | 26 ++++++++++++++++++++
 2 files changed, 55 insertions(+), 15 deletions(-)

diff --git a/src/components/EditProfile.vue b/src/components/EditProfile.vue
index 1efe19b..aef56df 100644
--- a/src/components/EditProfile.vue
+++ b/src/components/EditProfile.vue
@@ -16,10 +16,10 @@
             <input type="text" required v-model="this.updatedProfile.upName"><br>
             <br>
             <h4>Brukertype</h4>
-            <input type="radio" id="normal" value="false" v-model="this.updatedProfile.upRestricted">
+            <input type="radio" id="normal" value="false" name="restrict" v-model="this.updatedProfile.upRestricted">
             <label for="normal"> Standard</label><br>
 
-            <input type="radio" id="restricted" value="true" v-model="this.updatedProfile.upRestricted">
+            <input type="radio" id="restricted" value="true" name="restrict" v-model="this.updatedProfile.upRestricted">
             <label for="restricted"> Begrenset - Kan ikke redigere ukeplan eller handleliste</label><br>
 
             <br>
@@ -39,7 +39,7 @@
         <br><br>
             <div id = "submitbuttonBox">
                 <button class="saveBtn" @click=" saveUserSettings">Lagre profilendringer</button>
-                <button class="delBtn" @click="deleteUser">Slett brukerprofil</button>
+                <button class="delBtn" @click="deleteProfile">Slett brukerprofil</button>
             </div>
 
         </form>
@@ -73,34 +73,48 @@ export default {
             return this.updatedProfile.upImage.length > 0;
         }
     },
-    data() {
-        return {
-            deletionConfirmation: false,
-        }
-    },
     methods: {
         saveUserSettings(){
             const id = this.profile.id;
 
-            API.updateProfile(
-                id,{
+            const numOfProfiles = API.getProfiles().length
+            if(numOfProfiles===1 && this.updatedProfile.upRestricted===true){
+              alert("Du må ha minst en standardprofil per konto. (ingen endringer er gjort)")
+            }
+            else {
+              API.updateProfile(
+                  id,{
                     name:this.updatedProfile.upName,
                     profileImageUrl:this.updatedProfile.upImage,
                     restricted:this.updatedProfile.upRestricted,
-                }
-            ).then((savedProfile)=>{
+                  }
+              ).then((savedProfile)=>{
                 useAuthStore().setProfile(savedProfile);
                 alert("profil oppdatert.")
-            }).catch((error)=> {
+              }).catch((error)=> {
                 console.log(error)
-            })
+              })
+            }
         },
         chooseProfilePicture(){
             alert("skriv inn bildelenke i feltet, og oppdater innstillinger")
         },
         changeProfile(){
             router.push("/selectProfile");
-        }
+        },
+      deleteProfile(){
+          const numOfProfiles = API.getProfiles().length
+          if(numOfProfiles===1){
+            alert("Du kan ikke slette profilen. Hver Konto må ha minst en profil")
+          }else {
+            const id = this.profile.id;
+            API.deleteProfile(id).then(()=>{
+              router.push('/selectProfile')
+            }).catch((error)=> {
+              alert("Det oppsto en feil ved sletting profil: " + error)
+            })
+          }
+      }
     }
 }
 
diff --git a/src/util/API.js b/src/util/API.js
index 8a9b450..a9c0fed 100644
--- a/src/util/API.js
+++ b/src/util/API.js
@@ -191,6 +191,32 @@ export const API = {
 
     },
 
+    /**
+     * Deletes a profile from an account
+     * @param id
+     * @param request
+     * @returns {Promise<*>}
+     */
+    deleteProfile: async (id) => {
+        const authStore = useAuthStore();
+        if (!authStore.isLoggedIn) {
+            throw new Error();
+        }
+
+        return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/profile/${id}`, {
+            headers: { Authorization: `Bearer ${authStore.token}` },
+        })
+            .then(() => {
+                router.push('/selectProfile')
+            })
+            .catch(() => {
+                throw new Error("Kan ikke slette profil");
+            });
+
+    },
+
+
+
 
 
 
-- 
GitLab