Skip to content

Commit b5e3fc0

Browse files
authored
[Chore] Badges labels and color updates (alexjustesen#1363)
1 parent f8a2386 commit b5e3fc0

File tree

5 files changed

+60
-24
lines changed

5 files changed

+60
-24
lines changed

app/Enums/ResultStatus.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ enum ResultStatus: string implements HasColor, HasLabel
1111
case Failed = 'failed'; // a speedtest that failed to run successfully.
1212
case Started = 'started'; // a speedtest that has been started by a worker but has not finish running.
1313

14-
public function getLabel(): ?string
15-
{
16-
return $this->name;
17-
}
18-
1914
public function getColor(): ?string
2015
{
2116
return match ($this) {
@@ -24,4 +19,13 @@ public function getColor(): ?string
2419
self::Started => 'warning',
2520
};
2621
}
22+
23+
public function getLabel(): ?string
24+
{
25+
return match ($this) {
26+
self::Completed => 'Completed',
27+
self::Failed => 'Failed',
28+
self::Started => 'Started',
29+
};
30+
}
2731
}

app/Enums/UserRole.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
use Filament\Support\Contracts\HasColor;
6+
use Filament\Support\Contracts\HasLabel;
7+
8+
enum UserRole: string implements HasColor, HasLabel
9+
{
10+
case Admin = 'admin';
11+
case User = 'user';
12+
13+
public function getColor(): ?string
14+
{
15+
return match ($this) {
16+
self::Admin => 'success',
17+
self::User => 'gray',
18+
};
19+
}
20+
21+
public function getLabel(): ?string
22+
{
23+
return match ($this) {
24+
self::Admin => 'Admin',
25+
self::User => 'User',
26+
};
27+
}
28+
}

app/Filament/Resources/ResultResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public static function table(Table $table): Table
216216
->toggleable()
217217
->toggledHiddenByDefault(),
218218
Tables\Columns\TextColumn::make('status')
219+
->badge()
219220
->toggleable()
220221
->sortable(),
221222
Tables\Columns\IconColumn::make('scheduled')

app/Filament/Resources/UserResource.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
namespace App\Filament\Resources;
44

5+
use App\Enums\UserRole;
56
use App\Filament\Resources\UserResource\Pages;
67
use App\Filament\Resources\UserResource\Pages\CreateUser;
78
use App\Filament\Resources\UserResource\Pages\EditUser;
9+
use App\Helpers\TimeZoneHelper;
810
use App\Models\User;
11+
use App\Settings\GeneralSettings;
912
use Filament\Forms;
1013
use Filament\Forms\Form;
1114
use Filament\Resources\Resource;
1215
use Filament\Tables;
1316
use Filament\Tables\Table;
17+
use Illuminate\Support\Facades\Auth;
1418
use Illuminate\Support\Facades\Hash;
1519
use Illuminate\Support\HtmlString;
1620
use Illuminate\Validation\Rules\Password;
@@ -71,12 +75,8 @@ public static function form(Form $form): Form
7175
Forms\Components\Section::make()
7276
->schema([
7377
Forms\Components\Select::make('role')
74-
->options([
75-
'admin' => 'Admin',
76-
'user' => 'User',
77-
])
78-
->default('user')
79-
->disabled(fn (): bool => ! auth()->user()->is_admin || auth()->user()->is_user)
78+
->options(UserRole::class)
79+
->disabled(fn (): bool => ! Auth::user()->is_admin)
8080
->required(),
8181
])
8282
->columns(1)
@@ -106,6 +106,8 @@ public static function form(Form $form): Form
106106

107107
public static function table(Table $table): Table
108108
{
109+
$settings = new GeneralSettings();
110+
109111
return $table
110112
->columns([
111113
Tables\Columns\TextColumn::make('id')
@@ -116,20 +118,20 @@ public static function table(Table $table): Table
116118
->searchable(),
117119
Tables\Columns\TextColumn::make('role')
118120
->badge()
119-
->color(fn (string $state): string => match ($state) {
120-
'admin' => 'success',
121-
'user' => 'gray',
122-
}),
121+
->color(UserRole::class),
122+
Tables\Columns\TextColumn::make('created_at')
123+
->alignEnd()
124+
->dateTime($settings->time_format ?? 'M j, Y G:i:s')
125+
->timezone(TimeZoneHelper::displayTimeZone($settings)),
123126
Tables\Columns\TextColumn::make('updated_at')
124-
->label('Last updated')
125-
->dateTime(),
127+
->alignEnd()
128+
->dateTime($settings->time_format ?? 'M j, Y G:i:s')
129+
->timezone(TimeZoneHelper::displayTimeZone($settings))
130+
->toggleable(isToggledHiddenByDefault: true),
126131
])
127132
->filters([
128133
Tables\Filters\SelectFilter::make('role')
129-
->options([
130-
'admin' => 'Admin',
131-
'user' => 'User',
132-
]),
134+
->options(UserRole::class),
133135
])
134136
->actions([
135137
Tables\Actions\ActionGroup::make([

app/Models/User.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace App\Models;
44

5+
use App\Enums\UserRole;
56
use Filament\Models\Contracts\FilamentUser;
67
use Filament\Panel;
7-
// use Illuminate\Contracts\Auth\MustVerifyEmail;
88
use Illuminate\Database\Eloquent\Casts\Attribute;
99
use Illuminate\Database\Eloquent\Factories\HasFactory;
1010
use Illuminate\Foundation\Auth\User as Authenticatable;
@@ -46,6 +46,7 @@ class User extends Authenticatable implements FilamentUser
4646
protected $casts = [
4747
'email_verified_at' => 'datetime',
4848
'password' => 'hashed',
49+
'role' => UserRole::class,
4950
];
5051

5152
/**
@@ -62,7 +63,7 @@ public function canAccessPanel(Panel $panel): bool
6263
protected function isAdmin(): Attribute
6364
{
6465
return Attribute::make(
65-
get: fn (mixed $value, array $attributes): bool => $attributes['role'] == 'admin',
66+
get: fn (): bool => $this->role === UserRole::Admin,
6667
);
6768
}
6869

@@ -72,7 +73,7 @@ protected function isAdmin(): Attribute
7273
protected function isUser(): Attribute
7374
{
7475
return Attribute::make(
75-
get: fn (mixed $value, array $attributes): bool => $attributes['role'] == 'user',
76+
get: fn (): bool => $this->role === UserRole::User,
7677
);
7778
}
7879
}

0 commit comments

Comments
 (0)