From 0aa2267e3b85de0bca26271649f2c49d02b79088 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 22 Jan 2021 20:34:10 -0800 Subject: [PATCH] Refactor journal entry form fields structure --- .../Controllers/JournalEntryController.php | 55 +++++++++++------ .../views/journal-entries/create.blade.php | 60 +++++++------------ .../partials/entry-item-input.blade.php | 38 ++++++++++++ 3 files changed, 98 insertions(+), 55 deletions(-) create mode 100644 resources/views/journal-entries/partials/entry-item-input.blade.php diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 713574f..2d7aeec 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -57,7 +57,24 @@ class JournalEntryController extends Controller ->map(function ($recipe) { return ['value' => $recipe->id, 'label' => $recipe->name]; }); + + $items = []; + if ($old = old('items')) { + foreach ($old['amount'] as $key => $amount) { + if (empty($amount) && empty($old['unit'][$key]) && empty($old['food'][$key]) && empty($old['recipe'][$key])) { + continue; + } + $items[] = [ + 'amount' => $amount, + 'unit' => $old['unit'][$key], + 'food' => $old['food'][$key], + 'recipe' => $old['recipe'][$key], + ]; + } + } + return view('journal-entries.create') + ->with('items', $items) ->with('foods', $foods) ->with('recipes', $recipes) ->with('meals', [ @@ -83,20 +100,20 @@ class JournalEntryController extends Controller $input = $request->validate([ 'date' => 'required|date', 'meal' => 'required|string', - 'amounts' => ['required', 'array', new ArrayNotEmpty], - 'amounts.*' => ['required_with:foods.*,recipes.*', 'nullable', new StringIsDecimalOrFraction], - 'units' => ['required', 'array', new ArrayNotEmpty], - 'units.*' => 'nullable|string', - 'foods' => 'required|array', - 'foods.*' => 'nullable|exists:App\Models\Food,id', - 'recipes' => 'required|array', - 'recipes.*' => 'nullable|exists:App\Models\Recipe,id', + 'items.amount' => ['required', 'array', new ArrayNotEmpty], + 'items.amount.*' => ['required_with:foods.*,recipes.*', 'nullable', new StringIsDecimalOrFraction], + 'items.unit' => 'required|array', + 'items.unit.*' => 'nullable|string', + 'items.food' => 'required|array', + 'items.food.*' => 'nullable|exists:App\Models\Food,id', + 'items.recipe' => 'required|array', + 'items.recipe.*' => 'nullable|exists:App\Models\Recipe,id', ]); // Validate that at least one recipe or food is selected. // TODO: refactor as custom validator. - $foods_selected = array_filter($input['foods']); - $recipes_selected = array_filter($input['recipes']); + $foods_selected = array_filter($input['items']['food']); + $recipes_selected = array_filter($input['items']['recipe']); if (empty($recipes_selected) && empty($foods_selected)) { return back()->withInput()->withErrors('At least one food or recipe is required.'); } @@ -107,7 +124,7 @@ class JournalEntryController extends Controller // Validate only "serving" unit used for recipes. // TODO: refactor as custom validator. foreach ($recipes_selected as $key => $id) { - if ($input['units'][$key] !== 'servings') { + if ($input['items']['unit'][$key] !== 'servings') { return back()->withInput()->withErrors('Recipes must use the "servings" unit.'); } } @@ -121,13 +138,13 @@ class JournalEntryController extends Controller $food = $foods->get($id); $nutrient_multiplier = Nutrients::calculateFoodNutrientMultiplier( $food, - Number::floatFromString($input['amounts'][$key]), - $input['units'][$key], + Number::floatFromString($input['items']['amount'][$key]), + $input['items']['unit'][$key], ); foreach ($nutrients as $nutrient => $amount) { $nutrients[$nutrient] += $food->{$nutrient} * $nutrient_multiplier; } - $summary[] = "{$input['amounts'][$key]} {$input['units'][$key]} {$food->name}"; + $summary[] = "{$input['items']['amount'][$key]} {$input['items']['unit'][$key]} {$food->name}"; } } @@ -136,9 +153,9 @@ class JournalEntryController extends Controller foreach ($recipes_selected as $key => $id) { $recipe = $recipes->get($id); foreach ($nutrients as $nutrient => $amount) { - $nutrients[$nutrient] += $recipe->{"{$nutrient}PerServing"}() * Number::floatFromString($input['amounts'][$key]); + $nutrients[$nutrient] += $recipe->{"{$nutrient}PerServing"}() * Number::floatFromString($input['items']['amount'][$key]); } - $summary[] = "{$input['amounts'][$key]} {$input['units'][$key]} {$recipe->name}"; + $summary[] = "{$input['items']['amount'][$key]} {$input['items']['unit'][$key]} {$recipe->name}"; } } @@ -157,7 +174,11 @@ class JournalEntryController extends Controller } } - return back()->with('message', "Journal entry added!"); + session()->flash('message', "Journal entry added!"); + return redirect()->route( + 'journal-entries.index', + ['date' => $entry->date->format('Y-m-d')] + ); } /** diff --git a/resources/views/journal-entries/create.blade.php b/resources/views/journal-entries/create.blade.php index 4fe3998..a5a1121 100644 --- a/resources/views/journal-entries/create.blade.php +++ b/resources/views/journal-entries/create.blade.php @@ -37,45 +37,29 @@ -
- - - -
- or -
- - @for ($i = 0; $i < 10; $i++) -
- -
-
- - - -
-
- - - -
+ + +
+
+ + +
- or -
-
- - - -
- @endfor + +
+
+ @foreach($items as $item) + @include('journal-entries.partials.entry-item-input', $item) + @endforeach + +
+ + + + +
diff --git a/resources/views/journal-entries/partials/entry-item-input.blade.php b/resources/views/journal-entries/partials/entry-item-input.blade.php new file mode 100644 index 0000000..faac388 --- /dev/null +++ b/resources/views/journal-entries/partials/entry-item-input.blade.php @@ -0,0 +1,38 @@ +
+
+ +
+
+ + + +
+
+ + + +
+
- or -
+
+ + + +
+ + + + + +