Make it possible to add recipes from /recipes to the meal plan (closes #2003)

This commit is contained in:
Bernd Bestel
2022-12-26 14:25:33 +01:00
parent c60a1783da
commit 3f2ad17460
6 changed files with 146 additions and 8 deletions

View File

@@ -27,6 +27,7 @@
### Recipes ### Recipes
- Added a new entry "Add to meal plan" in the context/more menu per recipe to directly add a recipe to the meal plan from the recipes page
- Changed that when a ingredient has a "Variable amount" set, the text entered there now also replaces the unit when displaying the recipe (not only the amount as before) - Changed that when a ingredient has a "Variable amount" set, the text entered there now also replaces the unit when displaying the recipe (not only the amount as before)
- Fixed that hiding the "Requirements fulfilled" column (table options) on the recipes page didn't work - Fixed that hiding the "Requirements fulfilled" column (table options) on the recipes page didn't work
- Fixed that ingredient costs and calories were wrong when product substitution and unit conversions were involved at the same time - Fixed that ingredient costs and calories were wrong when product substitution and unit conversions were involved at the same time

View File

@@ -95,7 +95,7 @@ class RecipesController extends BaseController
$totalCalories = FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->calories; $totalCalories = FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->calories;
} }
$renderArray = [ $viewData = [
'recipes' => $recipes, 'recipes' => $recipes,
'recipesResolved' => $recipesResolved, 'recipesResolved' => $recipesResolved,
'recipePositionsResolved' => $this->getDatabase()->recipes_pos_resolved()->where('recipe_id', $selectedRecipe->id), 'recipePositionsResolved' => $this->getDatabase()->recipes_pos_resolved()->where('recipe_id', $selectedRecipe->id),
@@ -106,7 +106,8 @@ class RecipesController extends BaseController
'userfieldValues' => $this->getUserfieldsService()->GetAllValues('recipes'), 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('recipes'),
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved(), 'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved(),
'selectedRecipeTotalCosts' => $totalCosts, 'selectedRecipeTotalCosts' => $totalCosts,
'selectedRecipeTotalCalories' => $totalCalories 'selectedRecipeTotalCalories' => $totalCalories,
'mealplanSections' => $this->getDatabase()->meal_plan_sections()->orderBy('sort_number')
]; ];
if ($selectedRecipe) if ($selectedRecipe)
@@ -137,12 +138,12 @@ class RecipesController extends BaseController
} }
} }
$renderArray['selectedRecipeSubRecipes'] = $selectedRecipeSubRecipes; $viewData['selectedRecipeSubRecipes'] = $selectedRecipeSubRecipes;
$renderArray['includedRecipeIdsAbsolute'] = $includedRecipeIdsAbsolute; $viewData['includedRecipeIdsAbsolute'] = $includedRecipeIdsAbsolute;
$renderArray['allRecipePositions'] = $allRecipePositions; $viewData['allRecipePositions'] = $allRecipePositions;
} }
return $this->renderPage($response, 'recipes', $renderArray); return $this->renderPage($response, 'recipes', $viewData);
} }
public function RecipeEditForm(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) public function RecipeEditForm(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)

View File

@@ -2350,3 +2350,9 @@ msgstr ""
msgid "This is the default quantity unit used when consuming this product" msgid "This is the default quantity unit used when consuming this product"
msgstr "" msgstr ""
msgid "Add to meal plan"
msgstr ""
msgid "Successfully added the recipe to the meal plan"
msgstr ""

View File

@@ -442,3 +442,58 @@ $(document).on('click', '.ingredient-done-button', function(e)
$(e.currentTarget).parent().toggleClass("text-strike-through"); $(e.currentTarget).parent().toggleClass("text-strike-through");
}); });
$(document).on("click", ".add-to-mealplan-button", function(e)
{
Grocy.Components.DateTimePicker.Init(true);
Grocy.Components.DateTimePicker.SetValue(moment().format("YYYY-MM-DD"));
Grocy.Components.RecipePicker.Clear();
$("#add-to-mealplan-modal").modal("show");
$('#recipe_id').val($(e.currentTarget).attr("data-recipe-id"));
$('#recipe_id').data('combobox').refresh();
$('#recipe_id').trigger('change');
Grocy.FrontendHelpers.ValidateForm("add-to-mealplan-form");
$("#recipe_servings").focus();
});
$('#save-add-to-mealplan-button').on('click', function(e)
{
e.preventDefault();
if (!Grocy.FrontendHelpers.ValidateForm("add-to-mealplan-form", true) || $(".combobox-menu-visible").length)
{
return false;
}
var formData = $('#add-to-mealplan-form').serializeJSON();
formData.day = Grocy.Components.DateTimePicker.GetValue();
Grocy.Api.Post('objects/meal_plan', formData,
function(result)
{
toastr.success(__t("Successfully added the recipe to the meal plan"));
$("#add-to-mealplan-modal").modal("hide");
},
function(xhr)
{
Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response)
}
);
});
$('#add-to-mealplan-form input').keydown(function(event)
{
if (event.keyCode === 13) // Enter
{
event.preventDefault();
if (!Grocy.FrontendHelpers.ValidateForm('add-to-mealplan-form'))
{
return false;
}
else
{
$("#save-add-to-mealplan-button").click();
}
}
});

View File

@@ -70,7 +70,6 @@
width: 200px !important; width: 200px !important;
} }
} }
</style> </style>
@endpush @endpush
@@ -244,7 +243,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="period_type">{{ $__t('Section') }}</label> <label for="section_id_note">{{ $__t('Section') }}</label>
<select class="custom-control custom-select" <select class="custom-control custom-select"
id="section_id_note" id="section_id_note"
name="section_id_note" name="section_id_note"

View File

@@ -156,6 +156,12 @@
<i class="fa-solid fa-ellipsis-v"></i> <i class="fa-solid fa-ellipsis-v"></i>
</button> </button>
<div class="table-inline-menu dropdown-menu dropdown-menu-right hide-on-fullscreen-card hide-when-embedded"> <div class="table-inline-menu dropdown-menu dropdown-menu-right hide-on-fullscreen-card hide-when-embedded">
<a class="dropdown-item add-to-mealplan-button"
type="button"
href="#"
data-recipe-id="{{ $recipe->id }}">
<span class="dropdown-item-text">{{ $__t('Add to meal plan') }}</span>
</a>
<a class="dropdown-item recipe-delete" <a class="dropdown-item recipe-delete"
type="button" type="button"
href="#" href="#"
@@ -566,4 +572,74 @@
</div> </div>
@endif @endif
</div> </div>
<div class="modal fade"
id="add-to-mealplan-modal"
tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title w-100">
<span>{{ $__t('Add meal plan entry') }}</span>
<span class="text-muted float-right">{{ $__t('Recipe') }}</span>
</h4>
</div>
<div class="modal-body">
<form id="add-to-mealplan-form"
novalidate>
@include('components.datetimepicker', array(
'id' => 'day',
'label' => 'Day',
'format' => 'YYYY-MM-DD',
'initWithNow' => false,
'limitEndToNow' => false,
'limitStartToNow' => false,
'isRequired' => true,
'additionalCssClasses' => 'date-only-datetimepicker',
'invalidFeedback' => $__t('A date is required')
))
@include('components.recipepicker', array(
'recipes' => $recipes,
'isRequired' => true,
'nextInputSelector' => '#recipe_servings'
))
@include('components.numberpicker', array(
'id' => 'recipe_servings',
'label' => 'Servings',
'min' => $DEFAULT_MIN_AMOUNT,
'decimals' => $userSettings['stock_decimal_places_amounts'],
'value' => '1',
'additionalCssClasses' => 'locale-number-input locale-number-quantity-amount'
))
<div class="form-group">
<label for="section_id">{{ $__t('Section') }}</label>
<select class="custom-control custom-select"
id="section_id"
name="section_id"
required>
@foreach($mealplanSections as $mealplanSection)
<option value="{{ $mealplanSection->id }}">{{ $mealplanSection->name }}</option>
@endforeach
</select>
</div>
<input type="hidden"
name="type"
value="recipe">
</form>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-secondary"
data-dismiss="modal">{{ $__t('Cancel') }}</button>
<button id="save-add-to-mealplan-button"
class="btn btn-success">{{ $__t('Save') }}</button>
</div>
</div>
</div>
</div>
@stop @stop