From ecd828008e49fbecb4733c7d9cdfff573e693485 Mon Sep 17 00:00:00 2001 From: AquaticLava Date: Tue, 1 Aug 2023 21:52:59 -0600 Subject: [PATCH] added auto shopping functionality. fixed bug when there are no matching recipes --- cookbook/serializer.py | 1 + cookbook/views/api.py | 4 ++- vue/src/apps/MealPlanView/MealPlanView.vue | 6 ++-- vue/src/components/AutoMealPlanModal.vue | 38 ++++++++++++++++++++-- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index 505f4e8b..15121d17 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -988,6 +988,7 @@ class AutoMealPlanSerializer(serializers.Serializer): keywords = KeywordSerializer(many=True) servings = CustomDecimalField() shared = UserSerializer(many=True, required=False, allow_null=True) + addshopping = serializers.BooleanField() class ShoppingListRecipeSerializer(serializers.ModelSerializer): diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 7ebe76c5..b3a8c762 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -688,6 +688,8 @@ class AutoPlanViewSet(viewsets.ViewSet): for keyword in keywords: recipes = recipes.filter(keywords__name=keyword['name']) + if len(recipes) == 0: + return Response(serializer.data) recipes = recipes.order_by('?')[:days] recipes = list(recipes) @@ -707,7 +709,7 @@ class AutoPlanViewSet(viewsets.ViewSet): for m in meal_plans: m.shared.set(shared_pks) - if request.data.get('addshopping', False) and request.data.get('recipe', None): + if request.data.get('addshopping', False): SLR = RecipeShoppingEditor(user=request.user, space=request.space) SLR.create(mealplan=m, servings=servings) diff --git a/vue/src/apps/MealPlanView/MealPlanView.vue b/vue/src/apps/MealPlanView/MealPlanView.vue index 4c5d3579..2763ac0d 100644 --- a/vue/src/apps/MealPlanView/MealPlanView.vue +++ b/vue/src/apps/MealPlanView/MealPlanView.vue @@ -372,7 +372,8 @@ export default { date: Date.now(), startDay: null, endDay: null, - shared: [] + shared: [], + addshopping: false }, showDate: new Date(), plan_entries: [], @@ -695,7 +696,8 @@ export default { "meal_type_id" : autoPlan.meal_types[mealTypeIndex].id, "keywords" : autoPlan.keywords[mealTypeIndex], "servings" : autoPlan.servings, - "shared" : autoPlan.shared + "shared" : autoPlan.shared, + "addshopping": autoPlan.addshopping } await apiClient.createAutoPlanViewSet(data) diff --git a/vue/src/components/AutoMealPlanModal.vue b/vue/src/components/AutoMealPlanModal.vue index c559f32d..a2997c56 100644 --- a/vue/src/components/AutoMealPlanModal.vue +++ b/vue/src/components/AutoMealPlanModal.vue @@ -47,6 +47,12 @@ > {{ $t("Share") }} + + + {{ + $t("AddToShopping") + }} +
@@ -77,11 +83,14 @@ import {BootstrapVue} from "bootstrap-vue" import GenericMultiselect from "@/components/GenericMultiselect" import {ApiMixin} from "@/utils/utils" import {useUserPreferenceStore} from "@/stores/UserPreferenceStore"; +import VueCookies from "vue-cookies"; const { ApiApiFactory } = require("@/utils/openapi/api") const { StandardToasts } = require("@/utils/utils") Vue.use(BootstrapVue) +Vue.use(VueCookies) +let MEALPLAN_COOKIE_NAME = "mealplan_settings" export default { name: "AutoMealPlanModal", @@ -106,16 +115,38 @@ export default { date: Date.now(), startDay: null, endDay: null, - shared: [] - } + shared: [], + addshopping: false + }, + mealplan_settings: { + addshopping: false, + } } }, - mounted: function () {}, + watch: { + mealplan_settings: { + handler(newVal) { + this.$cookies.set(MEALPLAN_COOKIE_NAME, this.mealplan_settings) + }, + deep: true, + }, + }, + mounted: function () { + useUserPreferenceStore().updateIfStaleOrEmpty() + }, + computed: { + autoMealPlan: function () { + return useUserPreferenceStore().getStaleData()?.mealplan_autoadd_shopping + }, + }, methods: { genericSelectChanged: function (obj) { this.AutoPlan.keywords[obj.var] = obj.val }, showModal() { + if (this.$cookies.isKey(MEALPLAN_COOKIE_NAME)) { + this.mealplan_settings = Object.assign({}, this.mealplan_settings, this.$cookies.get(MEALPLAN_COOKIE_NAME)) + } this.refreshMealTypes() this.AutoPlan.servings = 1 @@ -164,6 +195,7 @@ export default { }, createPlan() { this.$bvModal.hide(`autoplan-modal`) + this.AutoPlan.addshopping = this.mealplan_settings.addshopping this.$emit("create-plan", this.AutoPlan) }, updateStartDay(date){