My shopping list page

This commit is contained in:
jableader 2024-05-18 14:46:27 +10:00
parent 5279521416
commit 895af5bc75
3 changed files with 43 additions and 56 deletions

View file

@ -98,9 +98,7 @@ export default {
}, },
toggleEditing() { toggleEditing() {
this.editing = !this.editing; this.editing = !this.editing;
if (this.editing && !this.ingredients.length) { this.$emit('on-editing', this.editing);
this.$emit('on-add');
}
} }
} }
} }

View file

@ -1,9 +1,7 @@
<template> <template>
<div> <div>
<h1>My Shopping List</h1> <h1>My Shopping List</h1>
<editable-ingredients-panel @on-add="addIngredient" @on-delete="deleteIngredient" @on-update-ingredient="updateIngredient" :ingredients="mySources" /> <editable-ingredients-panel @on-add="addIngredient" @on-delete="deleteIngredient" @on-update-ingredient="updateIngredient" @on-editing="onEditing" :ingredients="ingredients" />
</div> </div>
<!-- <!--
@ -40,33 +38,12 @@
import data from '@/data.js' import data from '@/data.js'
import EditableIngredientsPanel from '@/components/ingredients/EditableIngredientsPanel.vue' import EditableIngredientsPanel from '@/components/ingredients/EditableIngredientsPanel.vue'
import ShoppingListItem from './ShoppingListItem.vue'
import MealSelectionList from './MealSelectionList.vue'
import { mealToShoppingListSources, groupByProduct } from './shopping.js'
export default { export default {
name: 'MyShoppingpage', name: 'MyShoppingpage',
components: { ShoppingListItem, MealSelectionList, EditableIngredientsPanel }, components: { EditableIngredientsPanel },
data() { data() {
const from = new Date(); return { ingredients: [], person: null }
from.setTime(0);
const to = new Date();
to.setDate(to.getDate() + 7);
return { from, to, shoppingList: [], sources: [], mySources: [], includedMeals: [], meals: [], person: null }
},
watch: {
sources() {
this.shoppingList = groupByProduct(this.sources);
},
includedMeals() {
this.updateSources();
},
mySources() {
this.updateSources();
}
}, },
async beforeMount() { async beforeMount() {
const person = await data.currentUser(); const person = await data.currentUser();
@ -74,43 +51,31 @@ export default {
return this.$router.push({ name: 'login' }); return this.$router.push({ name: 'login' });
this.person = person; this.person = person;
await this.updateShoppingList();
this.meals = await data.getMeals(this.from, this.to);
// this.includedMeals = this.meals;
}, },
methods: { methods: {
updateSources() { async updateShoppingList(save = false) {
const person = this.person; const requests = save ?
if (!person) return; await data.saveMyShoppingList(this.ingredients) :
await data.getMyShoppingList();
const mealSources = this.includedMeals.flatMap(mealToShoppingListSources); this.ingredients = requests.map(r => r.ingredient);
const personSources = this.mySources.map(ingredient => ({ ingredient, person }));
this.sources = [...mealSources, ...personSources];
},
mealSelected(meal) {
this.includedMeals = [...this.includedMeals, meal];
},
mealUnselected(meal) {
this.includedMeals = this.includedMeals.filter(m => m !== meal);
}, },
addIngredient() { addIngredient() {
const person = this.person; this.ingredients = [{ id: 0 }, ...this.ingredients];
if (!person) return;
this.myRequestedItems = [{ id: 0 }, ...this.mySources];
}, },
deleteIngredient(ingredient) { deleteIngredient(ingredient) {
const person = this.person; this.ingredients = this.ingredients.filter(i => i !== ingredient);
if (!person) return;
this.myRequestedItems = this.mySources.filter(source => source !== ingredient);
}, },
updateIngredient(oldIngredient, newIngredient) { updateIngredient(oldIngredient, newIngredient) {
const person = this.person; this.ingredients = this.ingredients.map(source => source === oldIngredient ? newIngredient : source);
if (!person) return; },
async onEditing(isStartingEdit) {
await this.updateShoppingList(!isStartingEdit);
this.myRequestedItems = this.mySources.map(source => source === oldIngredient ? newIngredient : source); if (isStartingEdit && this.ingredients.length === 0) {
this.addIngredient();
}
} }
} }
} }

View file

@ -136,4 +136,28 @@ export default {
const response = await fetch(BASE_URL + "/persons?q=" + encodeURIComponent(name)); const response = await fetch(BASE_URL + "/persons?q=" + encodeURIComponent(name));
return await response.json(); return await response.json();
}, },
async getMyShoppingList() {
const response = await fetch(BASE_URL + "/shopping/current/me", { credentials: "include" });
return await response.json();
},
async saveMyShoppingList(list) {
const response = await fetch(BASE_URL + "/shopping/current/me", {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(list),
});
return await response.json();
},
async getShoppingList(id) {
const response = await fetch(BASE_URL + `/shopping/${encodeURIComponent(id)}`);
return await response.json();
},
async getCurrentShoppingList() {
const response = await fetch(BASE_URL + "/shopping/current");
return await response.json();
}
} }