118 lines
4.8 KiB
PHP
118 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Settings;
|
|
|
|
use App\Services\UserAdminService;
|
|
use App\Services\UserService;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\CreateAction;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Filament\Schemas\Components\EmbeddedTable;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Support\Enums\FontWeight;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Concerns\InteractsWithTable;
|
|
use Filament\Tables\Contracts\HasTable;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use UnitEnum;
|
|
|
|
class Users extends Page implements HasTable
|
|
{
|
|
use InteractsWithTable;
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-user-group';
|
|
|
|
protected static string|UnitEnum|null $navigationGroup = 'Pengaturan';
|
|
|
|
protected static ?int $navigationSort = 0;
|
|
|
|
protected static ?string $navigationLabel = 'Pengguna';
|
|
|
|
protected static ?string $slug = 'settings/users';
|
|
|
|
protected string $view = 'filament.pages.blank';
|
|
|
|
public function getTitle(): string|Htmlable
|
|
{
|
|
return 'Manajemen Pengguna';
|
|
}
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
return Gate::check('manage-users');
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
$service = app(UserService::class);
|
|
|
|
return $table
|
|
->records(fn (?string $search, ?string $sortColumn, ?string $sortDirection, int|string $page, int|string $recordsPerPage) => $service->paginate(
|
|
search: $search,
|
|
sortColumn: $sortColumn,
|
|
sortDirection: $sortDirection,
|
|
page: (int) $page,
|
|
perPage: (int) $recordsPerPage,
|
|
))
|
|
->columns([
|
|
TextColumn::make('name')->label('Nama')->weight(FontWeight::SemiBold),
|
|
TextColumn::make('email')->label('Email'),
|
|
TextColumn::make('role')
|
|
->label('Role')
|
|
->badge()
|
|
->color(fn (string $state): string => $state === 'admin' ? 'danger' : 'info'),
|
|
IconColumn::make('is_active')->label('Aktif')->boolean(),
|
|
])
|
|
->recordActions([
|
|
Action::make('role')
|
|
->label('Ubah Role')
|
|
->schema(fn (Schema $schema): Schema => $schema->components([
|
|
Select::make('role')->label('Role')->options(['admin' => 'Admin', 'operator' => 'Operator'])->required(),
|
|
]))
|
|
->fillForm(fn (array $record): array => ['role' => $record['role']])
|
|
->action(function (array $data, array $record): void {
|
|
app(UserAdminService::class)->setRole($record['id'], $data['role'], auth()->user());
|
|
Notification::make()->title('Role diperbarui.')->success()->send();
|
|
}),
|
|
Action::make('toggle_aktif')
|
|
->label(fn (array $record): string => ($record['is_active'] ?? false) ? 'Nonaktifkan' : 'Aktifkan')
|
|
->color(fn (array $record): bool|string => ($record['is_active'] ?? false) ? 'danger' : 'success')
|
|
->requiresConfirmation()
|
|
->action(function (array $record): void {
|
|
app(UserAdminService::class)->setActive($record['id'], ! ($record['is_active'] ?? false), auth()->user());
|
|
Notification::make()->title('Status user diperbarui.')->success()->send();
|
|
}),
|
|
])
|
|
->headerActions([
|
|
CreateAction::make()
|
|
->label('Tambah User')
|
|
->schema(fn (Schema $schema): Schema => $schema->components([
|
|
TextInput::make('name')->label('Nama')->required(),
|
|
TextInput::make('email')->label('Email')->email()->required(),
|
|
TextInput::make('password')->label('Password')->password()->minLength(8)->required(),
|
|
Select::make('role')->label('Role')->options(['operator' => 'Operator', 'admin' => 'Admin'])->default('operator')->required(),
|
|
]))
|
|
->using(function (array $data): void {
|
|
app(UserAdminService::class)->createUser($data, auth()->user());
|
|
Notification::make()->title('User dibuat & terverifikasi.')->success()->send();
|
|
}),
|
|
])
|
|
->paginated([10, 25]);
|
|
}
|
|
|
|
public function content(Schema $schema): Schema
|
|
{
|
|
return $schema
|
|
->components([
|
|
EmbeddedTable::make(),
|
|
]);
|
|
}
|
|
}
|