Refactor Goal attribute names

This commit is contained in:
Christopher C. Wells 2021-02-13 12:49:50 -08:00
parent eee1cfaaf1
commit 755ef60561
6 changed files with 34 additions and 40 deletions

View File

@ -51,7 +51,7 @@ class GoalController extends Controller
{
return view('goals.edit')
->with('goal', $goal)
->with('attributeOptions', Goal::getAttributeOptions());
->with('goalOptions', Goal::getGoalOptions());
}
/**
@ -62,8 +62,8 @@ class GoalController extends Controller
$attributes = $request->validate([
'from' => ['nullable', 'date'],
'to' => ['nullable', 'date'],
'attribute' => ['required', 'string'],
'goal' => ['required', 'numeric'],
'goal' => ['required', 'string'],
'amount' => ['required', 'numeric'],
]);
$goal->fill(array_filter($attributes))
->user()->associate(Auth::user());

View File

@ -6,7 +6,6 @@ use App\Support\Nutrients;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
/**
* App\Models\Goal
@ -15,15 +14,15 @@ use Illuminate\Support\Str;
* @property int $user_id
* @property \datetime|null $from
* @property \datetime|null $to
* @property string $attribute
* @property float $goal
* @property string $goal
* @property float $amount
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\User $user
* @method static \Illuminate\Database\Eloquent\Builder|Goal newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Goal newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Goal query()
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereAttribute($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereAmount($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereFrom($value)
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereGoal($value)
@ -33,7 +32,7 @@ use Illuminate\Support\Str;
* @method static \Illuminate\Database\Eloquent\Builder|Goal whereUserId($value)
* @mixin \Eloquent
*/
class Goal extends Model
final class Goal extends Model
{
use HasFactory;
@ -43,8 +42,8 @@ class Goal extends Model
protected $fillable = [
'from',
'to',
'attribute',
'goal',
'amount',
];
/**
@ -53,7 +52,7 @@ class Goal extends Model
protected $casts = [
'from' => 'datetime:Y-m-d',
'to' => 'datetime:Y-m-d',
'goal' => 'float',
'amount' => 'float',
];
/**
@ -64,20 +63,18 @@ class Goal extends Model
}
/**
* Get options for the "attribute" column.
* Get options for the "goal" column.
*
* @return array
*/
public static function getAttributeOptions(): array {
public static function getGoalOptions(): array {
$options = [];
foreach (Nutrients::$all as $nutrient) {
foreach (['daily', 'weekly', 'monthly', 'yearly'] as $frequency) {
$key = "{$nutrient['value']}_{$frequency}";
$options[$key] = [
'value' => $key,
'label' => Str::ucfirst("{$frequency} {$nutrient['value']}")
];
}
$key = "{$nutrient['value']}_per_day";
$options[$key] = [
'value' => $key,
'label' => "{$nutrient['value']} per day",
];
}
return $options;
}

View File

@ -19,8 +19,8 @@ class CreateGoalsTable extends Migration
$table->foreignIdFor(User::class)->constrained()->cascadeOnUpdate()->cascadeOnDelete();
$table->date('from')->nullable();
$table->date('to')->nullable();
$table->string('attribute');
$table->unsignedFloat('goal');
$table->string('goal');
$table->unsignedFloat('amount');
$table->timestamps();
$table->index('user_id');
});

View File

@ -1,7 +1,7 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
Delete {{ $goal->attribute }} Goal?
Delete {{ $goal->goal }} goal?
</h2>
</x-slot>
@ -13,7 +13,7 @@
@method('delete')
@csrf
<div class="text-lg pb-3">
Are you sure what to delete your <span class="font-extrabold">{{ $goal->attribute }}</span> goal?
Are you sure what to delete your <span class="font-extrabold">{{ $goal->goal }}</span> goal?
</div>
<x-inputs.button class="bg-red-800 hover:bg-red-700">
Yes, delete

View File

@ -32,27 +32,24 @@
:value="old('to', $goal->to)" />
</div>
<!-- Attribute -->
<!-- Amount -->
<div class="flex-auto">
<x-inputs.label for="attribute" value="Attribute"/>
<x-inputs.select name="attribute"
class="block mt-1 w-full"
:options="$attributeOptions"
:selectedValue="old('attribute', $goal->attribute)">
<option value=""></option>
</x-inputs.select>
<x-inputs.label for="amount" value="Amount" />
<x-inputs.input name="amount"
type="number"
step="any"
class="block w-full"
:value="old('amount', $goal->amount)"/>
</div>
<!-- Goal -->
<div class="flex-auto">
<x-inputs.label for="goal" value="Goal"/>
<x-inputs.input name="goal"
type="number"
step="any"
class="block mt-1 w-full"
:value="old('goal', $goal->goal)"/>
<x-inputs.label for="goal" value="Goal" />
<x-inputs.select name="goal"
class="block w-full"
:options="$goalOptions"
:selectedValue="old('goal', $goal->goal)">
</x-inputs.select>
</div>
</div>
</div>

View File

@ -1,7 +1,7 @@
<x-app-layout>
<x-slot name="header">
<div class="flex justify-between items-center">
<h2 class="font-semibold text-2xl text-gray-800 leading-tight">Goals</h2>
<h2 class="font-semibold text-2xl text-gray-800 leading-tight">{{ Auth::user()->name }}'s Goals</h2>
<a href="{{ route('goals.create') }}" class="inline-flex items-center rounded-md font-semibold text-white p-2 bg-green-500 tracking-widest hover:bg-green-700 active:bg-green-900 focus:outline-none focus:border-green-900 focus:ring ring-green-600 disabled:opacity-25 transition ease-in-out duration-150">
<svg class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />