mirror of https://github.com/kcal-app/kcal.git
Handle meals form
This commit is contained in:
parent
eca41e89db
commit
8ab11d5456
|
|
@ -2,9 +2,11 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Rules\ArrayNotEmpty;
|
||||
use App\Support\ArrayFormat;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\Client\Request;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class MealsController extends Controller
|
||||
|
|
@ -23,8 +25,17 @@ class MealsController extends Controller
|
|||
*/
|
||||
public function update(Request $request): RedirectResponse
|
||||
{
|
||||
// @todo Handle meals update request.
|
||||
Auth::user()->refresh();
|
||||
$attributes = $request->validate([
|
||||
'meal' => ['required', new ArrayNotEmpty],
|
||||
'meal.value.*' => ['required', 'numeric'],
|
||||
'meal.weight.*' => ['required', 'numeric'],
|
||||
'meal.label.*' => ['nullable', 'string'],
|
||||
'meal.enabled.*' => ['required', 'boolean'],
|
||||
]);
|
||||
|
||||
$user = Auth::user();
|
||||
$user->meals = ArrayFormat::flipTwoDimensionalKeys($attributes['meal']);
|
||||
$user->save();
|
||||
session()->flash('message', "Meals customizations updated!");
|
||||
return redirect()->route('meals.edit');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ final class User extends Authenticatable implements HasMedia
|
|||
'value' => $i,
|
||||
'label' => 'Meal ' . ($i + 1),
|
||||
'weight' => $i,
|
||||
'active' => $i < 3,
|
||||
'enabled' => $i < 3,
|
||||
]);
|
||||
}
|
||||
return $meals;
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -135,7 +135,7 @@
|
|||
</section>
|
||||
</div>
|
||||
<div class="w-full sm:w-3/5 md:w-2/3 lg:w-3/4 flex flex-col space-y-4">
|
||||
@foreach(\Illuminate\Support\Facades\Auth::user()->meals->where('active')->sortBy('weight') as $meal)
|
||||
@foreach(\Illuminate\Support\Facades\Auth::user()->meals->where('enabled')->sortBy('weight') as $meal)
|
||||
<div>
|
||||
<h3 class="font-semibold text-lg text-gray-800">
|
||||
<div class="flex items-center">
|
||||
|
|
|
|||
|
|
@ -7,24 +7,36 @@
|
|||
@method('put')
|
||||
@csrf
|
||||
<div class="flex flex-col space-y-4">
|
||||
<div class="flex flex-row space-x-4 w-full items-center bg-gray-200 text-gray-600 uppercase text-sm leading-normal font-bold">
|
||||
<div class="w-1/6 sm:w-1/12 p-2"> </div>
|
||||
<div class="w-4/6 sm:w-5/6 p-2">Meal name</div>
|
||||
<div class="w-1/6 sm:w-1/12 p-2 text-center">Active</div>
|
||||
</div>
|
||||
<div x-data class="meals space-y-4">
|
||||
@foreach($meals as $key => $meal)
|
||||
<div class="meal draggable w-full">
|
||||
<x-inputs.input type="hidden" name="meal[value][]" :value="$key" />
|
||||
<x-inputs.input type="hidden" name="meal[value][]" :value="$meal['value']" />
|
||||
<x-inputs.input type="hidden" name="meal[weight][]" :value="$meal['weight'] ?? null" />
|
||||
<div class="flex flex-row space-x-4 w-full items-center">
|
||||
<div class="draggable-handle self-center text-gray-500 bg-gray-100 w-auto p-2 cursor-move">
|
||||
<svg class="h-6 w-6 mx-auto" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
<div class="w-1/6 sm:w-1/12">
|
||||
<div class="draggable-handle self-center text-gray-500 bg-gray-100 p-2 cursor-move">
|
||||
<svg class="h-6 w-6 mx-auto" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
|
||||
<path fill-rule="evenodd" d="M3 5a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 10a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1zM3 15a1 1 0 011-1h12a1 1 0 110 2H4a1 1 0 01-1-1z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
<x-inputs.input name="meal[label][]"
|
||||
type="text"
|
||||
size="5"
|
||||
placeholder="Breakfast, lunch, dinner, etc."
|
||||
class="block w-full"
|
||||
class="block w-4/6 sm:w-5/6"
|
||||
:value="$meal['label'] ?? null" />
|
||||
<x-inputs.input type="checkbox" name="meal[enabled][]" value="1" checked="checked" />
|
||||
<div class="w-1/6 sm:w-1/12 text-center">
|
||||
<x-inputs.input name="meal[enabled][]"
|
||||
type="checkbox"
|
||||
value="1"
|
||||
:checked="$meal['enabled'] ?? null" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endforeach
|
||||
|
|
|
|||
Loading…
Reference in New Issue