From f499e5ffb77a2e3bc3d93aefbfa23ca257baec62 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Thu, 21 Jan 2021 05:54:35 -0800 Subject: [PATCH] Add support for old values in recipe form --- app/Models/Food.php | 2 +- app/Models/FoodAmount.php | 13 +++ resources/views/recipes/edit.blade.php | 97 ++++++++----------- .../partials/ingredient-input.blade.php | 22 +++++ 4 files changed, 79 insertions(+), 55 deletions(-) create mode 100644 resources/views/recipes/partials/ingredient-input.blade.php diff --git a/app/Models/Food.php b/app/Models/Food.php index 9d89006..ed533d0 100644 --- a/app/Models/Food.php +++ b/app/Models/Food.php @@ -105,7 +105,7 @@ class Food extends Model protected $appends = ['serving_size_formatted']; /** - * Get the serving size as a fractional. + * Get the serving size as a formatted string (e.g. 0.5 = 1/2). */ public function getServingSizeFormattedAttribute(): string { return Number::fractionStringFromFloat($this->serving_size); diff --git a/app/Models/FoodAmount.php b/app/Models/FoodAmount.php index aba62ba..232a568 100644 --- a/app/Models/FoodAmount.php +++ b/app/Models/FoodAmount.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Support\Number; use App\Support\Nutrients; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -74,6 +75,18 @@ class FoodAmount extends Model 'sodium', ]; + /** + * @inheritdoc + */ + protected $appends = ['amount_formatted']; + + /** + * Get the amount as a formatted string (e.g. 0.5 = 1/2). + */ + public function getAmountFormattedAttribute(): string { + return Number::fractionStringFromFloat($this->amount); + } + /** * Get the Food this amount belongs to. */ diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php index 1182ff4..552eead 100644 --- a/resources/views/recipes/edit.blade.php +++ b/resources/views/recipes/edit.blade.php @@ -64,50 +64,32 @@

Ingredients

- @foreach($recipe->foodAmounts as $foodAmount) -
- - - - - - - - - - - -
- @endforeach - + @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 @@ -119,20 +101,27 @@

Steps

@php($step_number = 0) -
- @foreach($recipe->steps as $step) -
-
{{ $step_number++ }}
- -
- @endforeach +
+ @if(old('steps')) + @foreach(old('steps') as $i => $step) + @if (empty($step)) @continue @endif +
+
{{ $step_number++ }}
+ +
+ @endforeach + @else + @foreach($recipe->steps as $step) +
+
{{ $step_number++ }}
+ +
+ @endforeach + @endif diff --git a/resources/views/recipes/partials/ingredient-input.blade.php b/resources/views/recipes/partials/ingredient-input.blade.php new file mode 100644 index 0000000..c473faf --- /dev/null +++ b/resources/views/recipes/partials/ingredient-input.blade.php @@ -0,0 +1,22 @@ +
+ + + + + + + + + + + +