From 4c0ebe738e4c39a10e5c57bbcb36ff9d42ebf0e5 Mon Sep 17 00:00:00 2001
From: Ingrid Martinsheimen Egge <ingrimeg@stud.ntnu.no>
Date: Tue, 2 May 2023 18:04:36 +0200
Subject: [PATCH] bedre feilmeldinger. forenklet metoder

---
 src/components/EditAccount.vue | 63 +++++++++++++++++---------------
 src/components/EditProfile.vue | 67 ++++++++++++++++++++++------------
 src/util/API.js                |  3 +-
 3 files changed, 78 insertions(+), 55 deletions(-)

diff --git a/src/components/EditAccount.vue b/src/components/EditAccount.vue
index 5ad3d4a..3cdb1dd 100644
--- a/src/components/EditAccount.vue
+++ b/src/components/EditAccount.vue
@@ -1,5 +1,6 @@
 <template>
     <h2>Konto-innstillinger</h2>
+
     <form @submit.prevent="submit">
         <p class="infoText">OBS: Kontakt admin dersom du ønsker å oppdatere epost</p><br>
 
@@ -15,6 +16,8 @@
         <p :style={color:alertMsgColor} id="alert">{{alertMsg}}</p>
     </form>
 
+    <button @click="logOut">Logg ut</button>
+
     <br>
     <br>
     <hr>
@@ -61,37 +64,30 @@ export default {
     },
     methods: {
         saveAccountSettings(){ //passord
-            if(this.updatedAccount.upPassword.length===0){ //Oppdater ikke passordet dersom man ikke har gjort endringer
-                const id = this.account.id;
+            const id = this.account.id;
 
-                API.updateAccount(
-                    id,{
-                        firstname:this.updatedAccount.upFirstname,
-                        password:null,
-                    }
-                ).then((savedAccount)=>{
-                    useAuthStore().setAccount(savedAccount);
-                    this.setAlertText("Melding: Bruker oppdatert.",'light-green');
-                }).catch((error)=> {
-                    this.setAlertText("Melding: Det oppsto en feil. ",'red');
-                    console.log(error)
-                })
-            } else {
-                const id = this.account.id;
+            let newPassword = null
+            let newFirstName = null;
 
-                API.updateAccount(
-                    id,{
-                        firstname:this.updatedAccount.upFirstname,
-                        password:this.updatedAccount.upPassword,
-                    }
-                ).then((savedAccount)=>{
-                    useAuthStore().setAccount(savedAccount);
-                    this.setAlertText("Melding: Konto oppdatert.",'light-green')
-                }).catch((error)=> {
-                    this.setAlertText("Melding: Det oppsto en feil. ",'red');
-                    console.log(error)
-                })
+            //checks if username and password have been changed
+            if(this.updatedAccount.upPassword.length!==0) {
+                newPassword = this.updatedAccount.upPassword;
+            }
+            if(this.updatedAccount.upFirstname!==''){
+                newFirstName = this.updatedAccount.upFirstname;
             }
+
+            API.updateAccount(
+                id,{
+                    firstname:newFirstName,
+                    password:newPassword,
+                }
+            ).then((savedAccount)=>{
+                useAuthStore().setAccount(savedAccount);
+                this.setAlertText("Konto oppdatert.",'light-green')
+            }).catch((error)=> {
+                this.setAlertText("Det oppsto en feil. ",'red');
+            })
         },
         deleteAccount(){
             if(this.deletionConfirmation===false){
@@ -108,6 +104,14 @@ export default {
                 })
             }
         },
+        logOut(){
+            useAuthStore().logout();
+            router.push('/login')
+        },
+
+
+
+
         setAlertText(text, color){
             switch (color) {
                 case 'red':
@@ -140,9 +144,8 @@ export default {
                     break;
             }
         }
-
     }
-}
+ }
 </script>
 
 <style scoped lang="scss">
diff --git a/src/components/EditProfile.vue b/src/components/EditProfile.vue
index 3487a09..4b00dd4 100644
--- a/src/components/EditProfile.vue
+++ b/src/components/EditProfile.vue
@@ -62,6 +62,8 @@ export default {
         return {
             alertMsg:'',
             alertMsgColor:'black',
+            initialName: '', //used to compare with updated values
+            initialRestriction: '',
         }
     },
     computed: {
@@ -81,6 +83,10 @@ export default {
             return this.updatedProfile.upImage.length > 0;
         }
     },
+    beforeMount() {
+        this.initialName=this.profile.name;
+        this.initialRestriction=this.profile.restricted;
+    },
     methods: {
         changeProfile(){
             router.push("/selectProfile");
@@ -89,11 +95,46 @@ export default {
                 const id = this.profile.id;
                 API.deleteProfile(id).then(() => {
                     router.push('/selectProfile')
-                }).catch((error) => {
-                    this.setAlertText("Det oppsto en feil ved sletting profil: " + error, 'red')
+                }).catch((_) => {
+                    this.setAlertText("Alle kontoer må ha minst en profil (profil ble ikke slettet)", 'red')
                 })
         },
 
+        saveUserSettings(){
+            const id = this.profile.id;
+
+            let newName = null;
+            let newRestricted = null;
+
+            if(this.updatedProfile.upName !== this.initialName){
+                newName = this.updatedProfile.upName
+            }
+            if(this.updatedProfile.upRestricted !== this.initialRestriction){
+                newRestricted = this.updatedProfile.upRestricted
+            }
+
+            API.updateProfile(
+                id,{
+                    name:newName,
+                    profileImageUrl:this.updatedProfile.upImage,
+                    restricted: newRestricted,
+                }
+            ).then((savedProfile)=>{
+                useAuthStore().setProfile(savedProfile);
+                this.setAlertText("Profil oppdatert.",'light-green')
+            }).catch(error=> {
+                if (error.message === '400') {
+                    if(this.updatedProfile.name !== this.initialName){
+                        this.setAlertText('Det oppsto en feil: Det finnes allerede en bruker med samme navn' ,'red')
+                    } else {
+                        this.setAlertText('Det oppsto en feil: Sørg for at det finnes mist en standard profil på kontoen ','red')
+                    }
+                }else{
+                    this.setAlertText("Det oppsto en feil.",'red')
+                }
+            })
+        },
+
 
 
 
@@ -101,29 +142,7 @@ export default {
         updateImage(){
             //todo update image preview
         },
-        saveUserSettings(){
-            const id = this.profile.id;
 
-            const numOfProfiles = API.getProfiles().length
-            if(numOfProfiles===1 && this.updatedProfile.upRestricted===true){
-                this.setAlertText("Du må ha minst en standardprofil per konto. (ingen endringer er gjort)",'red')
-            }
-            else {
-              API.updateProfile(
-                  id,{
-                    name:this.updatedProfile.upName,
-                    profileImageUrl:this.updatedProfile.upImage,
-                    restricted:this.updatedProfile.upRestricted,
-                  }
-              ).then((savedProfile)=>{
-                useAuthStore().setProfile(savedProfile);
-                  this.setAlertText("profil oppdatert.",'light-green')
-              }).catch((error)=> {
-                  this.setAlertText("Det oppsto en feil",'red')
-                console.log(error)
-              })
-            }
-        },
         chooseProfilePicture(){
             this.setAlertText("skriv inn bildelenke i feltet, og oppdater innstillinger",'black')
         },
diff --git a/src/util/API.js b/src/util/API.js
index acf4a4e..c4653e1 100644
--- a/src/util/API.js
+++ b/src/util/API.js
@@ -287,6 +287,7 @@ export const API = {
 
     /**
      * Updates the profile name, restriction and profile image
+     * If error: "error.message" returns the status code
      * @param id profile id
      * @param request
      * @returns {Promise<*>}
@@ -306,7 +307,7 @@ export const API = {
                 return response.data;
             })
             .catch((error) => {
-                throw new Error("Error when updating profile: " + error);
+                throw new Error(error.response.status);
             });
 
     },
-- 
GitLab