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\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
class FoodController extends Controller
{
@ -27,7 +28,12 @@ class FoodController extends Controller
*/
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
@ -36,16 +42,18 @@ class FoodController extends Controller
public function store(Request $request): RedirectResponse
{
$attributes = $request->validate([
'name' => 'required|string',
'detail' => 'nullable|string',
'carbohydrates' => 'nullable|numeric',
'calories' => 'nullable|numeric',
'cholesterol' => 'nullable|numeric',
'fat' => 'nullable|numeric',
'protein' => 'nullable|numeric',
'sodium' => 'nullable|numeric',
'unit_weight' => 'required_without:cup_weight|nullable|numeric',
'cup_weight' => 'required_without:unit_weight|nullable|numeric',
'name' => 'required|string',
'detail' => 'nullable|string',
'brand' => 'nullable|string',
'serving_size' => 'required|numeric',
'serving_unit' => 'nullable|string',
'serving_weight' => 'required|numeric',
'calories' => 'nullable|numeric',
'fat' => 'nullable|numeric',
'cholesterol' => 'nullable|numeric',
'sodium' => 'nullable|numeric',
'carbohydrates' => 'nullable|numeric',
'protein' => 'nullable|numeric',
]);
/** @var \App\Models\Food $food */
$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\Relations\BelongsTo;
/**
* TODO: Change this model to track nutrients _directly_.
*/
class JournalEntry extends Model
{
use HasFactory;

View File

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

View File

@ -17,20 +17,21 @@
<div class="grid grid-cols-3 gap-4">
@foreach ($foods as $food)
<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">
<div class="text-2xl lowercase">
{{ $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 class="text-right text-sm">
@if ($food->unit_weight)
{{ $food->unit_weight }}g each
@else
{{ $food->cup_weight }}g per cup
@endif
</div>
@endif
<div class="font-bold">
Serving size {{ $food->serving_size }}
{{ $food->serving_unit }}
({{ $food->serving_weight }}g)
</div>
<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-right text-lg border-b-4 border-black">{{$food->calories}}</div>
<div class="font-bold border-b border-gray-300">Fat</div>