mirror of https://github.com/kcal-app/kcal.git
Add ingredient picker support to journal entry form
This commit is contained in:
parent
333abbb8f5
commit
3101c34071
|
@ -10,6 +10,7 @@ use App\Models\JournalEntry;
|
|||
use App\Models\Recipe;
|
||||
use App\Rules\ArrayNotEmpty;
|
||||
use App\Rules\StringIsDecimalOrFraction;
|
||||
use App\Rules\UsesIngredientTrait;
|
||||
use App\Support\Number;
|
||||
use App\Support\Nutrients;
|
||||
use Illuminate\Contracts\View\View;
|
||||
|
@ -40,43 +41,24 @@ class JournalEntryController extends Controller
|
|||
*/
|
||||
public function create(): View
|
||||
{
|
||||
$foods = Food::all(['id', 'name', 'detail', 'brand'])
|
||||
->sortBy('name')
|
||||
->collect()
|
||||
->map(function ($food) {
|
||||
return [
|
||||
'value' => $food->id,
|
||||
'label' => "{$food->name}"
|
||||
. ($food->detail ? ", {$food->detail}" : "")
|
||||
. ($food->brand ? " ({$food->brand})" : ""),
|
||||
];
|
||||
});
|
||||
$recipes = Recipe::all(['id', 'name'])
|
||||
->sortBy('name')
|
||||
->collect()
|
||||
->map(function ($recipe) {
|
||||
return ['value' => $recipe->id, 'label' => $recipe->name];
|
||||
});
|
||||
|
||||
$items = [];
|
||||
if ($old = old('items')) {
|
||||
$ingredients = [];
|
||||
if ($old = old('ingredients')) {
|
||||
foreach ($old['amount'] as $key => $amount) {
|
||||
if (empty($amount) && empty($old['unit'][$key]) && empty($old['food'][$key]) && empty($old['recipe'][$key])) {
|
||||
if (empty($amount) && empty($old['unit'][$key]) && empty($old['id'][$key])) {
|
||||
continue;
|
||||
}
|
||||
$items[] = [
|
||||
$ingredients[] = [
|
||||
'amount' => $amount,
|
||||
'unit' => $old['unit'][$key],
|
||||
'food' => $old['food'][$key],
|
||||
'recipe' => $old['recipe'][$key],
|
||||
'id' => $old['id'][$key],
|
||||
'type' => $old['type'][$key],
|
||||
'name' => $old['name'][$key],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return view('journal-entries.create')
|
||||
->with('items', $items)
|
||||
->with('foods', $foods)
|
||||
->with('recipes', $recipes)
|
||||
->with('ingredients', $ingredients)
|
||||
->with('meals', [
|
||||
['value' => 'breakfast', 'label' => 'Breakfast'],
|
||||
['value' => 'lunch', 'label' => 'Lunch'],
|
||||
|
@ -88,6 +70,7 @@ class JournalEntryController extends Controller
|
|||
['value' => 'tbsp', 'label' => 'tbsp.'],
|
||||
['value' => 'cup', 'label' => 'cup'],
|
||||
['value' => 'oz', 'label' => 'oz'],
|
||||
['value' => 'g', 'label' => 'grams'],
|
||||
['value' => 'servings', 'label' => 'servings'],
|
||||
]);
|
||||
}
|
||||
|
@ -100,62 +83,39 @@ class JournalEntryController extends Controller
|
|||
$input = $request->validate([
|
||||
'date' => 'required|date',
|
||||
'meal' => 'required|string',
|
||||
'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',
|
||||
'ingredients.amount' => ['required', 'array', new ArrayNotEmpty],
|
||||
'ingredients.amount.*' => ['required_with:foods.*,recipes.*', 'nullable', new StringIsDecimalOrFraction],
|
||||
'ingredients.unit' => 'required|array',
|
||||
'ingredients.unit.*' => 'nullable|string',
|
||||
'ingredients.id' => ['required', 'array', new ArrayNotEmpty],
|
||||
'ingredients.id.*' => 'required_with:ingredients.amount.*|nullable',
|
||||
'ingredients.type' => ['required', 'array', new ArrayNotEmpty],
|
||||
'ingredients.type.*' => ['required_with:ingredients.id.*', 'nullable', new UsesIngredientTrait()],
|
||||
]);
|
||||
|
||||
// Validate that at least one recipe or food is selected.
|
||||
// TODO: refactor as custom validator.
|
||||
$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.');
|
||||
}
|
||||
elseif (!empty(array_intersect_key($foods_selected, $recipes_selected))) {
|
||||
return back()->withInput()->withErrors('Select only one food or recipe per line.');
|
||||
}
|
||||
|
||||
// Validate only "serving" unit used for recipes.
|
||||
// TODO: refactor as custom validator.
|
||||
foreach ($recipes_selected as $key => $id) {
|
||||
if ($input['items']['unit'][$key] !== 'servings') {
|
||||
return back()->withInput()->withErrors('Recipes must use the "servings" unit.');
|
||||
}
|
||||
}
|
||||
|
||||
$summary = [];
|
||||
$nutrients = array_fill_keys(Nutrients::$all, 0);
|
||||
|
||||
if (!empty($foods_selected)) {
|
||||
$foods = Food::findMany($foods_selected)->keyBy('id');
|
||||
foreach ($foods_selected as $key => $id) {
|
||||
$food = $foods->get($id);
|
||||
// TODO: Improve efficiency? Potential for lots of queries here...
|
||||
foreach ($input['ingredients']['id'] as $key => $id) {
|
||||
if ($input['ingredients']['type'][$key] == Food::class) {
|
||||
$food = Food::whereId($id)->first();
|
||||
$nutrient_multiplier = Nutrients::calculateFoodNutrientMultiplier(
|
||||
$food,
|
||||
Number::floatFromString($input['items']['amount'][$key]),
|
||||
$input['items']['unit'][$key],
|
||||
Number::floatFromString($input['ingredients']['amount'][$key]),
|
||||
$input['ingredients']['unit'][$key],
|
||||
);
|
||||
foreach ($nutrients as $nutrient => $amount) {
|
||||
$nutrients[$nutrient] += $food->{$nutrient} * $nutrient_multiplier;
|
||||
}
|
||||
$summary[] = "{$input['items']['amount'][$key]} {$input['items']['unit'][$key]} {$food->name}";
|
||||
$summary[] = "{$input['ingredients']['amount'][$key]} {$input['ingredients']['unit'][$key]} {$food->name}";
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($recipes_selected)) {
|
||||
$recipes = Recipe::findMany($recipes_selected)->keyBy('id');
|
||||
foreach ($recipes_selected as $key => $id) {
|
||||
$recipe = $recipes->get($id);
|
||||
elseif ($input['ingredients']['type'][$key] == Recipe::class) {
|
||||
$recipe = Recipe::whereId($id)->first();
|
||||
foreach ($nutrients as $nutrient => $amount) {
|
||||
$nutrients[$nutrient] += $recipe->{"{$nutrient}PerServing"}() * Number::floatFromString($input['items']['amount'][$key]);
|
||||
$nutrients[$nutrient] += $recipe->{"{$nutrient}PerServing"}() * Number::floatFromString($input['ingredients']['amount'][$key]);
|
||||
}
|
||||
$summary[] = "{$input['items']['amount'][$key]} {$input['items']['unit'][$key]} {$recipe->name}";
|
||||
$summary[] = "{$input['ingredients']['amount'][$key]} {$input['ingredients']['unit'][$key]} {$recipe->name}";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,40 +141,6 @@ class JournalEntryController extends Controller
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param \App\Models\JournalEntry $journalEntry
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(JournalEntry $journalEntry)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param \App\Models\JournalEntry $journalEntry
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit(JournalEntry $journalEntry)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Models\JournalEntry $journalEntry
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, JournalEntry $journalEntry)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm removal of the specified resource.
|
||||
*/
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
value="{{ $defaultName ?? '' }}"
|
||||
placeholder="Search..."
|
||||
autocomplete="off"
|
||||
class="w-full"
|
||||
x-ref="ingredients_name"
|
||||
x-spread="search" />
|
||||
</div>
|
||||
|
|
|
@ -39,23 +39,21 @@
|
|||
</div>
|
||||
|
||||
<!-- Items -->
|
||||
<div x-data="{ items: 0 }">
|
||||
<div x-data="{ ingredients: 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>
|
||||
<x-inputs.label for="recipes" :value="__('Recipe')" class="col-span-4"/>
|
||||
<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 or Recipe" class="col-span-8"/>
|
||||
</div>
|
||||
<div>
|
||||
@foreach($items as $item)
|
||||
@include('journal-entries.partials.entry-item-input', $item)
|
||||
@foreach($ingredients as $ingredient)
|
||||
@include('journal-entries.partials.entry-item-input', $ingredient)
|
||||
@endforeach
|
||||
<template x-for="i in items + 1">
|
||||
<template x-for="i in ingredients + 1">
|
||||
@include('journal-entries.partials.entry-item-input')
|
||||
</template>
|
||||
</div>
|
||||
<x-inputs.icon-button type="button" color="green" x-on:click="items++;">
|
||||
<x-inputs.icon-button type="button" color="green" x-on:click="ingredients++;">
|
||||
<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>
|
||||
|
|
|
@ -1,34 +1,22 @@
|
|||
<div class="grid grid-cols-12 gap-4 items-center mt-2">
|
||||
<div>
|
||||
<x-inputs.input type="text"
|
||||
name="items[amount][]"
|
||||
name="ingredients[amount][]"
|
||||
class="block w-full"
|
||||
:value="$amount ?? null" />
|
||||
</div>
|
||||
<div class="col-span-2">
|
||||
<x-inputs.select name="items[unit][]"
|
||||
<x-inputs.select name="ingredients[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 class="col-span-8">
|
||||
<x-ingredient-picker :default-id="$id ?? null"
|
||||
:default-type="$type ?? null"
|
||||
:default-name="$name ?? null" />
|
||||
</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">
|
||||
|
|
Loading…
Reference in New Issue