Allowed for null products, move ingredient spreading server-side #2

Merged
jacob merged 6 commits from nullproduct into master 2025-07-28 23:22:59 +00:00
2 changed files with 11 additions and 16 deletions
Showing only changes of commit 67cbb370ed - Show all commits

View file

@ -56,11 +56,11 @@ export default {
}, },
methods: { methods: {
async updateShoppingList(save = false) { async updateShoppingList(save = false) {
const requests = save ? const new_ingredients = save ?
await data.saveMyShoppingList(this.ingredients) : await data.saveMyShoppingList(this.ingredients) :
await data.getMyShoppingList(); await data.getMyShoppingList();
this.ingredients = requests.map(r => r.ingredient); this.ingredients = new_ingredients;
}, },
addIngredient() { addIngredient() {
this.ingredients = [{ id: -1 }, ...this.ingredients]; this.ingredients = [{ id: -1 }, ...this.ingredients];

View file

@ -3,9 +3,8 @@ const BASE_URL = window.location.href.replace(/^(https?:\/\/[^/]+).*/, "$1/api")
const datesToFix = { const datesToFix = {
Meal: { fields: [ "suggested_date", "purchase_date", "consumed_date" ] }, Meal: { fields: [ "suggested_date", "purchase_date", "consumed_date" ] },
CurrentShoppingList: { dependants: l => ({ ShoppingListRequest: l.requests, ShoppingList: l.overlapping_previous_shops }) }, CurrentShoppingList: { dependants: l => ({ ShoppingListRequest: l.requests, ShoppingList: l.overlapping_previous_shops }) },
ShoppingList: { fields: [ "created_date", "purchased_date" ], dependants: l => ({ ShoppingListResult: l.results, ShoppingListRequest: l.requests }) }, ShoppingList: { fields: [ "created_date" ], dependants: l => ({ ShoppingListItem: l.items }) },
ShoppingListResult: { fields: [ "found_date", "created_date" ], }, ShoppingListItem: { fields: [ "created_date" ], dependants: r => ({ Meal: r.meal }) },
ShoppingListRequest: { fields: [ "created_date" ], dependants: r => ({ Meal: r.meal }) },
Recipe: { fields: [ "date_created", "date_hidden" ], }, Recipe: { fields: [ "date_created", "date_hidden" ], },
}; };
@ -213,10 +212,8 @@ export default {
}, },
async getMyShoppingList() { async getMyShoppingList() {
const response = await fetch(BASE_URL + "/shopping/current/me/ingredients", { credentials: "include" }); const response = await fetch(BASE_URL + "/shopping/current/me/ingredients", { credentials: "include" });
const requests = await response.json();
fixDates(requests, "ShoppingListRequest"); return await response.json();
return requests;
}, },
async saveMyShoppingList(list) { async saveMyShoppingList(list) {
const response = await fetch(BASE_URL + "/shopping/current/me/ingredients", { const response = await fetch(BASE_URL + "/shopping/current/me/ingredients", {
@ -228,10 +225,7 @@ export default {
body: JSON.stringify(list), body: JSON.stringify(list),
}); });
const requests = await response.json(); return await response.json();
fixDates(requests, "ShoppingListRequest");
return requests;
}, },
async getShoppingList(id) { async getShoppingList(id) {
const response = await fetch(BASE_URL + `/shopping/${encodeURIComponent(id)}`); const response = await fetch(BASE_URL + `/shopping/${encodeURIComponent(id)}`);
@ -273,7 +267,7 @@ export default {
}); });
const requests = await response.json(); const requests = await response.json();
fixDates(requests, "ShoppingListRequest"); fixDates(requests, "ShoppingListItem");
return requests; return requests;
}, },
@ -283,8 +277,9 @@ export default {
credentials: "include", credentials: "include",
}); });
const requests = await response.json(); if (!response.ok) {
fixDates(requests, "ShoppingListRequest"); throw new Error("Failed to unrequest meal");
}
}, },
async getPersonsInHome() { async getPersonsInHome() {
const response = await fetch(BASE_URL + "/persons"); const response = await fetch(BASE_URL + "/persons");