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>
<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">
......
......@@ -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')
},
......
......@@ -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);
});
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment