Handle step deletes

This commit is contained in:
Christopher C. Wells 2021-01-22 20:01:59 -08:00 committed by Christopher Charbonneau Wells
parent acdd89c192
commit fe868a1a08
3 changed files with 47 additions and 19 deletions

View File

@ -70,7 +70,7 @@ class RecipeController extends Controller
*/ */
public function edit(Recipe $recipe): View public function edit(Recipe $recipe): View
{ {
// Pre-populate ingredients from form data or current recipe. // Pre-populate relationships from form data or current recipe.
$ingredients = []; $ingredients = [];
if ($old = old('ingredients')) { if ($old = old('ingredients')) {
foreach ($old['id'] as $key => $food_id) { foreach ($old['id'] as $key => $food_id) {
@ -100,9 +100,31 @@ class RecipeController extends Controller
} }
} }
$steps = [];
if ($old = old('steps')) {
foreach ($old['step'] as $key => $step) {
if (empty($step)) {
continue;
}
$steps[] = [
'original_key' => $old['original_key'][$key],
'step_default' => $step,
];
}
}
else {
foreach ($recipe->steps as $key => $step) {
$steps[] = [
'original_key' => $key,
'step_default' => $step->step,
];
}
}
return view('recipes.edit') return view('recipes.edit')
->with('recipe', $recipe) ->with('recipe', $recipe)
->with('ingredients', $ingredients) ->with('ingredients', $ingredients)
->with('steps', $steps)
->with('ingredients_units', new Collection([ ->with('ingredients_units', new Collection([
['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tsp', 'label' => 'tsp.'],
['value' => 'tbsp', 'label' => 'tbsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'],
@ -138,8 +160,9 @@ class RecipeController extends Controller
'ingredients.id' => ['required', 'array', new ArrayNotEmpty], 'ingredients.id' => ['required', 'array', new ArrayNotEmpty],
'ingredients.id.*' => 'required_with:ingredients.amount.*|nullable|exists:App\Models\Food,id', 'ingredients.id.*' => 'required_with:ingredients.amount.*|nullable|exists:App\Models\Food,id',
'ingredients.original_key' => 'nullable|array', 'ingredients.original_key' => 'nullable|array',
'steps' => ['required', 'array', new ArrayNotEmpty], 'steps.step' => ['required', 'array', new ArrayNotEmpty],
'steps.*' => 'nullable|string', 'steps.step.*' => 'nullable|string',
'steps.original_key' => 'nullable|array',
]); ]);
$recipe->fill([ $recipe->fill([
@ -183,16 +206,27 @@ class RecipeController extends Controller
$steps = []; $steps = [];
$number = 1; $number = 1;
// TODO: Handle removals.
foreach (array_filter($input['steps']) as $key => $step) { // Delete any removed steps.
$steps[$key] = $recipe->steps[$key] ?? new RecipeStep(); $removed = array_diff($recipe->steps->keys()->all(), $input['steps']['original_key']);
foreach ($removed as $removed_key) {
$recipe->steps[$removed_key]->delete();
}
foreach (array_filter($input['steps']['step']) as $key => $step) {
if (!is_null($input['steps']['original_key'][$key])) {
$steps[$key] = $recipe->steps[$input['steps']['original_key'][$key]];
}
else {
$steps[$key] = new RecipeStep();
}
$steps[$key]->fill(['number' => $number++, 'step' => $step]); $steps[$key]->fill(['number' => $number++, 'step' => $step]);
} }
$recipe->foodAmounts()->saveMany($steps); $recipe->steps()->saveMany($steps);
}); });
} catch (\Exception $e) { } catch (\Exception $e) {
DB::rollBack(); DB::rollBack();
return back()->withInput()->withErrors("Failed to updated recipe due to database error: {$e->getMessage()}."); return back()->withInput()->withErrors($e->getMessage());
} }
session()->flash('message', "Recipe {$recipe->name} updated!"); session()->flash('message', "Recipe {$recipe->name} updated!");

View File

@ -80,16 +80,9 @@
<!-- Steps --> <!-- Steps -->
<h3 class="pt-2 mb-2 font-extrabold">Steps</h3> <h3 class="pt-2 mb-2 font-extrabold">Steps</h3>
<div x-data="{ steps: 0 }"> <div x-data="{ steps: 0 }">
@if(old('steps')) @foreach($steps as $step)
@foreach(old('steps') as $i => $step_default) @include('recipes.partials.step-input', $step)
@if (empty($step)) @continue @endif @endforeach
@include('recipes.partials.step-input')
@endforeach
@else
@foreach($recipe->steps as $step)
@include('recipes.partials.step-input', ['step_default' => $step->step])
@endforeach
@endif
<template x-for="i in steps + 1"> <template x-for="i in steps + 1">
@include('recipes.partials.step-input') @include('recipes.partials.step-input')
</template> </template>

View File

@ -1,6 +1,7 @@
<div class="flex flex-row space-x-4 mb-4"> <div class="flex flex-row space-x-4 mb-4">
<x-inputs.input type="hidden" name="steps[original_key][]" :value="$original_key ?? null" />
<div class="text-3xl text-gray-400 text-center">#</div> <div class="text-3xl text-gray-400 text-center">#</div>
<x-inputs.textarea class="block mt-1 w-full" name="steps[]" :value="$step_default ?? null" /> <x-inputs.textarea class="block mt-1 w-full" name="steps[step][]" :value="$step_default ?? null" />
<x-inputs.icon-button type="button" color="red" x-on:click="$event.target.parentNode.remove();"> <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"> <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" /> <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" />