Add remaining fields for Goal edit form

This commit is contained in:
Christopher C. Wells 2021-05-02 09:41:20 -07:00 committed by Christopher Charbonneau Wells
parent 15762790a5
commit 3ae6c6ef13
4 changed files with 73 additions and 18 deletions

View File

@ -2,11 +2,10 @@
namespace App\Models; namespace App\Models;
use App\Support\Nutrients;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str; use Illuminate\Support\Collection;
/** /**
* App\Models\Goal * App\Models\Goal
@ -57,7 +56,7 @@ final class Goal extends Model
'name', 'name',
'from', 'from',
'to', 'to',
// Bitwise field: sun=1, mon=2, tue=4, wed=8, thu=16, fri=32, sat=64. // Bitwise field: mon=1, tue=2, wed=4, thu=8, fri=16, sat=32, sun=64.
'days', 'days',
'calories', 'calories',
'carbohydrates', 'carbohydrates',
@ -82,6 +81,47 @@ final class Goal extends Model
'sodium' => 'float', 'sodium' => 'float',
]; ];
/**
* Get all supported days and metadata.
*
* Each entry has the following keys:
* - value: Day value (used for bitwise operations).
* - label: Human-readable name for the day.
*/
public static function days(): Collection
{
return new Collection([
[
'value' => 1,
'label' => 'monday',
],
[
'value' => 2,
'label' => 'tuesday',
],
[
'value' => 4,
'label' => 'wednesday',
],
[
'value' => 8,
'label' => 'thursday',
],
[
'value' => 16,
'label' => 'friday',
],
[
'value' => 32,
'label' => 'saturday',
],
[
'value' => 64,
'label' => 'sunday',
],
]);
}
/** /**
* Get the User this goal belongs to. * Get the User this goal belongs to.
*/ */

View File

@ -13,13 +13,11 @@ class Nutrients
/** /**
* Get all supported units and metadata. * Get all supported units and metadata.
* *
* Each entry has two keys: * Each entry has the following keys:
* - value: Machine name for the unit. * - value: Machine name for the unit.
* - label: Human-readable name for the unit. * - label: Human-readable name for the unit.
* - plural: Human-readable plural form of the unit name. * - plural: Human-readable plural form of the unit name.
* - type: Unit type -- matching types can be converted. * - type: Unit type -- matching types can be converted.
*
* @return \Illuminate\Support\Collection
*/ */
public static function units(): Collection { public static function units(): Collection {
return new Collection([ return new Collection([

2
public/css/app.css vendored

File diff suppressed because one or more lines are too long

View File

@ -41,17 +41,34 @@
:hasError="$errors->has('to')" /> :hasError="$errors->has('to')" />
</div> </div>
<!-- Goal --> <!-- Days of the week -->
<div class="flex-auto"> <fieldset class="flex-auto">
<x-inputs.label for="goal" value="Goal" /> <legend class="block font-medium text-sm text-gray-700">Days of the week</legend>
<x-inputs.input name="goal" <div class="inline-flex justify-between divide-x border rounded-md shadow-sm border-gray-300">
type="number" @foreach(\App\Models\Goal::days() as $day)
step="any" <x-inputs.label class="inline-flex items-center p-2">
class="block w-full" <x-inputs.input type="checkbox" name="days[]" :value="$day['value']" />
:value="old('goal', $goal->goal)" <span class="ml-2">{{ \Illuminate\Support\Str::ucfirst($day['label']) }}</span>
:hasError="$errors->has('goal')" </x-inputs.label>
required /> @endforeach
</div> </div>
</fieldset>
</div>
<div class="flex flex-col space-y-4 md:flex-row md:space-y-0">
@foreach (\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
<!-- {{ ucfirst($nutrient['value']) }} -->
<div class="flex-auto">
<x-inputs.label for="{{ $nutrient['value'] }}"
:value="ucfirst($nutrient['value']) . ($nutrient['unit'] ? ' (' . $nutrient['unit'] . ')' : '')"/>
<x-inputs.input name="{{ $nutrient['value'] }}"
type="number"
step="any"
class="block w-full mt-1 md:w-5/6"
:value="old($nutrient['value'], $goal->{$nutrient['value']})"
:hasError="$errors->has($nutrient['value'])"/>
</div>
@endforeach
</div> </div>
</div> </div>