review shopping list in MealPlan modal

This commit is contained in:
Chris Scoggins 2022-01-29 14:10:14 -06:00
parent a7796cbf5c
commit e00794bbdf
No known key found for this signature in database
GPG Key ID: 41617A4206CCBAC6
7 changed files with 54 additions and 15 deletions

View File

@ -8,7 +8,7 @@ from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from cookbook.helper.HelperFunctions import Round, str2bool from cookbook.helper.HelperFunctions import Round, str2bool
from cookbook.models import (Ingredient, ShoppingListEntry, ShoppingListRecipe, from cookbook.models import (Ingredient, MealPlan, Recipe, ShoppingListEntry, ShoppingListRecipe,
SupermarketCategoryRelation) SupermarketCategoryRelation)
from recipes import settings from recipes import settings
@ -45,6 +45,8 @@ class RecipeShoppingEditor():
self._kwargs = {**kwargs} self._kwargs = {**kwargs}
self.mealplan = self._kwargs.get('mealplan', None) 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)
self.id = self._kwargs.get('id', None) self.id = self._kwargs.get('id', None)
self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space) self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space)
@ -55,6 +57,8 @@ class RecipeShoppingEditor():
self.created_by = getattr(self._shopping_list_recipe.entries.first(), 'created_by', self.created_by) self.created_by = getattr(self._shopping_list_recipe.entries.first(), 'created_by', self.created_by)
self.recipe = getattr(self._shopping_list_recipe, 'recipe', None) or self._kwargs.get('recipe', None) or getattr(self.mealplan, 'recipe', None) self.recipe = getattr(self._shopping_list_recipe, 'recipe', None) or self._kwargs.get('recipe', None) or getattr(self.mealplan, 'recipe', None)
if type(self.recipe) in [int, float]:
self.recipe = Recipe.objects.filter(id=self.recipe, space=self.space)
try: try:
self.servings = float(self._kwargs.get('servings', None)) self.servings = float(self._kwargs.get('servings', None))

View File

@ -644,7 +644,6 @@ class RecipeViewSet(viewsets.ModelViewSet):
schema = QueryParamAutoSchema() schema = QueryParamAutoSchema()
def get_queryset(self): def get_queryset(self):
if self.detail: if self.detail:
self.queryset = self.queryset.filter(space=self.request.space) self.queryset = self.queryset.filter(space=self.request.space)
return super().get_queryset() return super().get_queryset()
@ -718,7 +717,8 @@ class RecipeViewSet(viewsets.ModelViewSet):
ingredients = request.data.get('ingredients', None) ingredients = request.data.get('ingredients', None)
servings = request.data.get('servings', None) servings = request.data.get('servings', None)
list_recipe = request.data.get('list_recipe', None) list_recipe = request.data.get('list_recipe', None)
SLR = RecipeShoppingEditor(request.user, request.space, id=list_recipe, recipe=obj) mealplan = request.data.get('mealplan', None)
SLR = RecipeShoppingEditor(request.user, request.space, id=list_recipe, recipe=obj, mealplan=mealplan)
content = {'msg': _(f'{obj.name} was added to the shopping list.')} content = {'msg': _(f'{obj.name} was added to the shopping list.')}
http_status = status.HTTP_204_NO_CONTENT http_status = status.HTTP_204_NO_CONTENT

View File

@ -35,7 +35,7 @@ export default {
// this.Models and this.Actions inherited from ApiMixin // this.Models and this.Actions inherited from ApiMixin
loading: false, loading: false,
objects: [], objects: [],
selected_objects: [], selected_objects: undefined,
} }
}, },
props: { props: {
@ -80,7 +80,7 @@ export default {
this.selected_objects = newVal this.selected_objects = newVal
}, },
clear: function (newVal, oldVal) { clear: function (newVal, oldVal) {
if (this.multiple) { if (this.multiple || !this.initial_single_selection) {
this.selected_objects = [] this.selected_objects = []
} else { } else {
this.selected_objects = undefined this.selected_objects = undefined
@ -100,10 +100,10 @@ export default {
return this.placeholder || this.model.name || this.$t("Search") return this.placeholder || this.model.name || this.$t("Search")
}, },
nothingSelected() { nothingSelected() {
if (this.multiple) { if (this.multiple || !this.initial_single_selection) {
return this.selected_objects.length === 0 && this.initial_selection.length === 0 return this.selected_objects.length === 0 && this.initial_selection.length === 0
} else { } else {
return !this.selected_objects && !this.initial_selection return !this.selected_objects && !this.initial_single_selection
} }
}, },
}, },

View File

@ -76,9 +76,13 @@
<small tabindex="-1" class="form-text text-muted">{{ $t("Share") }}</small> <small tabindex="-1" class="form-text text-muted">{{ $t("Share") }}</small>
</b-form-group> </b-form-group>
<b-input-group v-if="!autoMealPlan"> <b-input-group v-if="!autoMealPlan">
<b-form-checkbox id="AddToShopping" v-model="entryEditing.addshopping" /> <b-form-checkbox id="AddToShopping" v-model="mealplan_settings.addshopping" />
<small tabindex="-1" class="form-text text-muted">{{ $t("AddToShopping") }}</small> <small tabindex="-1" class="form-text text-muted">{{ $t("AddToShopping") }}</small>
</b-input-group> </b-input-group>
<b-input-group v-if="mealplan_settings.addshopping">
<b-form-checkbox id="reviewShopping" v-model="mealplan_settings.reviewshopping" />
<small tabindex="-1" class="form-text text-muted">{{ $t("review_shopping") }}</small>
</b-input-group>
</div> </div>
<div class="col-lg-6 d-none d-lg-block d-xl-block"> <div class="col-lg-6 d-none d-lg-block d-xl-block">
<recipe-card v-if="entryEditing.recipe" :recipe="entryEditing.recipe" :detailed="false"></recipe-card> <recipe-card v-if="entryEditing.recipe" :recipe="entryEditing.recipe" :detailed="false"></recipe-card>
@ -99,6 +103,7 @@
<script> <script>
import Vue from "vue" import Vue from "vue"
import VueCookies from "vue-cookies"
import { BootstrapVue } from "bootstrap-vue" import { BootstrapVue } from "bootstrap-vue"
import GenericMultiselect from "@/components/GenericMultiselect" import GenericMultiselect from "@/components/GenericMultiselect"
import { ApiMixin, getUserPreference } from "@/utils/utils" import { ApiMixin, getUserPreference } from "@/utils/utils"
@ -107,6 +112,8 @@ const { ApiApiFactory } = require("@/utils/openapi/api")
const { StandardToasts } = require("@/utils/utils") const { StandardToasts } = require("@/utils/utils")
Vue.use(BootstrapVue) Vue.use(BootstrapVue)
Vue.use(VueCookies)
let MEALPLAN_COOKIE_NAME = "mealplan_settings"
export default { export default {
name: "MealPlanEditModal", name: "MealPlanEditModal",
@ -134,13 +141,17 @@ export default {
missing_recipe: false, missing_recipe: false,
missing_meal_type: false, missing_meal_type: false,
default_plan_share: [], default_plan_share: [],
mealplan_settings: {
addshopping: false,
reviewshopping: false,
},
} }
}, },
watch: { watch: {
entry: { entry: {
handler() { handler() {
this.entryEditing = Object.assign({}, this.entry) this.entryEditing = Object.assign({}, this.entry)
console.log("entryEditing", this.entryEditing)
if (this.entryEditing_inital_servings) { if (this.entryEditing_inital_servings) {
this.entryEditing.servings = this.entryEditing_inital_servings this.entryEditing.servings = this.entryEditing_inital_servings
} }
@ -151,6 +162,12 @@ export default {
handler(newVal) {}, handler(newVal) {},
deep: true, deep: true,
}, },
mealplan_settings: {
handler(newVal) {
this.$cookies.set(MEALPLAN_COOKIE_NAME, this.mealplan_settings)
},
deep: true,
},
entryEditing_inital_servings: function (newVal) { entryEditing_inital_servings: function (newVal) {
this.entryEditing.servings = newVal this.entryEditing.servings = newVal
}, },
@ -163,6 +180,9 @@ export default {
}, },
methods: { methods: {
showModal() { showModal() {
if (this.$cookies.isKey(MEALPLAN_COOKIE_NAME)) {
this.mealplan_settings = Object.assign({}, this.mealplan_settings, this.$cookies.get(MEALPLAN_COOKIE_NAME))
}
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
apiClient.listUserPreferences().then((result) => { apiClient.listUserPreferences().then((result) => {
@ -185,8 +205,10 @@ export default {
cancel = true cancel = true
} }
if (!cancel) { if (!cancel) {
console.log("saving", { ...this.mealplan_settings, ...this.entryEditing })
this.$bvModal.hide(`edit-modal`) this.$bvModal.hide(`edit-modal`)
this.$emit("save-entry", this.entryEditing) this.$emit("save-entry", { ...this.mealplan_settings, ...this.entryEditing })
console.log("after emit", { ...this.mealplan_settings, ...this.entryEditing }.addshopping)
} }
}, },
deleteEntry() { deleteEntry() {

View File

@ -80,6 +80,7 @@ export default {
recipe: { required: true, type: Object }, recipe: { required: true, type: Object },
servings: { type: Number, default: undefined }, servings: { type: Number, default: undefined },
modal_id: { required: true, type: Number }, modal_id: { required: true, type: Number },
mealplan: { type: Number, default: undefined },
}, },
data() { data() {
return { return {
@ -181,6 +182,7 @@ export default {
id: this.recipe.id, id: this.recipe.id,
ingredients: this.add_shopping, ingredients: this.add_shopping,
servings: this.recipe_servings, servings: this.recipe_servings,
mealplan: this.mealplan,
} }
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
apiClient apiClient

View File

@ -49,7 +49,7 @@
<cook-log :recipe="recipe" :modal_id="modal_id"></cook-log> <cook-log :recipe="recipe" :modal_id="modal_id"></cook-log>
<add-recipe-to-book :recipe="recipe" :modal_id="modal_id" :entryEditing_inital_servings="servings_value"></add-recipe-to-book> <add-recipe-to-book :recipe="recipe" :modal_id="modal_id" :entryEditing_inital_servings="servings_value"></add-recipe-to-book>
<shopping-modal :recipe="recipe" :servings="servings_value" :modal_id="modal_id" /> <shopping-modal :recipe="recipe" :servings="servings_value" :modal_id="modal_id" :mealplan="undefined" />
<b-modal :id="`modal-share-link_${modal_id}`" v-bind:title="$t('Share')" hide-footer> <b-modal :id="`modal-share-link_${modal_id}`" v-bind:title="$t('Share')" hide-footer>
<div class="row"> <div class="row">
@ -64,10 +64,8 @@
</b-modal> </b-modal>
<meal-plan-edit-modal <meal-plan-edit-modal
v-if="entryEditing"
:entry="entryEditing" :entry="entryEditing"
:entryEditing_inital_servings="servings_value" :entryEditing_inital_servings="servings_value"
:entry-editing_initial_meal_type="[]"
@save-entry="saveMealPlan" @save-entry="saveMealPlan"
:modal_id="`modal-meal-plan_${modal_id}`" :modal_id="`modal-meal-plan_${modal_id}`"
:allow_delete="false" :allow_delete="false"
@ -118,6 +116,7 @@ export default {
}, },
}, },
entryEditing: {}, entryEditing: {},
mealplan: undefined,
} }
}, },
props: { props: {
@ -147,12 +146,20 @@ export default {
}, },
saveMealPlan: function (entry) { saveMealPlan: function (entry) {
entry.date = moment(entry.date).format("YYYY-MM-DD") entry.date = moment(entry.date).format("YYYY-MM-DD")
let reviewshopping = entry.addshopping && entry.reviewshopping
entry.addshopping = entry.addshopping && !entry.reviewshopping
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
apiClient apiClient
.createMealPlan(entry) .createMealPlan(entry)
.then((result) => { .then((result) => {
this.$bvModal.hide(`modal-meal-plan_${this.modal_id}`) this.$bvModal.hide(`modal-meal-plan_${this.modal_id}`)
console.log(entry)
if (reviewshopping) {
this.mealplan = result.data.id
this.servings_value = result.data.servings
this.addToShopping()
}
StandardToasts.makeStandardToast(StandardToasts.SUCCESS_CREATE) StandardToasts.makeStandardToast(StandardToasts.SUCCESS_CREATE)
}) })
.catch((error) => { .catch((error) => {
@ -163,7 +170,9 @@ export default {
this.entryEditing = this.options.entryEditing this.entryEditing = this.options.entryEditing
this.entryEditing.recipe = this.recipe this.entryEditing.recipe = this.recipe
this.entryEditing.date = moment(new Date()).format("YYYY-MM-DD") this.entryEditing.date = moment(new Date()).format("YYYY-MM-DD")
this.$bvModal.show(`modal-meal-plan_${this.modal_id}`) this.$nextTick(function () {
this.$bvModal.show(`modal-meal-plan_${this.modal_id}`)
})
}, },
createShareLink: function () { createShareLink: function () {
axios axios
@ -192,6 +201,7 @@ export default {
navigator.share(shareData) navigator.share(shareData)
}, },
addToShopping() { addToShopping() {
console.log("opening shopping modal")
this.$bvModal.show(`shopping_${this.modal_id}`) this.$bvModal.show(`shopping_${this.modal_id}`)
}, },
}, },

View File

@ -294,5 +294,6 @@
"ignore_shopping_help": "Never add food to the shopping list (e.g. water)", "ignore_shopping_help": "Never add food to the shopping list (e.g. water)",
"shopping_category_help": "Supermarkets can be ordered and filtered by Shopping Category according to the layout of the aisles.", "shopping_category_help": "Supermarkets can be ordered and filtered by Shopping Category according to the layout of the aisles.",
"food_recipe_help": "Linking a recipe here will include the linked recipe in any other recipe that use this food", "food_recipe_help": "Linking a recipe here will include the linked recipe in any other recipe that use this food",
"Foods":"Foods" "Foods": "Foods",
"review_shopping": "Review shopping entries before saving"
} }