Update food views for servings data

This commit is contained in:
Christopher C. Wells 2020-12-31 15:30:56 -08:00
parent ebc6f9cdd5
commit dd40582b64
4 changed files with 102 additions and 72 deletions

View File

@ -6,6 +6,7 @@ use App\Models\Food;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class FoodController extends Controller class FoodController extends Controller
{ {
@ -27,7 +28,12 @@ class FoodController extends Controller
*/ */
public function create(): View public function create(): View
{ {
return view('foods.create'); return view('foods.create')
->with('serving_units', new Collection([
['value' => 'tsp', 'label' => 'tsp.'],
['value' => 'tbsp', 'label' => 'tbsp.'],
['value' => 'cup', 'label' => 'cup'],
]));
} }
/**newly /**newly
@ -36,16 +42,18 @@ class FoodController extends Controller
public function store(Request $request): RedirectResponse public function store(Request $request): RedirectResponse
{ {
$attributes = $request->validate([ $attributes = $request->validate([
'name' => 'required|string', 'name' => 'required|string',
'detail' => 'nullable|string', 'detail' => 'nullable|string',
'carbohydrates' => 'nullable|numeric', 'brand' => 'nullable|string',
'calories' => 'nullable|numeric', 'serving_size' => 'required|numeric',
'cholesterol' => 'nullable|numeric', 'serving_unit' => 'nullable|string',
'fat' => 'nullable|numeric', 'serving_weight' => 'required|numeric',
'protein' => 'nullable|numeric', 'calories' => 'nullable|numeric',
'sodium' => 'nullable|numeric', 'fat' => 'nullable|numeric',
'unit_weight' => 'required_without:cup_weight|nullable|numeric', 'cholesterol' => 'nullable|numeric',
'cup_weight' => 'required_without:unit_weight|nullable|numeric', 'sodium' => 'nullable|numeric',
'carbohydrates' => 'nullable|numeric',
'protein' => 'nullable|numeric',
]); ]);
/** @var \App\Models\Food $food */ /** @var \App\Models\Food $food */
$food = tap(new Food(array_filter($attributes)))->save(); $food = tap(new Food(array_filter($attributes)))->save();

View File

@ -6,6 +6,9 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* TODO: Change this model to track nutrients _directly_.
*/
class JournalEntry extends Model class JournalEntry extends Model
{ {
use HasFactory; use HasFactory;

View File

@ -28,7 +28,7 @@
<form method="POST" action="{{ route('foods.store') }}"> <form method="POST" action="{{ route('foods.store') }}">
@csrf @csrf
<div class="flex flex-col space-y-4"> <div class="flex flex-col space-y-4">
<div class="grid grid-cols-2 gap-4"> <div class="grid grid-cols-3 gap-4">
<!-- Name --> <!-- Name -->
<div> <div>
<x-inputs.label for="name" :value="__('Name')"/> <x-inputs.label for="name" :value="__('Name')"/>
@ -51,12 +51,62 @@
name="detail" name="detail"
:value="old('detail')"/> :value="old('detail')"/>
</div> </div>
<!-- Brand -->
<div>
<x-inputs.label for="brand" :value="__('Brand')"/>
<x-inputs.input id="brand"
class="block mt-1 w-full"
type="text"
name="brand"
:value="old('brand')"/>
</div>
</div>
<div class="flex items-center space-x-4">
<!-- Serving size -->
<div>
<x-inputs.label for="serving_size" :value="__('Serving size')"/>
<x-inputs.input id="serving_size"
class="block mt-1"
type="number"
step="any"
name="serving_size"
size="10"
:value="old('serving_size')"/>
</div>
<!-- Serving unit -->
<div>
<x-inputs.label for="serving_unit" :value="__('Serving unit')"/>
<x-inputs.select name="serving_unit"
:options="$serving_units"
:selectedValue="old('serving_unit')">
<option value=""></option>
</x-inputs.select>
</div>
<!-- Serving weight -->
<div>
<x-inputs.label for="serving_weight" :value="__('Serving weight (g)')"/>
<x-inputs.input id="serving_weight"
class="block mt-1"
type="number"
step="any"
name="serving_weight"
size="10"
:value="old('serving_weight')"/>
</div>
</div> </div>
<div class="grid grid-rows-3 md:grid-rows-2 lg:grid-rows-1 grid-flow-col"> <div class="grid grid-rows-3 md:grid-rows-2 lg:grid-rows-1 grid-flow-col">
<!-- Calories --> <!-- Calories -->
<div> <div>
<x-inputs.label for="calories" :value="__('Calories')"/> <x-inputs.label for="calories" :value="__('Calories (g)')"/>
<x-inputs.input id="calories" <x-inputs.input id="calories"
class="block mt-1" class="block mt-1"
@ -67,35 +117,9 @@
:value="old('calories')"/> :value="old('calories')"/>
</div> </div>
<!-- Carbohydrates -->
<div>
<x-inputs.label for="carbohydrates" :value="__('Carbohydrates')"/>
<x-inputs.input id="carbohydrates"
class="block mt-1"
type="number"
step="any"
name="carbohydrates"
size="10"
:value="old('carbohydrates')"/>
</div>
<!-- Cholesterol -->
<div>
<x-inputs.label for="cholesterol" :value="__('Cholesterol')"/>
<x-inputs.input id="cholesterol"
class="block mt-1"
type="number"
step="any"
name="cholesterol"
size="10"
:value="old('cholesterol')"/>
</div>
<!-- Fat --> <!-- Fat -->
<div> <div>
<x-inputs.label for="fat" :value="__('Fat')"/> <x-inputs.label for="fat" :value="__('Fat (g)')"/>
<x-inputs.input id="fat" <x-inputs.input id="fat"
class="block mt-1" class="block mt-1"
@ -106,22 +130,22 @@
:value="old('fat')"/> :value="old('fat')"/>
</div> </div>
<!-- Protein --> <!-- Cholesterol -->
<div> <div>
<x-inputs.label for="protein" :value="__('Protein')"/> <x-inputs.label for="cholesterol" :value="__('Cholesterol (g)')"/>
<x-inputs.input id="protein" <x-inputs.input id="cholesterol"
class="block mt-1" class="block mt-1"
type="number" type="number"
step="any" step="any"
name="protein" name="cholesterol"
size="10" size="10"
:value="old('protein')"/> :value="old('cholesterol')"/>
</div> </div>
<!-- Sodium --> <!-- Sodium -->
<div> <div>
<x-inputs.label for="sodium" :value="__('Sodium')"/> <x-inputs.label for="sodium" :value="__('Sodium (g)')"/>
<x-inputs.input id="sodium" <x-inputs.input id="sodium"
class="block mt-1" class="block mt-1"
@ -131,37 +155,31 @@
size="10" size="10"
:value="old('sodium')"/> :value="old('sodium')"/>
</div> </div>
</div>
<div class="flex items-center"> <!-- Carbohydrates -->
<!-- Cup weight -->
<div> <div>
<x-inputs.label for="cup_weight" :value="__('Cup weight')"/> <x-inputs.label for="carbohydrates" :value="__('Carbohydrates (g)')"/>
<x-inputs.input id="cup_weight" <x-inputs.input id="carbohydrates"
class="block mt-1" class="block mt-1"
type="number" type="number"
step="any" step="any"
name="cup_weight" name="carbohydrates"
size="10" size="10"
:value="old('cup_weight')"/> :value="old('carbohydrates')"/>
</div> </div>
<div class="p-4 font-black text-3xl"> <!-- Protein -->
or
</div>
<!-- Unit weight -->
<div> <div>
<x-inputs.label for="unit_weight" :value="__('Unit weight')"/> <x-inputs.label for="protein" :value="__('Protein (g)')"/>
<x-inputs.input id="unit_weight" <x-inputs.input id="protein"
class="block mt-1" class="block mt-1"
type="number" type="number"
step="any" step="any"
name="unit_weight" name="protein"
size="10" size="10"
:value="old('unit_weight')"/> :value="old('protein')"/>
</div> </div>
</div> </div>
</div> </div>

View File

@ -17,20 +17,21 @@
<div class="grid grid-cols-3 gap-4"> <div class="grid grid-cols-3 gap-4">
@foreach ($foods as $food) @foreach ($foods as $food)
<div class="p-2 font-light rounded-lg border-2 border-gray-200"> <div class="p-2 font-light rounded-lg border-2 border-gray-200">
<div class="pb-2 lowercase flex justify-between items-baseline"> <div class="text-2xl lowercase">
<div class="text-2xl">
{{ $food->name }}@if($food->detail), <span class="text-gray-500">{{ $food->detail }}</span>@endif {{ $food->name }}@if($food->detail), <span class="text-gray-500">{{ $food->detail }}</span>@endif
</div>
@if($food->brand)
<div class="text-xl text-gray-600">
{{ $food->brand }}
</div> </div>
<div class="text-right text-sm"> @endif
@if ($food->unit_weight) <div class="font-bold">
{{ $food->unit_weight }}g each Serving size {{ $food->serving_size }}
@else {{ $food->serving_unit }}
{{ $food->cup_weight }}g per cup ({{ $food->serving_weight }}g)
@endif
</div>
</div> </div>
<div class="grid grid-cols-2 text-sm border-t-8 border-black pt-2"> <div class="grid grid-cols-2 text-sm border-t-8 border-black pt-2">
<div class="col-span-2 text-xs text-right">Amount per 100g</div> <div class="col-span-2 text-xs font-bold">Amount per serving</div>
<div class="font-extrabold text-lg border-b-4 border-black">Calories</div> <div class="font-extrabold text-lg border-b-4 border-black">Calories</div>
<div class="font-extrabold text-right text-lg border-b-4 border-black">{{$food->calories}}</div> <div class="font-extrabold text-right text-lg border-b-4 border-black">{{$food->calories}}</div>
<div class="font-bold border-b border-gray-300">Fat</div> <div class="font-bold border-b border-gray-300">Fat</div>