Combine ingredients and separators handler in recipe edit

This commit is contained in:
Christopher C. Wells 2021-03-26 06:05:12 -07:00
parent 47a8f5675f
commit 8288a73faa
2 changed files with 34 additions and 3 deletions

View File

@ -88,6 +88,7 @@ class RecipeController extends Controller
continue; continue;
} }
$ingredients[] = [ $ingredients[] = [
'type' => 'ingredient',
'key' => $old['key'][$key], 'key' => $old['key'][$key],
'weight' => $old['weight'][$key], 'weight' => $old['weight'][$key],
'amount' => $old['amount'][$key], 'amount' => $old['amount'][$key],
@ -102,6 +103,7 @@ class RecipeController extends Controller
else { else {
foreach ($recipe->ingredientAmounts as $key => $ingredientAmount) { foreach ($recipe->ingredientAmounts as $key => $ingredientAmount) {
$ingredients[] = [ $ingredients[] = [
'type' => 'ingredient',
'key' => $key, 'key' => $key,
'weight' => $ingredientAmount->weight, 'weight' => $ingredientAmount->weight,
'amount' => $ingredientAmount->amount_formatted, 'amount' => $ingredientAmount->amount_formatted,
@ -114,6 +116,31 @@ class RecipeController extends Controller
} }
} }
$separators = [];
if ($old = old('separators')) {
foreach ($old['key'] as $index => $key) {
if (empty($key)) {
continue;
}
$separators[] = [
'type' => 'separator',
'key' => $old['key'][$index],
'weight' => $old['weight'][$index],
'text' => $old['text'][$index],
];
}
}
else {
foreach ($recipe->ingredientSeparators as $key => $ingredientSeparator) {
$ingredients[] = [
'type' => 'separator',
'key' => $key,
'weight' => $ingredientSeparator->weight,
'text' => $ingredientSeparator->text,
];
}
}
$steps = []; $steps = [];
if ($old = old('steps')) { if ($old = old('steps')) {
foreach ($old['step'] as $key => $step) { foreach ($old['step'] as $key => $step) {
@ -144,7 +171,7 @@ class RecipeController extends Controller
return view('recipes.edit') return view('recipes.edit')
->with('recipe', $recipe) ->with('recipe', $recipe)
->with('recipe_tags', $recipe_tags) ->with('recipe_tags', $recipe_tags)
->with('ingredients', $ingredients) ->with('ingredients_list', new Collection([...$ingredients, ...$separators]))
->with('steps', $steps) ->with('steps', $steps)
->with('ingredients_units', Nutrients::$units); ->with('ingredients_units', Nutrients::$units);
} }

View File

@ -126,8 +126,12 @@
<!-- Ingredients --> <!-- Ingredients -->
<h3 class="mt-6 mb-2 font-extrabold text-lg">Ingredients</h3> <h3 class="mt-6 mb-2 font-extrabold text-lg">Ingredients</h3>
<div x-data class="ingredients space-y-4"> <div x-data class="ingredients space-y-4">
@forelse($ingredients as $ingredient) @forelse($ingredients_list->sortBy('weight') as $item)
@include('recipes.partials.ingredient-input', $ingredient) @if($item['type'] === 'ingredient')
@include('recipes.partials.ingredient-input', $item)
@elseif($item['type'] === 'separator')
@include('recipes.partials.separator-input', $item)
@endif
@empty @empty
@include('recipes.partials.ingredient-input') @include('recipes.partials.ingredient-input')
@endforelse @endforelse