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 IngredientComponent from "@/components/IngredientComponent"
import {ApiMixin, StandardToasts} from "@/utils/utils"
import {ApiMixin} from "@/utils/utils"
Vue.use(BootstrapVue)
@ -71,27 +71,11 @@ export default {
},
data() {
return {
show_shopping: false,
shopping_list: [],
update_shopping: [],
selected_shoppingrecipe: undefined,
}
},
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: {

View File

@ -1,201 +1,188 @@
<template>
<div>
<b-modal hide-footer :id="`shopping_${this.modal_id}`" @show="loadRecipe" body-class="p-3 pt-0 pt-md-3">
<template v-slot:modal-title><h5>{{ $t("Add_Servings_to_Shopping", { servings: recipe_servings }) }}</h5></template>
<loading-spinner v-if="loading"></loading-spinner>
<div class="accordion" role="tablist" v-if="!loading">
<b-card no-body class="mb-1">
<b-card-header header-tag="header" class="p-0" role="tab">
<b-button block v-b-toggle.accordion-0 class="text-left" variant="outline-info">{{ recipe.name }}</b-button>
</b-card-header>
<b-collapse id="accordion-0" class="p-2" visible accordion="my-accordion" role="tabpanel">
<ingredients-card
:steps="steps"
:recipe="recipe.id"
:ingredient_factor="ingredient_factor"
:servings="recipe_servings"
:add_shopping_mode="true"
:recipe_list="list_recipe"
:header="false"
@add-to-shopping="addShopping($event)"
@starting-cart="add_shopping = $event"
/>
</b-collapse>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
<template v-for="r in related_recipes">
<b-card no-body class="mb-1" :key="r.recipe.id">
<b-card-header header-tag="header" class="p-1" role="tab">
<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-collapse :id="'accordion-' + r.recipe.id" accordion="my-accordion" role="tabpanel">
<ingredients-card
:steps="r.steps"
:recipe="r.recipe.id"
:ingredient_factor="ingredient_factor"
:servings="recipe_servings"
:add_shopping_mode="true"
:recipe_list="list_recipe"
:header="false"
@add-to-shopping="addShopping($event)"
@starting-cart="add_shopping = [...add_shopping, ...$event]"
/>
</b-collapse>
</b-card>
</template>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
</b-card>
</div>
<div>
<b-modal hide-footer :id="`shopping_${this.modal_id}`" @show="loadRecipe" body-class="p-3 pt-0 pt-md-3">
<template v-slot:modal-title><h5>{{ $t("Add_Servings_to_Shopping", {servings: recipe_servings}) }}</h5></template>
<loading-spinner v-if="loading"></loading-spinner>
<div class="accordion" role="tablist" v-if="!loading">
<b-card no-body class="mb-1">
<b-card-header header-tag="header" class="p-0" role="tab">
<b-button block v-b-toggle.accordion-0 class="text-left" variant="outline-info">{{ recipe.name }}</b-button>
</b-card-header>
<b-collapse id="accordion-0" class="p-2" visible accordion="my-accordion" role="tabpanel">
<div>
<b-input-group>
<b-input-group-prepend is-text class="d-inline-flex">
<i class="fa fa-calculator"></i>
</b-input-group-prepend>
<b-form-spinbutton min="1" v-model="recipe_servings" inline style="height: 3em"></b-form-spinbutton>
<!-- <CustomInputSpinButton v-model.number="recipe_servings" style="height: 3em" /> -->
<div v-for="i in steps.flatMap(s => s.ingredients)" v-bind:key="i.id">
<table class="table table-sm mb-0">
<b-input-group-append class="d-inline-flex">
<b-button variant="success" @click="saveShopping" class="d-none d-lg-inline-block">{{ $t("Save") }} </b-button>
<b-button variant="success" @click="saveShopping" class="d-inline-block d-lg-none"><i class="fa fa-cart-plus"></i></b-button>
</b-input-group-append>
</b-input-group>
</div>
</b-modal>
</div>
<ingredient-component
:use_plural="true"
:key="i.id"
:detailed="true"
:ingredient="i"
:ingredient_factor="ingredient_factor"
@checked-state-changed="$set(i, 'checked', !i.checked)"
/>
</table>
</div>
</b-collapse>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
<template v-for="r in related_recipes">
<b-card no-body class="mb-1" :key="r.recipe.id">
<b-card-header header-tag="header" class="p-1" role="tab">
<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-collapse :id="'accordion-' + r.recipe.id" accordion="my-accordion" role="tabpanel">
<div v-for="i in r.steps.flatMap(s => s.ingredients)" v-bind:key="i.id">
<table class="table table-sm mb-0">
<ingredient-component
:use_plural="true"
:key="i.id"
:detailed="true"
:ingredient="i"
:ingredient_factor="ingredient_factor"
@checked-state-changed="$set(i, 'checked', !i.checked)"
/>
</table>
</div>
</b-collapse>
</b-card>
</template>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
</b-card>
</div>
<div>
<b-input-group>
<b-input-group-prepend is-text class="d-inline-flex">
<i class="fa fa-calculator"></i>
</b-input-group-prepend>
<b-form-spinbutton min="1" v-model="recipe_servings" inline style="height: 3em"></b-form-spinbutton>
<!-- <CustomInputSpinButton v-model.number="recipe_servings" style="height: 3em" /> -->
<b-input-group-append class="d-inline-flex">
<b-button variant="success" @click="saveShopping" class="d-none d-lg-inline-block">{{ $t("Save") }}</b-button>
<b-button variant="success" @click="saveShopping" class="d-inline-block d-lg-none"><i class="fa fa-cart-plus"></i></b-button>
</b-input-group-append>
</b-input-group>
</div>
</b-modal>
</div>
</template>
<script>
import Vue from "vue"
import { BootstrapVue } from "bootstrap-vue"
import {BootstrapVue} from "bootstrap-vue"
Vue.use(BootstrapVue)
const { ApiApiFactory } = require("@/utils/openapi/api")
import { StandardToasts } from "@/utils/utils"
import IngredientsCard from "@/components/IngredientsCard"
const {ApiApiFactory} = require("@/utils/openapi/api")
import {StandardToasts} from "@/utils/utils"
import IngredientComponent from "@/components/IngredientComponent"
import LoadingSpinner from "@/components/LoadingSpinner"
// import CustomInputSpinButton from "@/components/CustomInputSpinButton"
export default {
name: "ShoppingModal",
components: { IngredientsCard, LoadingSpinner },
mixins: [],
props: {
recipe: { required: true, type: Object },
servings: { type: Number, default: undefined },
modal_id: { required: true, type: Number },
mealplan: { type: Number, default: undefined },
list_recipe: { type: Number, default: undefined },
},
data() {
return {
loading: true,
steps: [],
recipe_servings: undefined,
add_shopping: [],
related_recipes: [],
}
},
mounted() {
this.recipe_servings = this.servings
},
computed: {
ingredient_factor: function () {
return this.recipe_servings / this.recipe.servings
},
},
watch: {
servings: function (newVal) {
this.recipe_servings = parseInt(newVal)
},
},
methods: {
loadRecipe: function () {
this.add_shopping = []
this.related_recipes = []
let apiClient = new ApiApiFactory()
apiClient
.retrieveRecipe(this.recipe.id)
.then((result) => {
this.steps = result.data.steps
// ALERT: this will all break if ingredients are re-used between recipes
// ALERT: this also doesn't quite work right if the same recipe appears multiple time in the related recipes
if (!this.recipe_servings) {
this.recipe_servings = result.data?.servings
}
this.loading = false
})
.then(() => {
// get a list of related recipes
apiClient
.relatedRecipe(this.recipe.id)
.then((result) => {
return result.data
})
.then((related_recipes) => {
let promises = []
related_recipes.forEach((x) => {
promises.push(
apiClient.listSteps(x.id).then((recipe_steps) => {
this.related_recipes.push({
recipe: x,
steps: recipe_steps.data.results.filter((x) => x.ingredients.length > 0),
})
})
)
})
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 () {
// another choice would be to create ShoppingListRecipes for each recipe - this bundles all related recipe under the parent recipe
let shopping_recipe = {
id: this.recipe.id,
ingredients: this.add_shopping,
servings: this.recipe_servings,
mealplan: this.mealplan,
list_recipe: this.list_recipe,
}
let apiClient = new ApiApiFactory()
apiClient
.shoppingRecipe(this.recipe.id, shopping_recipe)
.then((result) => {
StandardToasts.makeStandardToast(this,StandardToasts.SUCCESS_CREATE)
this.$emit("finish")
})
.catch((err) => {
StandardToasts.makeStandardToast(this,StandardToasts.FAIL_CREATE, err)
})
name: "ShoppingModal",
components: {LoadingSpinner, IngredientComponent},
mixins: [],
props: {
recipe: {required: true, type: Object},
servings: {type: Number, default: undefined},
modal_id: {required: true, type: Number},
mealplan: {type: Number, default: undefined},
list_recipe: {type: Number, default: undefined},
},
data() {
return {
loading: true,
steps: [],
recipe_servings: undefined,
add_shopping: [],
related_recipes: [],
}
},
mounted() {
this.recipe_servings = this.servings
},
computed: {
ingredient_factor: function () {
return this.recipe_servings / this.recipe.servings
},
},
watch: {
servings: function (newVal) {
this.recipe_servings = parseInt(newVal)
},
},
methods: {
loadRecipe: function () {
this.add_shopping = []
this.related_recipes = []
let apiClient = new ApiApiFactory()
apiClient
.retrieveRecipe(this.recipe.id)
.then((result) => {
this.steps = result.data.steps
// ALERT: this will all break if ingredients are re-used between recipes
// ALERT: this also doesn't quite work right if the same recipe appears multiple time in the related recipes
if (!this.recipe_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
})
.then(() => {
// get a list of related recipes
apiClient
.relatedRecipe(this.recipe.id)
.then((result) => {
return result.data
})
.then((related_recipes) => {
let promises = []
related_recipes.forEach((x) => {
promises.push(
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({
recipe: x,
steps: sub_recipe_steps,
})
})
)
})
return Promise.all(promises)
})
})
},
saveShopping: function () {
// another choice would be to create ShoppingListRecipes for each recipe - this bundles all related recipe under the parent recipe
let shopping_recipe = {
id: this.recipe.id,
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,
mealplan: this.mealplan,
list_recipe: this.list_recipe,
}
let apiClient = new ApiApiFactory()
apiClient.shoppingRecipe(this.recipe.id, shopping_recipe)
.then((result) => {
StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE)
this.$emit("finish")
})
.catch((err) => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err)
})
this.$bvModal.hide(`shopping_${this.modal_id}`)
},
},
this.$bvModal.hide(`shopping_${this.modal_id}`)
},
},
}
</script>
<style>
.b-form-spinbutton.form-control {
background-color: #e9ecef;
border: 1px solid #ced4da;
background-color: #e9ecef;
border: 1px solid #ced4da;
}
</style>