Skip to content

Commit b4f1054

Browse files
alexjustesenjohndivamMohammad Salam
authored
[Chore] Refactor user resource (alexjustesen#1783)
* Improvements user resource (alexjustesen#1732) * Improve textInput of password,password_confirmation * Improve textInput of password,password_confirmation * Drop the label if the field --------- Co-authored-by: Mohammad Salam <[email protected]> Co-authored-by: Alex Justesen <[email protected]> * refactored user resource * group user resource table actions --------- Co-authored-by: John Divam <[email protected]> Co-authored-by: Mohammad Salam <[email protected]>
1 parent bc56830 commit b4f1054

File tree

2 files changed

+74
-81
lines changed

2 files changed

+74
-81
lines changed

app/Filament/Resources/UserResource.php

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use App\Enums\UserRole;
66
use App\Filament\Resources\UserResource\Pages;
7-
use App\Filament\Resources\UserResource\Pages\CreateUser;
8-
use App\Filament\Resources\UserResource\Pages\EditUser;
97
use App\Models\User;
108
use Filament\Forms;
119
use Filament\Forms\Form;
@@ -14,8 +12,6 @@
1412
use Filament\Tables\Table;
1513
use Illuminate\Support\Facades\Auth;
1614
use Illuminate\Support\Facades\Hash;
17-
use Illuminate\Support\HtmlString;
18-
use Illuminate\Validation\Rules\Password;
1915

2016
class UserResource extends Resource
2117
{
@@ -29,77 +25,72 @@ public static function form(Form $form): Form
2925
->schema([
3026
Forms\Components\Grid::make([
3127
'default' => 1,
32-
'md' => 3,
33-
])
28+
])->columnSpan([
29+
'lg' => 2,
30+
])->schema([
31+
Forms\Components\Section::make('Details')
32+
->columns([
33+
'default' => 1,
34+
'lg' => 2,
35+
])
36+
->schema([
37+
Forms\Components\TextInput::make('name')
38+
->required()
39+
->maxLength(255)
40+
->columnSpanFull(),
41+
42+
Forms\Components\TextInput::make('email')
43+
->email()
44+
->required()
45+
->maxLength(255)
46+
->columnSpanFull(),
47+
48+
Forms\Components\TextInput::make('password')
49+
->confirmed()
50+
->password()
51+
->revealable()
52+
->required(fn (string $context): bool => $context === 'create')
53+
->dehydrateStateUsing(fn ($state) => Hash::make($state))
54+
->dehydrated(fn ($state) => filled($state)),
55+
56+
Forms\Components\TextInput::make('password_confirmation')
57+
->password()
58+
->revealable(),
59+
60+
// ...
61+
]),
62+
]),
63+
64+
Forms\Components\Grid::make(1)
65+
->columnSpan(1)
3466
->schema([
35-
Forms\Components\Section::make()
67+
Forms\Components\Section::make('Platform')
3668
->schema([
37-
Forms\Components\TextInput::make('name')
38-
->required(),
39-
Forms\Components\TextInput::make('email')
40-
->required()
41-
->email()
42-
->unique(ignoreRecord: true),
43-
Forms\Components\TextInput::make('password')
69+
Forms\Components\Select::make('role')
70+
->label('Role')
71+
->default(UserRole::User)
72+
->options(UserRole::class)
4473
->required()
45-
->password()
46-
->dehydrateStateUsing(fn ($state) => Hash::make($state))
47-
->visible(fn ($livewire) => $livewire instanceof CreateUser)
48-
->rule(Password::default()),
49-
Forms\Components\TextInput::make('new_password')
50-
->password()
51-
->label('New Password')
52-
->nullable()
53-
->rule(Password::default())
54-
->visible(fn ($livewire) => $livewire instanceof EditUser)
55-
->dehydrated(false),
56-
Forms\Components\TextInput::make('new_password_confirmation')
57-
->password()
58-
->label('Confirm New Password')
59-
->rule('required', fn ($get) => (bool) $get('new_password'))
60-
->same('new_password')
61-
->visible(fn ($livewire) => $livewire instanceof EditUser)
62-
->dehydrated(false),
63-
])
64-
->columns(1)
65-
->columnSpan([
66-
'md' => 2,
74+
->disabled(fn (?User $record): bool => Auth::user()->role !== UserRole::Admin),
75+
76+
// ...
6777
]),
6878

69-
Forms\Components\Grid::make([
70-
'default' => 1,
71-
])
79+
Forms\Components\Section::make()
7280
->schema([
73-
Forms\Components\Section::make()
74-
->schema([
75-
Forms\Components\Select::make('role')
76-
->options(UserRole::class)
77-
->default(UserRole::User)
78-
->disabled(fn (): bool => ! Auth::user()->is_admin)
79-
->required(),
80-
])
81-
->columns(1)
82-
->columnSpan([
83-
'md' => 1,
84-
]),
85-
86-
Forms\Components\Section::make()
87-
->schema([
88-
Forms\Components\Placeholder::make('created_at')
89-
->content(fn ($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('&mdash;')),
90-
Forms\Components\Placeholder::make('updated_at')
91-
->content(fn ($record) => $record?->updated_at?->diffForHumans() ?? new HtmlString('&mdash;')),
92-
])
93-
->columns(1)
94-
->columnSpan([
95-
'md' => 1,
96-
]),
97-
])
98-
->columns(1)
99-
->columnSpan([
100-
'md' => 1,
81+
Forms\Components\Placeholder::make('created_at')
82+
->content(fn (?User $record): string => $record ? $record->created_at->diffForHumans() : '-'),
83+
84+
Forms\Components\Placeholder::make('updated_at')
85+
->content(fn (?User $record): string => $record ? $record->updated_at->diffForHumans() : '-'),
86+
87+
// ...
10188
]),
10289
]),
90+
])
91+
->columns([
92+
'default' => 1,
93+
'lg' => 3,
10394
]);
10495
}
10596

@@ -108,33 +99,45 @@ public static function table(Table $table): Table
10899
return $table
109100
->columns([
110101
Tables\Columns\TextColumn::make('id')
111-
->label('ID'),
102+
->label('ID')
103+
->sortable(),
104+
112105
Tables\Columns\TextColumn::make('name')
113106
->searchable(),
107+
114108
Tables\Columns\TextColumn::make('email')
115109
->searchable(),
110+
116111
Tables\Columns\TextColumn::make('role')
117112
->badge(),
113+
118114
Tables\Columns\TextColumn::make('created_at')
119115
->alignEnd()
120116
->dateTime(config('app.datetime_format'))
121-
->timezone(config('app.display_timezone')),
117+
->timezone(config('app.display_timezone'))
118+
->sortable()
119+
->toggleable(isToggledHiddenByDefault: false),
120+
122121
Tables\Columns\TextColumn::make('updated_at')
123122
->alignEnd()
124123
->dateTime(config('app.datetime_format'))
125124
->timezone(config('app.display_timezone'))
125+
->sortable()
126126
->toggleable(isToggledHiddenByDefault: true),
127+
128+
// ...
127129
])
128130
->filters([
129131
Tables\Filters\SelectFilter::make('role')
130132
->options(UserRole::class),
131133
])
132134
->actions([
133135
Tables\Actions\ActionGroup::make([
134-
Tables\Actions\ViewAction::make(),
135136
Tables\Actions\EditAction::make(),
136-
Tables\Actions\DeleteAction::make(),
137137
]),
138+
])
139+
->bulkActions([
140+
// ...
138141
]);
139142
}
140143

app/Filament/Resources/UserResource/Pages/EditUser.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Filament\Resources\UserResource;
66
use Filament\Actions;
77
use Filament\Resources\Pages\EditRecord;
8-
use Illuminate\Support\Facades\Hash;
98

109
class EditUser extends EditRecord
1110
{
@@ -17,13 +16,4 @@ protected function getHeaderActions(): array
1716
Actions\DeleteAction::make(),
1817
];
1918
}
20-
21-
public function beforeSave()
22-
{
23-
if (! array_key_exists('new_password', $this->data) || ! filled($this->data['new_password'])) {
24-
return;
25-
}
26-
27-
$this->record->password = Hash::make($this->data['new_password']);
28-
}
2919
}

0 commit comments

Comments
 (0)