allow filter in add to shopping modal

This commit is contained in:
vabene1111
2022-12-31 17:35:16 +01:00
parent 8307828528
commit 1a6e677c06
2 changed files with 171 additions and 200 deletions

View File

@@ -46,7 +46,7 @@ import {BootstrapVue} from "bootstrap-vue"
import "bootstrap-vue/dist/bootstrap-vue.css" import "bootstrap-vue/dist/bootstrap-vue.css"
import IngredientComponent from "@/components/IngredientComponent" import IngredientComponent from "@/components/IngredientComponent"
import {ApiMixin, StandardToasts} from "@/utils/utils" import {ApiMixin} from "@/utils/utils"
Vue.use(BootstrapVue) Vue.use(BootstrapVue)
@@ -71,27 +71,11 @@ export default {
}, },
data() { data() {
return { return {
show_shopping: false,
shopping_list: [],
update_shopping: [],
selected_shoppingrecipe: undefined,
} }
}, },
computed: { computed: {
ShoppingRecipes() {
// returns open shopping lists associated with this recipe
let recipe_in_list = this.shopping_list
.map((x) => {
return {
value: x?.list_recipe,
text: x?.recipe_mealplan?.name,
recipe: x?.recipe_mealplan?.recipe ?? 0,
servings: x?.recipe_mealplan?.servings,
}
})
.filter((x) => x?.recipe == this.recipe)
return [...new Map(recipe_in_list.map((x) => [x["value"], x])).values()] // filter to unique lists
},
}, },
watch: { watch: {

View File

@@ -9,17 +9,20 @@
<b-button block v-b-toggle.accordion-0 class="text-left" variant="outline-info">{{ recipe.name }}</b-button> <b-button block v-b-toggle.accordion-0 class="text-left" variant="outline-info">{{ recipe.name }}</b-button>
</b-card-header> </b-card-header>
<b-collapse id="accordion-0" class="p-2" visible accordion="my-accordion" role="tabpanel"> <b-collapse id="accordion-0" class="p-2" visible accordion="my-accordion" role="tabpanel">
<ingredients-card
:steps="steps" <div v-for="i in steps.flatMap(s => s.ingredients)" v-bind:key="i.id">
:recipe="recipe.id" <table class="table table-sm mb-0">
<ingredient-component
:use_plural="true"
:key="i.id"
:detailed="true"
:ingredient="i"
:ingredient_factor="ingredient_factor" :ingredient_factor="ingredient_factor"
:servings="recipe_servings" @checked-state-changed="$set(i, 'checked', !i.checked)"
:add_shopping_mode="true"
:recipe_list="list_recipe"
:header="false"
@add-to-shopping="addShopping($event)"
@starting-cart="add_shopping = $event"
/> />
</table>
</div>
</b-collapse> </b-collapse>
<!-- eslint-disable vue/no-v-for-template-key-on-child --> <!-- eslint-disable vue/no-v-for-template-key-on-child -->
<template v-for="r in related_recipes"> <template v-for="r in related_recipes">
@@ -28,17 +31,19 @@
<b-button btn-sm block v-b-toggle="'accordion-' + r.recipe.id" class="text-left" variant="outline-primary">{{ r.recipe.name }}</b-button> <b-button btn-sm block v-b-toggle="'accordion-' + r.recipe.id" class="text-left" variant="outline-primary">{{ r.recipe.name }}</b-button>
</b-card-header> </b-card-header>
<b-collapse :id="'accordion-' + r.recipe.id" accordion="my-accordion" role="tabpanel"> <b-collapse :id="'accordion-' + r.recipe.id" accordion="my-accordion" role="tabpanel">
<ingredients-card
:steps="r.steps" <div v-for="i in r.steps.flatMap(s => s.ingredients)" v-bind:key="i.id">
:recipe="r.recipe.id" <table class="table table-sm mb-0">
<ingredient-component
:use_plural="true"
:key="i.id"
:detailed="true"
:ingredient="i"
:ingredient_factor="ingredient_factor" :ingredient_factor="ingredient_factor"
:servings="recipe_servings" @checked-state-changed="$set(i, 'checked', !i.checked)"
:add_shopping_mode="true"
:recipe_list="list_recipe"
:header="false"
@add-to-shopping="addShopping($event)"
@starting-cart="add_shopping = [...add_shopping, ...$event]"
/> />
</table>
</div>
</b-collapse> </b-collapse>
</b-card> </b-card>
</template> </template>
@@ -67,17 +72,18 @@
<script> <script>
import Vue from "vue" import Vue from "vue"
import {BootstrapVue} from "bootstrap-vue" import {BootstrapVue} from "bootstrap-vue"
Vue.use(BootstrapVue) Vue.use(BootstrapVue)
const {ApiApiFactory} = require("@/utils/openapi/api") const {ApiApiFactory} = require("@/utils/openapi/api")
import {StandardToasts} from "@/utils/utils" import {StandardToasts} from "@/utils/utils"
import IngredientsCard from "@/components/IngredientsCard" import IngredientComponent from "@/components/IngredientComponent"
import LoadingSpinner from "@/components/LoadingSpinner" import LoadingSpinner from "@/components/LoadingSpinner"
// import CustomInputSpinButton from "@/components/CustomInputSpinButton" // import CustomInputSpinButton from "@/components/CustomInputSpinButton"
export default { export default {
name: "ShoppingModal", name: "ShoppingModal",
components: { IngredientsCard, LoadingSpinner }, components: {LoadingSpinner, IngredientComponent},
mixins: [], mixins: [],
props: { props: {
recipe: {required: true, type: Object}, recipe: {required: true, type: Object},
@@ -122,6 +128,7 @@ export default {
if (!this.recipe_servings) { if (!this.recipe_servings) {
this.recipe_servings = result.data?.servings this.recipe_servings = result.data?.servings
} }
this.steps.forEach(s => s.ingredients.filter(i => i.food.food_onhand === false).forEach(i => this.$set(i, 'checked', true)))
this.loading = false this.loading = false
}) })
.then(() => { .then(() => {
@@ -136,50 +143,30 @@ export default {
related_recipes.forEach((x) => { related_recipes.forEach((x) => {
promises.push( promises.push(
apiClient.listSteps(x.id).then((recipe_steps) => { apiClient.listSteps(x.id).then((recipe_steps) => {
let sub_recipe_steps = recipe_steps.data.results.filter((x) => x.ingredients.length > 0)
sub_recipe_steps.forEach(s => s.ingredients.filter(i => i.food.food_onhand === false).forEach(i => this.$set(i, 'checked', true)))
this.related_recipes.push({ this.related_recipes.push({
recipe: x, recipe: x,
steps: recipe_steps.data.results.filter((x) => x.ingredients.length > 0), steps: sub_recipe_steps,
}) })
}) })
) )
}) })
return Promise.all(promises) return Promise.all(promises)
}) })
// .then(() => {
// if (!this.list_recipe) {
// this.add_shopping = [
// ...this.add_shopping,
// ...this.related_recipes
// .map((x) => x.steps)
// .flat()
// .map((x) => x.ingredients)
// .flat()
// .filter((x) => !x.food.override_ignore)
// .map((x) => x.id),
// ]
// }
// })
}) })
}, },
addShopping: function (e) {
if (e.add) {
this.add_shopping.push(e.item.id)
} else {
this.add_shopping = this.add_shopping.filter((x) => x !== e.item.id)
}
},
saveShopping: function () { saveShopping: function () {
// another choice would be to create ShoppingListRecipes for each recipe - this bundles all related recipe under the parent recipe // another choice would be to create ShoppingListRecipes for each recipe - this bundles all related recipe under the parent recipe
let shopping_recipe = { let shopping_recipe = {
id: this.recipe.id, id: this.recipe.id,
ingredients: this.add_shopping, ingredients: this.steps.flatMap(s => s.ingredients.filter(i => i.checked === true).flatMap(i => i.id)).concat(this.related_recipes.flatMap(r => r.steps.flatMap(rs => rs.ingredients.filter(i => i.checked === true).flatMap(i => i.id)))),
servings: this.recipe_servings, servings: this.recipe_servings,
mealplan: this.mealplan, mealplan: this.mealplan,
list_recipe: this.list_recipe, list_recipe: this.list_recipe,
} }
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
apiClient apiClient.shoppingRecipe(this.recipe.id, shopping_recipe)
.shoppingRecipe(this.recipe.id, shopping_recipe)
.then((result) => { .then((result) => {
StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE) StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE)
this.$emit("finish") this.$emit("finish")