From 9f180b9b62922421f639a38bf9c7048f186e104f Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 22 Jan 2021 19:18:32 -0800 Subject: [PATCH] Refactor ingredients field structure --- app/Http/Controllers/RecipeController.php | 55 ++++++++++++++----- .../components/ingredient-picker.blade.php | 4 +- resources/views/recipes/edit.blade.php | 29 ++-------- .../partials/ingredient-input.blade.php | 6 +- resources/views/recipes/show.blade.php | 9 ++- 5 files changed, 59 insertions(+), 44 deletions(-) diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 23c5d25..090c78e 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -70,8 +70,37 @@ class RecipeController extends Controller */ public function edit(Recipe $recipe): View { + // Pre-populate ingredients from form data or current recipe. + $ingredients = []; + if ($old = old('ingredients')) { + foreach ($old['id'] as $key => $food_id) { + if (empty($food_id)) { + continue; + } + $ingredients[] = [ + 'amount' => $old['amount'][$key], + 'unit' => $old['unit'][$key], + 'food_id' => $food_id, + 'food_name' => $old['name'][$key], + 'detail' => $old['detail'][$key], + ]; + } + } + else { + foreach ($recipe->foodAmounts as $foodAmount) { + $ingredients[] = [ + 'amount' => $foodAmount->amount_formatted, + 'unit' => $foodAmount->unit, + 'food_id' => $foodAmount->food->id, + 'food_name' => $foodAmount->food->name, + 'detail' => $foodAmount->detail, + ]; + } + } + return view('recipes.edit') ->with('recipe', $recipe) + ->with('ingredients', $ingredients) ->with('ingredients_units', new Collection([ ['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'], @@ -98,14 +127,14 @@ class RecipeController extends Controller 'description' => 'nullable|string', 'source' => 'nullable|string', 'servings' => 'required|numeric', - 'ingredients_amount' => ['required', 'array', new ArrayNotEmpty], - 'ingredients_amount.*' => ['required_with:ingredients.*', 'nullable', new StringIsDecimalOrFraction], - 'ingredients_unit' => ['required', 'array'], - 'ingredients_unit.*' => 'nullable|string', - 'ingredients_detail' => ['required', 'array'], - 'ingredients_detail.*' => 'nullable|string', - 'ingredients' => ['required', 'array', new ArrayNotEmpty], - 'ingredients.*' => 'required_with:ingredients_amount.*|nullable|exists:App\Models\Food,id', + 'ingredients.amount' => ['required', 'array', new ArrayNotEmpty], + 'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction], + 'ingredients.unit' => ['required', 'array'], + 'ingredients.unit.*' => 'nullable|string', + 'ingredients.detail' => ['required', 'array'], + 'ingredients.detail.*' => 'nullable|string', + 'ingredients.id' => ['required', 'array', new ArrayNotEmpty], + 'ingredients.id.*' => 'required_with:ingredients.amount.*|nullable|exists:App\Models\Food,id', 'steps' => ['required', 'array', new ArrayNotEmpty], 'steps.*' => 'nullable|string', ]); @@ -126,15 +155,15 @@ class RecipeController extends Controller $food_amounts = []; $weight = 0; // TODO: Handle removals. - foreach (array_filter($input['ingredients_amount']) as $key => $amount) { + foreach (array_filter($input['ingredients']['id']) as $key => $food_id) { $food_amounts[$key] = $recipe->foodAmounts[$key] ?? new FoodAmount(); $food_amounts[$key]->fill([ - 'amount' => Number::floatFromString($amount), - 'unit' => $input['ingredients_unit'][$key], - 'detail' => $input['ingredients_detail'][$key], + 'amount' => Number::floatFromString($input['ingredients']['amount'][$key]), + 'unit' => $input['ingredients']['unit'][$key], + 'detail' => $input['ingredients']['detail'][$key], 'weight' => $weight++, ]); - $food_amounts[$key]->food()->associate($input['ingredients'][$key]); + $food_amounts[$key]->food()->associate($food_id); } $recipe->foodAmounts()->saveMany($food_amounts); diff --git a/resources/views/components/ingredient-picker.blade.php b/resources/views/components/ingredient-picker.blade.php index 946add9..d618d1c 100644 --- a/resources/views/components/ingredient-picker.blade.php +++ b/resources/views/components/ingredient-picker.blade.php @@ -2,11 +2,11 @@

Ingredients

- @if(old('ingredients')) - @foreach(old('ingredients') as $i => $ingredient) - @if (empty($ingredient) && empty(old('ingredients_amount')[$i]) && empty(old('ingredients_unit')[$i])) - @continue - @endif - @include('recipes.partials.ingredient-input', [ - 'amount' => old('ingredients_amount')[$i], - 'unit' => old('ingredients_unit')[$i], - 'food_id' => old('ingredients')[$i], - 'food_name' => old('ingredients_name')[$i], - 'detail' => old('ingredients_detail')[$i], - ]) - @endforeach - @else - @foreach($recipe->foodAmounts as $foodAmount) - @include('recipes.partials.ingredient-input', [ - 'amount' => $foodAmount->amount_formatted, - 'unit' => $foodAmount->unit, - 'food_id' => $foodAmount->food->id, - 'food_name' => $foodAmount->food->name, - 'detail' => $foodAmount->detail, - ]) - @endforeach - @endif + @foreach($ingredients as $ingredient) + @include('recipes.partials.ingredient-input', $ingredient) + @endforeach @@ -100,7 +79,7 @@

Steps

-
+
@if(old('steps')) @foreach(old('steps') as $i => $step_default) @if (empty($step)) @continue @endif diff --git a/resources/views/recipes/partials/ingredient-input.blade.php b/resources/views/recipes/partials/ingredient-input.blade.php index c473faf..a7db948 100644 --- a/resources/views/recipes/partials/ingredient-input.blade.php +++ b/resources/views/recipes/partials/ingredient-input.blade.php @@ -1,9 +1,9 @@
- @@ -12,7 +12,7 @@ :default-name="$food_name ?? null" /> diff --git a/resources/views/recipes/show.blade.php b/resources/views/recipes/show.blade.php index 390e3d3..b2b0732 100644 --- a/resources/views/recipes/show.blade.php +++ b/resources/views/recipes/show.blade.php @@ -1,7 +1,14 @@ -

+

{{ $recipe->name }} + + + + + +