diff --git a/cookbook/helper/shopping_helper.py b/cookbook/helper/shopping_helper.py index 1783ca42..3a91a194 100644 --- a/cookbook/helper/shopping_helper.py +++ b/cookbook/helper/shopping_helper.py @@ -44,9 +44,11 @@ class RecipeShoppingEditor(): self.space = space self._kwargs = {**kwargs} - self.mealplan = self._kwargs.get('mealplan', None) - if type(self.mealplan) in [int, float]: - self.mealplan = MealPlan.objects.filter(id=self.mealplan, space=self.space) + mealplan = self._kwargs.get('mealplan', None) + if type(mealplan) in [int, float]: + self.mealplan = MealPlan.objects.filter(id=self.mealplan, space=self.space).first() + if type(mealplan) == dict: + self.mealplan = MealPlan.objects.filter(id=mealplan['id'], space=self.space).first() self.id = self._kwargs.get('id', None) self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space) @@ -71,7 +73,7 @@ class RecipeShoppingEditor(): @property def _servings_factor(self): - return Decimal(self.servings)/Decimal(self._recipe_servings) + return Decimal(self.servings) / Decimal(self._recipe_servings) @property def _shared_users(self): @@ -88,9 +90,9 @@ class RecipeShoppingEditor(): def get_recipe_ingredients(self, id, exclude_onhand=False): if exclude_onhand: - return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space).exclude(food__onhand_users__id__in=[x.id for x in self._shared_users]) + return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space).exclude(food__onhand_users__id__in=[x.id for x in self._shared_users]) else: - return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space) + return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space) @property def _include_related(self): @@ -106,9 +108,8 @@ class RecipeShoppingEditor(): if servings := kwargs.get('servings', None): self.servings = float(servings) - if mealplan := kwargs.get('mealplan', None): - self.mealplan = mealplan - self.recipe = mealplan.recipe + if mealplan := kwargs.get('mealplan', None): # it appears this code is never called just init is used, no time to validate + self.mealplan = MealPlan.objects.filter(id=mealplan['id'], space=self.space).fist() elif recipe := kwargs.get('recipe', None): self.recipe = recipe @@ -195,7 +196,6 @@ class RecipeShoppingEditor(): ShoppingListEntry.objects.filter(id__in=to_delete).delete() self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space) - # # TODO refactor as class # def list_from_recipe(list_recipe=None, recipe=None, mealplan=None, servings=None, ingredients=None, created_by=None, space=None, append=False): # """ diff --git a/vue/src/apps/MealPlanView/MealPlanView.vue b/vue/src/apps/MealPlanView/MealPlanView.vue index 54775760..a1b24c4f 100644 --- a/vue/src/apps/MealPlanView/MealPlanView.vue +++ b/vue/src/apps/MealPlanView/MealPlanView.vue @@ -204,6 +204,7 @@ :entry="entryEditing" :modal_title="modal_title" :edit_modal_show="edit_modal_show" + :create_date="edit_modal_default_date" @reload-meal-types="refreshMealTypes" > @@ -322,29 +323,18 @@ export default { {text: this.$t("Year"), value: "year"}, ], displayPeriodCount: [1, 2, 3], - entryEditing: { - date: null, - id: -1, - meal_type: null, - note: "", - note_markdown: "", - recipe: null, - servings: 1, - shared: [], - title: "", - title_placeholder: this.$t("Title"), - }, }, shopping_list: [], current_period: null, - entryEditing: {}, + entryEditing: null, edit_modal_show: false, + edit_modal_default_date: null, ical_url: window.ICAL_URL, } }, computed: { modal_title: function () { - if (this.entryEditing.id === -1) { + if (this.entryEditing === null || this.entryEditing?.id === -1) { return this.$t("Create_Meal_Plan_Entry") } else { return this.$t("Edit_Meal_Plan_Entry") @@ -496,9 +486,11 @@ export default { this.showDate = d }, createEntryClick(data) { - this.entryEditing = this.options.entryEditing - this.entryEditing.date = moment(data).format("YYYY-MM-DD") - this.$bvModal.show(`edit-modal`) + this.edit_modal_default_date = moment(data).format("YYYY-MM-DD") + this.entryEditing = null + this.$nextTick(function () { + this.$bvModal.show(`edit-modal`) + }) }, findEntry(id) { return useMealPlanStore().plan_list.filter((entry) => { diff --git a/vue/src/components/MealPlanEditModal.vue b/vue/src/components/MealPlanEditModal.vue index 6bdc5a9c..ab1aa7e0 100644 --- a/vue/src/components/MealPlanEditModal.vue +++ b/vue/src/components/MealPlanEditModal.vue @@ -1,7 +1,7 @@ @@ -152,7 +152,7 @@ export default { name: "MealPlanEditModal", props: { entry: Object, - entryEditing_initial_servings: Number, + create_date: String, modal_title: String, modal_id: { type: String, @@ -171,7 +171,7 @@ export default { }, data() { return { - entryEditing: {}, + entryEditing: null, missing_recipe: false, missing_meal_type: false, default_plan_share: [], @@ -186,10 +186,6 @@ export default { entry: { handler() { this.entryEditing = Object.assign({}, this.entry) - - if (this.entryEditing_initial_servings) { - this.entryEditing.servings = this.entryEditing_initial_servings - } }, deep: true, }, @@ -204,9 +200,6 @@ export default { }, deep: true, }, - entryEditing_initial_servings: function (newVal) { - this.entryEditing.servings = newVal - }, }, mounted: function () { useUserPreferenceStore().updateIfStaleOrEmpty() @@ -222,8 +215,18 @@ export default { this.mealplan_settings = Object.assign({}, this.mealplan_settings, this.$cookies.get(MEALPLAN_COOKIE_NAME)) } + if (this.entry === null) { + this.entryEditing = useMealPlanStore().empty_meal_plan + } else { + this.entryEditing = this.entry + } + + if (this.create_date) { + this.entryEditing.date = this.create_date + } + useUserPreferenceStore().getData().then(userPreference => { - if (this.entry.id === -1) { + if (this.entryEditing.id === -1) { this.entryEditing.shared = userPreference.plan_share } }) @@ -248,9 +251,10 @@ export default { this.last_created_plan = r.data if (r.data.recipe && (this.mealplan_settings.addshopping || this.autoMealPlan) && this.mealplan_settings.reviewshopping) { console.log('OPENING SHOPPING MODAL', this.$bvModal) - this.$bvModal.show(`shopping_999999`) + this.$nextTick(function () { + this.$bvModal.show(`shopping_999999`) + }) } - //TODO cleanout entry editing without breaking default function from meal plan }) } else { console.log('CALLING UPDATE') diff --git a/vue/src/components/Modals/ShoppingModal.vue b/vue/src/components/Modals/ShoppingModal.vue index 8833ef0d..7d08da41 100644 --- a/vue/src/components/Modals/ShoppingModal.vue +++ b/vue/src/components/Modals/ShoppingModal.vue @@ -78,6 +78,7 @@ const {ApiApiFactory} = require("@/utils/openapi/api") import {StandardToasts} from "@/utils/utils" import IngredientComponent from "@/components/IngredientComponent" import LoadingSpinner from "@/components/LoadingSpinner" +import {useMealPlanStore} from "@/stores/MealPlanStore"; // import CustomInputSpinButton from "@/components/CustomInputSpinButton" export default { @@ -88,7 +89,7 @@ export default { recipe: {required: true, type: Object}, servings: {type: Number, default: undefined}, modal_id: {required: true, type: Number}, - mealplan: {type: Number, default: undefined}, + mealplan: {type: Object, default: undefined}, list_recipe: {type: Number, default: undefined}, }, data() { @@ -169,6 +170,9 @@ export default { .then((result) => { StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE) this.$emit("finish") + if (this.mealplan !== undefined && this.mealplan !== null){ + useMealPlanStore().plans[this.mealplan.id].shopping = true + } }) .catch((err) => { StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err)