From 309ec6810c39b189fb0fa33eb4791432d4d40632 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sun, 24 Jan 2021 13:07:42 -0800 Subject: [PATCH] Move date and meal fields to each line (WIP) Frontend only, no backend support yet. --- .../Controllers/JournalEntryController.php | 30 ++++++++------ app/Models/JournalEntry.php | 10 +++++ app/Rules/ArrayNotEmpty.php | 10 +---- app/Rules/InArray.php | 40 +++++++++++++++++++ app/Rules/StringIsDecimalOrFraction.php | 10 +---- app/Rules/UsesIngredientTrait.php | 10 +---- .../views/journal-entries/create.blade.php | 38 +++--------------- .../partials/entry-item-input.blade.php | 32 +++++++++++++-- 8 files changed, 109 insertions(+), 71 deletions(-) create mode 100644 app/Rules/InArray.php diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 09b06ba..3937935 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -9,6 +9,7 @@ use App\Models\Food; use App\Models\JournalEntry; use App\Models\Recipe; use App\Rules\ArrayNotEmpty; +use App\Rules\InArray; use App\Rules\StringIsDecimalOrFraction; use App\Rules\UsesIngredientTrait; use App\Support\Number; @@ -44,10 +45,18 @@ class JournalEntryController extends Controller $ingredients = []; if ($old = old('ingredients')) { foreach ($old['amount'] as $key => $amount) { - if (empty($amount) && empty($old['unit'][$key]) && empty($old['id'][$key])) { + if ( + empty($old['date'][$key]) + && empty($old['meal'][$key]) + && empty($amount) + && empty($old['unit'][$key]) + && empty($old['id'][$key]) + ) { continue; } $ingredients[] = [ + 'date' => $old['date'][$key], + 'meal' => $old['meal'][$key], 'amount' => $amount, 'unit' => $old['unit'][$key], 'id' => $old['id'][$key], @@ -59,12 +68,7 @@ class JournalEntryController extends Controller return view('journal-entries.create') ->with('ingredients', $ingredients) - ->with('meals', [ - ['value' => 'breakfast', 'label' => 'Breakfast'], - ['value' => 'lunch', 'label' => 'Lunch'], - ['value' => 'dinner', 'label' => 'Dinner'], - ['value' => 'snacks', 'label' => 'Snacks'], - ]) + ->with('meals', JournalEntry::$meals) ->with('units', [ ['value' => 'tsp', 'label' => 'tsp.'], ['value' => 'tbsp', 'label' => 'tbsp.'], @@ -81,12 +85,14 @@ class JournalEntryController extends Controller public function store(Request $request): RedirectResponse { $input = $request->validate([ - 'date' => 'required|date', - 'meal' => 'required|string', + 'ingredients.date' => ['required', 'array', new ArrayNotEmpty], + 'ingredients.date.*' => ['nullable', 'date', 'required_with:ingredients.id.*'], + 'ingredients.meal' => ['required', 'array', new ArrayNotEmpty], + 'ingredients.meal.*' => ['nullable', 'string', 'required_with:ingredients.id.*', new InArray(array_column(JournalEntry::$meals, 'value'))], 'ingredients.amount' => ['required', 'array', new ArrayNotEmpty], - 'ingredients.amount.*' => ['required_with:foods.*,recipes.*', 'nullable', new StringIsDecimalOrFraction], - 'ingredients.unit' => 'required|array', - 'ingredients.unit.*' => 'nullable|string', + 'ingredients.amount.*' => ['required_with:ingredients.id.*', '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], diff --git a/app/Models/JournalEntry.php b/app/Models/JournalEntry.php index 90c1fba..031453e 100644 --- a/app/Models/JournalEntry.php +++ b/app/Models/JournalEntry.php @@ -86,6 +86,16 @@ class JournalEntry extends Model */ protected $with = ['user', 'foods:id,name,slug', 'recipes:id,name,slug']; + /** + * Valid meal options. + */ + public static array $meals = [ + ['value' => 'breakfast', 'label' => 'Breakfast'], + ['value' => 'lunch', 'label' => 'Lunch'], + ['value' => 'dinner', 'label' => 'Dinner'], + ['value' => 'snacks', 'label' => 'Snacks'], + ]; + /** * Get the User this entry belongs to. */ diff --git a/app/Rules/ArrayNotEmpty.php b/app/Rules/ArrayNotEmpty.php index 395c254..f584c16 100644 --- a/app/Rules/ArrayNotEmpty.php +++ b/app/Rules/ArrayNotEmpty.php @@ -7,11 +7,7 @@ use Illuminate\Contracts\Validation\Rule; class ArrayNotEmpty implements Rule { /** - * Determine if the array is empty. - * - * @param string $attribute - * @param mixed $value - * @return bool + * {@inheritdoc} */ public function passes($attribute, $value): bool { @@ -19,9 +15,7 @@ class ArrayNotEmpty implements Rule } /** - * Get the validation error message. - * - * @return string + * {@inheritdoc} */ public function message(): string { diff --git a/app/Rules/InArray.php b/app/Rules/InArray.php new file mode 100644 index 0000000..04f1f62 --- /dev/null +++ b/app/Rules/InArray.php @@ -0,0 +1,40 @@ +array = $array; + } + + /** + * {@inheritdoc} + */ + public function passes($attribute, $value): bool + { + return in_array($value, $this->array); + } + + /** + * {@inheritdoc} + */ + public function message(): string + { + return 'Invalid :attribute value :input.'; + } +} diff --git a/app/Rules/StringIsDecimalOrFraction.php b/app/Rules/StringIsDecimalOrFraction.php index ba27a33..9a0c70e 100644 --- a/app/Rules/StringIsDecimalOrFraction.php +++ b/app/Rules/StringIsDecimalOrFraction.php @@ -8,11 +8,7 @@ use Illuminate\Contracts\Validation\Rule; class StringIsDecimalOrFraction implements Rule { /** - * Determine if the string is a decimal or fraction, excluding zero. - * - * @param string $attribute - * @param mixed $value - * @return bool + * {@inheritdoc} */ public function passes($attribute, $value): bool { @@ -27,9 +23,7 @@ class StringIsDecimalOrFraction implements Rule } /** - * Get the validation error message. - * - * @return string + * {@inheritdoc} */ public function message(): string { diff --git a/app/Rules/UsesIngredientTrait.php b/app/Rules/UsesIngredientTrait.php index 3f50ec1..4acfbd4 100644 --- a/app/Rules/UsesIngredientTrait.php +++ b/app/Rules/UsesIngredientTrait.php @@ -8,11 +8,7 @@ use Illuminate\Contracts\Validation\Rule; class UsesIngredientTrait implements Rule { /** - * Determine if the array is empty. - * - * @param string $attribute - * @param mixed $value - * @return bool + * {@inheritdoc} */ public function passes($attribute, $value): bool { @@ -23,9 +19,7 @@ class UsesIngredientTrait implements Rule } /** - * Get the validation error message. - * - * @return string + * {@inheritdoc} */ public function message(): string { diff --git a/resources/views/journal-entries/create.blade.php b/resources/views/journal-entries/create.blade.php index 0d556e1..d12174b 100644 --- a/resources/views/journal-entries/create.blade.php +++ b/resources/views/journal-entries/create.blade.php @@ -11,39 +11,13 @@
@csrf -
- -
- - - -
- - -
- - - - - -
-
- - -
+
- - - + + + + +
@foreach($ingredients as $ingredient) diff --git a/resources/views/journal-entries/partials/entry-item-input.blade.php b/resources/views/journal-entries/partials/entry-item-input.blade.php index 717d7be..6f0368e 100644 --- a/resources/views/journal-entries/partials/entry-item-input.blade.php +++ b/resources/views/journal-entries/partials/entry-item-input.blade.php @@ -1,10 +1,33 @@
+ +
+ +
+ + +
+ + + +
+ +
+ +
-
+ + +
- - + + +