Skip to content
Snippets Groups Projects
Commit 4c0ebe73 authored by Ingrid Martinsheimen Egge's avatar Ingrid Martinsheimen Egge :cow2:
Browse files

bedre feilmeldinger. forenklet metoder

parent c4ba80e2
No related branches found
No related tags found
1 merge request!21Merge profilinnstillinger into main
<template> <template>
<h2>Konto-innstillinger</h2> <h2>Konto-innstillinger</h2>
<form @submit.prevent="submit"> <form @submit.prevent="submit">
<p class="infoText">OBS: Kontakt admin dersom du ønsker å oppdatere epost</p><br> <p class="infoText">OBS: Kontakt admin dersom du ønsker å oppdatere epost</p><br>
...@@ -15,6 +16,8 @@ ...@@ -15,6 +16,8 @@
<p :style={color:alertMsgColor} id="alert">{{alertMsg}}</p> <p :style={color:alertMsgColor} id="alert">{{alertMsg}}</p>
</form> </form>
<button @click="logOut">Logg ut</button>
<br> <br>
<br> <br>
<hr> <hr>
...@@ -61,37 +64,30 @@ export default { ...@@ -61,37 +64,30 @@ export default {
}, },
methods: { methods: {
saveAccountSettings(){ //passord 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( let newPassword = null
id,{ let newFirstName = null;
firstname:this.updatedAccount.upFirstname,
password:null, //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;
} }
).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;
API.updateAccount( API.updateAccount(
id,{ id,{
firstname:this.updatedAccount.upFirstname, firstname:newFirstName,
password:this.updatedAccount.upPassword, password:newPassword,
} }
).then((savedAccount)=>{ ).then((savedAccount)=>{
useAuthStore().setAccount(savedAccount); useAuthStore().setAccount(savedAccount);
this.setAlertText("Melding: Konto oppdatert.",'light-green') this.setAlertText("Konto oppdatert.",'light-green')
}).catch((error)=> { }).catch((error)=> {
this.setAlertText("Melding: Det oppsto en feil. ",'red'); this.setAlertText("Det oppsto en feil. ",'red');
console.log(error)
}) })
}
}, },
deleteAccount(){ deleteAccount(){
if(this.deletionConfirmation===false){ if(this.deletionConfirmation===false){
...@@ -108,6 +104,14 @@ export default { ...@@ -108,6 +104,14 @@ export default {
}) })
} }
}, },
logOut(){
useAuthStore().logout();
router.push('/login')
},
setAlertText(text, color){ setAlertText(text, color){
switch (color) { switch (color) {
case 'red': case 'red':
...@@ -140,7 +144,6 @@ export default { ...@@ -140,7 +144,6 @@ export default {
break; break;
} }
} }
} }
} }
</script> </script>
......
...@@ -62,6 +62,8 @@ export default { ...@@ -62,6 +62,8 @@ export default {
return { return {
alertMsg:'', alertMsg:'',
alertMsgColor:'black', alertMsgColor:'black',
initialName: '', //used to compare with updated values
initialRestriction: '',
} }
}, },
computed: { computed: {
...@@ -81,6 +83,10 @@ export default { ...@@ -81,6 +83,10 @@ export default {
return this.updatedProfile.upImage.length > 0; return this.updatedProfile.upImage.length > 0;
} }
}, },
beforeMount() {
this.initialName=this.profile.name;
this.initialRestriction=this.profile.restricted;
},
methods: { methods: {
changeProfile(){ changeProfile(){
router.push("/selectProfile"); router.push("/selectProfile");
...@@ -89,41 +95,54 @@ export default { ...@@ -89,41 +95,54 @@ export default {
const id = this.profile.id; const id = this.profile.id;
API.deleteProfile(id).then(() => { API.deleteProfile(id).then(() => {
router.push('/selectProfile') router.push('/selectProfile')
}).catch((error) => { }).catch((_) => {
this.setAlertText("Det oppsto en feil ved sletting profil: " + error, 'red') this.setAlertText("Alle kontoer må ha minst en profil (profil ble ikke slettet)", 'red')
}) })
}, },
updateImage(){
//todo update image preview
},
saveUserSettings(){ saveUserSettings(){
const id = this.profile.id; const id = this.profile.id;
const numOfProfiles = API.getProfiles().length let newName = null;
if(numOfProfiles===1 && this.updatedProfile.upRestricted===true){ let newRestricted = null;
this.setAlertText("Du må ha minst en standardprofil per konto. (ingen endringer er gjort)",'red')
if(this.updatedProfile.upName !== this.initialName){
newName = this.updatedProfile.upName
} }
else { if(this.updatedProfile.upRestricted !== this.initialRestriction){
newRestricted = this.updatedProfile.upRestricted
}
API.updateProfile( API.updateProfile(
id,{ id,{
name:this.updatedProfile.upName, name:newName,
profileImageUrl:this.updatedProfile.upImage, profileImageUrl:this.updatedProfile.upImage,
restricted:this.updatedProfile.upRestricted, restricted: newRestricted,
} }
).then((savedProfile)=>{ ).then((savedProfile)=>{
useAuthStore().setProfile(savedProfile); useAuthStore().setProfile(savedProfile);
this.setAlertText("profil oppdatert.",'light-green') this.setAlertText("Profil oppdatert.",'light-green')
}).catch((error)=> { }).catch(error=> {
this.setAlertText("Det oppsto en feil",'red') if (error.message === '400') {
console.log(error) 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')
}
})
}, },
updateImage(){
//todo update image preview
},
chooseProfilePicture(){ chooseProfilePicture(){
this.setAlertText("skriv inn bildelenke i feltet, og oppdater innstillinger",'black') this.setAlertText("skriv inn bildelenke i feltet, og oppdater innstillinger",'black')
}, },
......
...@@ -287,6 +287,7 @@ export const API = { ...@@ -287,6 +287,7 @@ export const API = {
/** /**
* Updates the profile name, restriction and profile image * Updates the profile name, restriction and profile image
* If error: "error.message" returns the status code
* @param id profile id * @param id profile id
* @param request * @param request
* @returns {Promise<*>} * @returns {Promise<*>}
...@@ -306,7 +307,7 @@ export const API = { ...@@ -306,7 +307,7 @@ export const API = {
return response.data; return response.data;
}) })
.catch((error) => { .catch((error) => {
throw new Error("Error when updating profile: " + error); throw new Error(error.response.status);
}); });
}, },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment