Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chore] Badges labels and color updates #1363

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app/Enums/ResultStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ enum ResultStatus: string implements HasColor, HasLabel
case Failed = 'failed'; // a speedtest that failed to run successfully.
case Started = 'started'; // a speedtest that has been started by a worker but has not finish running.

public function getLabel(): ?string
{
return $this->name;
}

public function getColor(): ?string
{
return match ($this) {
Expand All @@ -24,4 +19,13 @@ public function getColor(): ?string
self::Started => 'warning',
};
}

public function getLabel(): ?string
{
return match ($this) {
self::Completed => 'Completed',
self::Failed => 'Failed',
self::Started => 'Started',
};
}
}
28 changes: 28 additions & 0 deletions app/Enums/UserRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasColor;
use Filament\Support\Contracts\HasLabel;

enum UserRole: string implements HasColor, HasLabel
{
case Admin = 'admin';
case User = 'user';

public function getColor(): ?string
{
return match ($this) {
self::Admin => 'success',
self::User => 'gray',
};
}

public function getLabel(): ?string
{
return match ($this) {
self::Admin => 'Admin',
self::User => 'User',
};
}
}
1 change: 1 addition & 0 deletions app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public static function table(Table $table): Table
->toggleable()
->toggledHiddenByDefault(),
Tables\Columns\TextColumn::make('status')
->badge()
->toggleable()
->sortable(),
Tables\Columns\IconColumn::make('scheduled')
Expand Down
34 changes: 18 additions & 16 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace App\Filament\Resources;

use App\Enums\UserRole;
use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\Pages\CreateUser;
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Helpers\TimeZoneHelper;
use App\Models\User;
use App\Settings\GeneralSettings;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\HtmlString;
use Illuminate\Validation\Rules\Password;
Expand Down Expand Up @@ -71,12 +75,8 @@ public static function form(Form $form): Form
Forms\Components\Section::make()
->schema([
Forms\Components\Select::make('role')
->options([
'admin' => 'Admin',
'user' => 'User',
])
->default('user')
->disabled(fn (): bool => ! auth()->user()->is_admin || auth()->user()->is_user)
->options(UserRole::class)
->disabled(fn (): bool => ! Auth::user()->is_admin)
->required(),
])
->columns(1)
Expand Down Expand Up @@ -106,6 +106,8 @@ public static function form(Form $form): Form

public static function table(Table $table): Table
{
$settings = new GeneralSettings();

return $table
->columns([
Tables\Columns\TextColumn::make('id')
Expand All @@ -116,20 +118,20 @@ public static function table(Table $table): Table
->searchable(),
Tables\Columns\TextColumn::make('role')
->badge()
->color(fn (string $state): string => match ($state) {
'admin' => 'success',
'user' => 'gray',
}),
->color(UserRole::class),
Tables\Columns\TextColumn::make('created_at')
->alignEnd()
->dateTime($settings->time_format ?? 'M j, Y G:i:s')
->timezone(TimeZoneHelper::displayTimeZone($settings)),
Tables\Columns\TextColumn::make('updated_at')
->label('Last updated')
->dateTime(),
->alignEnd()
->dateTime($settings->time_format ?? 'M j, Y G:i:s')
->timezone(TimeZoneHelper::displayTimeZone($settings))
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\SelectFilter::make('role')
->options([
'admin' => 'Admin',
'user' => 'User',
]),
->options(UserRole::class),
])
->actions([
Tables\Actions\ActionGroup::make([
Expand Down
7 changes: 4 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Models;

use App\Enums\UserRole;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -46,6 +46,7 @@ class User extends Authenticatable implements FilamentUser
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'role' => UserRole::class,
];

/**
Expand All @@ -62,7 +63,7 @@ public function canAccessPanel(Panel $panel): bool
protected function isAdmin(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): bool => $attributes['role'] == 'admin',
get: fn (): bool => $this->role === UserRole::Admin,
);
}

Expand All @@ -72,7 +73,7 @@ protected function isAdmin(): Attribute
protected function isUser(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): bool => $attributes['role'] == 'user',
get: fn (): bool => $this->role === UserRole::User,
);
}
}
Loading