mirror of https://github.com/kcal-app/kcal.git
Refactor journal entry form fields structure
This commit is contained in:
parent
fe868a1a08
commit
0aa2267e3b
|
@ -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')]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -37,45 +37,29 @@
|
|||
</x-inputs.select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid grid-cols-12 gap-4 items-center">
|
||||
<x-inputs.label for="amounts" :value="__('Amount')"/>
|
||||
<x-inputs.label for="units" :value="__('Unit')" class="col-span-2"/>
|
||||
<x-inputs.label for="foods" :value="__('Food')" class="col-span-4"/>
|
||||
<div class="text-center">- or -</div>
|
||||
<x-inputs.label for="recipes" :value="__('Recipe')" class="col-span-4"/>
|
||||
@for ($i = 0; $i < 10; $i++)
|
||||
<div>
|
||||
<x-inputs.input type="text"
|
||||
name="amounts[]"
|
||||
class="block w-full"
|
||||
:value="old('amounts.' . $i)" />
|
||||
</div>
|
||||
<div class="col-span-2">
|
||||
<x-inputs.select name="units[]"
|
||||
class="block w-full"
|
||||
:options="$units"
|
||||
:selectedValue="old('units.' . $i)">
|
||||
<option value=""></option>
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
<div class="col-span-4">
|
||||
<x-inputs.select name="foods[]"
|
||||
class="block w-full"
|
||||
:options="$foods"
|
||||
:selectedValue="old('foods.' . $i)">
|
||||
<option value=""></option>
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
|
||||
<!-- Items -->
|
||||
<div x-data="{ items: 0 }">
|
||||
<div class="grid grid-cols-12 gap-4 items-center">
|
||||
<x-inputs.label for="amounts" :value="__('Amount')"/>
|
||||
<x-inputs.label for="units" :value="__('Unit')" class="col-span-2"/>
|
||||
<x-inputs.label for="foods" :value="__('Food')" class="col-span-4"/>
|
||||
<div class="text-center">- or -</div>
|
||||
<div class="col-span-4">
|
||||
<x-inputs.select name="recipes[]"
|
||||
class="block w-full"
|
||||
:options="$recipes"
|
||||
:selectedValue="old('recipes.' . $i)">
|
||||
<option value=""></option>
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
@endfor
|
||||
<x-inputs.label for="recipes" :value="__('Recipe')" class="col-span-4"/>
|
||||
</div>
|
||||
<div>
|
||||
@foreach($items as $item)
|
||||
@include('journal-entries.partials.entry-item-input', $item)
|
||||
@endforeach
|
||||
<template x-for="i in items + 1">
|
||||
@include('journal-entries.partials.entry-item-input')
|
||||
</template>
|
||||
</div>
|
||||
<x-inputs.icon-button type="button" color="green" x-on:click="items++;">
|
||||
<svg class="h-10 w-10" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-11a1 1 0 10-2 0v2H7a1 1 0 100 2h2v2a1 1 0 102 0v-2h2a1 1 0 100-2h-2V7z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</x-inputs.icon-button>
|
||||
</div>
|
||||
<div class="flex items-center justify-end mt-4">
|
||||
<x-inputs.button class="ml-3">
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
<div class="grid grid-cols-12 gap-4 items-center mt-2">
|
||||
<div>
|
||||
<x-inputs.input type="text"
|
||||
name="items[amount][]"
|
||||
class="block w-full"
|
||||
:value="$amount ?? null" />
|
||||
</div>
|
||||
<div class="col-span-2">
|
||||
<x-inputs.select name="items[unit][]"
|
||||
class="block w-full"
|
||||
:options="$units"
|
||||
:selectedValue="$unit ?? null">
|
||||
<option value=""></option>
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
<div class="col-span-4">
|
||||
<x-inputs.select name="items[food][]"
|
||||
class="block w-full"
|
||||
:options="$foods"
|
||||
:selectedValue="$food ?? null">
|
||||
<option value=""></option>
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
<div class="text-center">- or -</div>
|
||||
<div class="col-span-3">
|
||||
<x-inputs.select name="items[recipe][]"
|
||||
class="block w-full"
|
||||
:options="$recipes"
|
||||
:selectedValue="$recipe ?? null">
|
||||
<option value=""></option>
|
||||
</x-inputs.select>
|
||||
</div>
|
||||
<x-inputs.icon-button type="button" color="red" x-on:click="$event.target.parentNode.remove();">
|
||||
<svg class="h-8 w-8 pointer-events-none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</x-inputs.icon-button>
|
||||
</div>
|
Loading…
Reference in New Issue