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

fjernet fridge som eget store. oppdaterte metoder

parent 3ead56bb
No related branches found
No related tags found
1 merge request!20Fridge updates
...@@ -6,7 +6,8 @@ export const useAuthStore = defineStore("auth", { ...@@ -6,7 +6,8 @@ export const useAuthStore = defineStore("auth", {
account: {}, account: {},
profile: {}, profile: {},
profiles: [], profiles: [],
items: {ingredientList: [], suggestionList: []} items: {ingredientList: [], suggestionList: []},
fridgeItems: [],
}; };
}, },
persist: { persist: {
...@@ -38,6 +39,12 @@ export const useAuthStore = defineStore("auth", { ...@@ -38,6 +39,12 @@ export const useAuthStore = defineStore("auth", {
}, },
setItem(item) { setItem(item) {
this.items.push(item); this.items.push(item);
},
setFridge(fridgeItems){
this.fridgeItems=fridgeItems;
},
addItemToFridge(fridgeItem){
this.fridgeItems.push(fridgeItem);
} }
} }
}); });
...@@ -183,6 +183,7 @@ export const API = { ...@@ -183,6 +183,7 @@ export const API = {
headers: { Authorization: `Bearer ${authStore.token}` }, headers: { Authorization: `Bearer ${authStore.token}` },
}) })
.then((response) => { .then((response) => {
authStore.setFridge(response.data)
return response.data; return response.data;
}).catch(() => { }).catch(() => {
throw new Error("Could not fetch fridge items"); throw new Error("Could not fetch fridge items");
...@@ -233,6 +234,7 @@ export const API = { ...@@ -233,6 +234,7 @@ export const API = {
axios.put(`${import.meta.env.VITE_BACKEND_URL}/fridge/ingredientsAmount`, request,{ axios.put(`${import.meta.env.VITE_BACKEND_URL}/fridge/ingredientsAmount`, request,{
headers: { Authorization: `Bearer ${authStore.token}` }, headers: { Authorization: `Bearer ${authStore.token}` },
}).then((response) => { }).then((response) => {
authStore.setFridge(response.data);
return response.data; return response.data;
}).catch(()=> { }).catch(()=> {
throw new Error("Could modify ingredient. "); throw new Error("Could modify ingredient. ");
...@@ -256,22 +258,6 @@ export const API = { ...@@ -256,22 +258,6 @@ export const API = {
}) })
}, },
//returns fridgeItem of specific id
getFridgeItem: async (id) =>{
const authStore = useAuthStore();
axios.get(`${import.meta.env.VITE_BACKEND_URL}/fridge/${id}`, {
headers: { Authorization: `Bearer ${authStore.token}` },
})
.then((response) => {
return response.data;
})
.catch(() => {
throw new Error("Could not fetch fridge item");
});
},
/** /**
* Getter for the shopping list of a logged in account * Getter for the shopping list of a logged in account
* @returns a promise that resolves to an array of Ingredient objects representing the shopping list, or an error if it fails * @returns a promise that resolves to an array of Ingredient objects representing the shopping list, or an error if it fails
...@@ -402,6 +388,9 @@ export const API = { ...@@ -402,6 +388,9 @@ export const API = {
}, },
/** /**
......
...@@ -31,14 +31,13 @@ export default { ...@@ -31,14 +31,13 @@ export default {
name: "FridgeView", name: "FridgeView",
components: {ItemSearch, EatFridgeItemModal, FridgeItem}, components: {ItemSearch, EatFridgeItemModal, FridgeItem},
computed:{ computed:{
...mapState(useAuthStore, ['account']), ...mapState(useAuthStore, ['account', 'fridgeItems']),
...mapStores(useFridgeStore),
}, },
data() { data() {
return { return {
visible: false, //is the useitemModal visible visible: false, //is the useitemModal visible
selectedItem: null, selectedItem: null,
fridgeItems: [], //fridgeItems: [],
searchVisible: false, searchVisible: false,
fridgeMsg: "" fridgeMsg: ""
} }
...@@ -63,14 +62,14 @@ export default { ...@@ -63,14 +62,14 @@ export default {
}, },
async updateFridge(addedItem) { async updateFridge(addedItem) {
this.fridgeItems = await API.getFridgeItems(); this.fridgeItems = await API.getFridgeItems();
this.fridgeItems = await API.getFridgeItems();
this.hideItemSearch(); this.hideItemSearch();
this.updateFridgeMessage(addedItem.name + " ble lagt i kjøleskapet.") this.updateFridgeMessage(addedItem.name + " ble lagt i kjøleskapet.")
}, },
}, },
async mounted() { async mounted() {
this.fridgeItems = await API.getFridgeItems(); await API.getFridgeItems()
if(this.fridgeItems.length===0){ if(this.fridgeItems.length===0){
this.fridgeMsg="Kjøleskapet ditt er tomt." this.fridgeMsg="Kjøleskapet ditt er tomt."
} }
......
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