From f2864b76dc46a48ff51107b73410ae84f8f74a1a Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 20 Jan 2021 20:39:14 -0800 Subject: [PATCH] Complete AlpineJS-based ingredient picket --- .../IngredientPickerController.php | 14 ++-- app/Http/Controllers/RecipeController.php | 27 ++++--- app/Models/Food.php | 15 +++- .../components/ingredient-picker.blade.php | 21 ++++-- resources/views/recipes/edit.blade.php | 70 +++++++++---------- 5 files changed, 83 insertions(+), 64 deletions(-) diff --git a/app/Http/Controllers/IngredientPickerController.php b/app/Http/Controllers/IngredientPickerController.php index c559ad9..65850e5 100644 --- a/app/Http/Controllers/IngredientPickerController.php +++ b/app/Http/Controllers/IngredientPickerController.php @@ -2,18 +2,22 @@ namespace App\Http\Controllers; +use App\Models\Food; use Illuminate\Http\JsonResponse; +use Illuminate\Http\Request; class IngredientPickerController extends Controller { /** * Search for ingredients. */ - public function search(): JsonResponse + public function search(Request $request): JsonResponse { - return response()->json([ - ['id' => 1, 'name' => 'Flour'], - ['id' => 2, 'name' => 'Eggs'], - ]); + $results = []; + $term = $request->query->get('term'); + if (!empty($term)) { + $results = Food::search($term); + } + return response()->json($results); } } diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index 7caecb8..23c5d25 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -2,7 +2,6 @@ namespace App\Http\Controllers; -use App\Models\Food; use App\Models\FoodAmount; use App\Models\Recipe; use App\Models\RecipeStep; @@ -73,7 +72,7 @@ class RecipeController extends Controller { return view('recipes.edit') ->with('recipe', $recipe) - ->with('food_units', new Collection([ + ->with('ingredients_units', new Collection([ ['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'], ['value' => 'cup', 'label' => 'cup'], @@ -99,14 +98,14 @@ class RecipeController extends Controller 'description' => 'nullable|string', 'source' => 'nullable|string', 'servings' => 'required|numeric', - 'foods_amount' => ['required', 'array', new ArrayNotEmpty], - 'foods_amount.*' => ['required_with:foods.*', 'nullable', new StringIsDecimalOrFraction], - 'foods_unit' => ['required', 'array'], - 'foods_unit.*' => 'nullable|string', - 'foods_detail' => ['required', 'array'], - 'foods_detail.*' => 'nullable|string', - 'foods' => ['required', 'array', new ArrayNotEmpty], - 'foods.*' => 'required_with:foods_amount.*|nullable|exists:App\Models\Food,id', + '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', 'steps' => ['required', 'array', new ArrayNotEmpty], 'steps.*' => 'nullable|string', ]); @@ -127,15 +126,15 @@ class RecipeController extends Controller $food_amounts = []; $weight = 0; // TODO: Handle removals. - foreach (array_filter($input['foods_amount']) as $key => $amount) { + foreach (array_filter($input['ingredients_amount']) as $key => $amount) { $food_amounts[$key] = $recipe->foodAmounts[$key] ?? new FoodAmount(); $food_amounts[$key]->fill([ 'amount' => Number::floatFromString($amount), - 'unit' => $input['foods_unit'][$key], - 'detail' => $input['foods_detail'][$key], + 'unit' => $input['ingredients_unit'][$key], + 'detail' => $input['ingredients_detail'][$key], 'weight' => $weight++, ]); - $food_amounts[$key]->food()->associate($input['foods'][$key]); + $food_amounts[$key]->food()->associate($input['ingredients'][$key]); } $recipe->foodAmounts()->saveMany($food_amounts); diff --git a/app/Models/Food.php b/app/Models/Food.php index 0208336..d95a2a0 100644 --- a/app/Models/Food.php +++ b/app/Models/Food.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Models\Traits\Journalable; use App\Models\Traits\Sluggable; +use App\Support\Number; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -84,7 +85,7 @@ class Food extends Model ]; /** - * The attributes that should be cast. + * @inheritdoc */ protected $casts = [ 'calories' => 'float', @@ -97,6 +98,18 @@ class Food extends Model 'sodium' => 'float', ]; + /** + * @inheritdoc + */ + protected $appends = ['serving_size_formatted']; + + /** + * Get the serving size as a fractional. + */ + public function getServingSizeFormattedAttribute(): string { + return Number::fractionStringFromFloat($this->serving_size); + } + /** * Get the food amounts using this food. */ diff --git a/resources/views/components/ingredient-picker.blade.php b/resources/views/components/ingredient-picker.blade.php index 7747975..946add9 100644 --- a/resources/views/components/ingredient-picker.blade.php +++ b/resources/views/components/ingredient-picker.blade.php @@ -2,33 +2,42 @@
+ x-ref="ingredients"/> + x-ref="ingredients_name" />
+ x-on:click="selected = $event.target; if (selected.dataset.id) { $refs.ingredients_name.value = selected.dataset.value; $refs.ingredients.value = selected.dataset.id; searching = false; }"> +
+ No results found. +
diff --git a/resources/views/recipes/edit.blade.php b/resources/views/recipes/edit.blade.php index ebe2a85..37520fe 100644 --- a/resources/views/recipes/edit.blade.php +++ b/resources/views/recipes/edit.blade.php @@ -64,46 +64,40 @@

Ingredients

-{{-- @foreach($recipe->foodAmounts as $foodAmount)--}} -{{--
--}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{-- --}} -{{--
--}} -{{-- @endforeach--}} + @foreach($recipe->foodAmounts as $foodAmount) +
+ + + + + + +
+ @endforeach - -{{-- --}} Add Ingredient