diff --git a/src/util/API.js b/src/util/API.js index d5b49caea90e41bed1846769cf9d385d13d963f1..d6f543228341e341ac05ffe294bd60c9e1da979d 100644 --- a/src/util/API.js +++ b/src/util/API.js @@ -105,15 +105,22 @@ export const API = { }, /** - * Fetches all available foodpreference options + * Deletes account from the + * @param id + * @param token + * @returns {Promise<*>} */ - getFoodpreferences: async () => { - return axios.get(`${import.meta.env.VITE_BACKEND_URL}/foodpreferences`) + deleteAccount: async (id, token) => { + return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/account/{id}`, { + headers: { Authorization: `Bearer ${token}` }, + }) .then((response) => { return response.data; - }).catch(() => { - throw new Error(); + }) + .catch(() => { + throw new Error("Account not found or not accessible"); }); + }, /** @@ -128,38 +135,28 @@ export const API = { }); }, - /** - * Deletes account from the - * @param id - * @param token - * @returns {Promise<*>} - */ - deleteAccount: async (id, token) => { - return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/account/{id}`, { - headers: { Authorization: `Bearer ${token}` }, - }) - .then((response) => { - return response.data; - }) - .catch(() => { - throw new Error("Account not found or not accessible"); - }); + updateProfile: async (id, request) => { + const authStore = useAuthStore(); + if (!authStore.isLoggedIn) { + throw new Error(); + } - }, + return axios.put(`${import.meta.env.VITE_BACKEND_URL}/profile/${id}`,request, { + headers: { Authorization: `Bearer ${authStore.token}` }, - updateProfile: async (id, token) => { - return axios.delete(`${import.meta.env.VITE_BACKEND_URL}/profile/{id}`, { - headers: { Authorization: `Bearer ${token}` }, }) .then((response) => { + //authStore.setProfile(response.data) return response.data; }) - .catch(() => { - throw new Error("Account not found or not accessible"); + .catch((error) => { + throw new Error("Error when updating profile: " + error); }); }, + + } diff --git a/src/views/ProfileSettings.vue b/src/views/ProfileSettings.vue index be448567b82c1a7551dd172b6d23fbb07c9f5cb8..588c0d180ff674ba614ca8ece269144eb3f662e7 100644 --- a/src/views/ProfileSettings.vue +++ b/src/views/ProfileSettings.vue @@ -118,7 +118,7 @@ export default { computed: { ...mapStores(useAuthStore), profile() { - const store = useAuthStore(); + let store = useAuthStore(); this.currentProfile.name = store.profile.name; this.currentProfile.isRestricted = store.profile.restricted; this.currentProfile.image = store.profile.profileImageUrl; @@ -152,14 +152,23 @@ export default { }, methods: { saveUserSettings(){ //profilnavn, profilbilde, profilrestriksjoner - console.log(this.currentProfile.image) - alert("(Denne knappen gjør ingen ting) brukerinnstillinger lagret!") + const namee = this.currentProfile.name; + const isRestrictede = this.currentProfile.isRestricted; + const profileImageUrle = this.currentProfile.image; + API.updateProfile( - this.user.id, - this.profile.name, - this.profile.isRestricted, - this.profile.image, - ); + this.profile.id,{ + name:namee, + profileImageUrl: profileImageUrle, + restricted: isRestrictede, + } + ).then((savedProfile)=>{ + useAuthStore().setProfile(savedProfile); + + }).catch((error)=> { + console.log(error) + }) + }, chooseProfilePicture(){ alert("velgBilde") @@ -171,11 +180,6 @@ export default { alert("(Denne knappen gjør ingen ting) konto oppdatert") } - - /*API.updateAccount( - this.user.id, - this.user.password, - token);*/ }, deleteAccount(){ if(this.deletionConfirmation===false){