From affa9f3630e02725ae6d4144d5d3b8fdc2e678cd Mon Sep 17 00:00:00 2001 From: alexPopaCode4 <156655986+alexPopaCode4@users.noreply.github.com> Date: Tue, 16 Jan 2024 15:28:07 +0200 Subject: [PATCH] Add checkbox for never expire on document resource (#141) * Add checkbox for never expire on document resource * apply cs-fixer * fix: checkbox label * simplify callbacks * fix: group checkbox with expires_at --- app/Filament/Filters/ResourceTreeFilter.php | 1 - app/Filament/Resources/DocumentResource.php | 29 ++++++++++++++++--- app/Filament/Widgets/PlatformStatsWidget.php | 2 +- app/Http/Kernel.php | 16 +++++----- .../Middleware/RedirectIfAuthenticated.php | 2 +- app/Models/Scopes/AlphabeticalOrder.php | 4 +-- app/Rules/EnumCollection.php | 2 +- config/filament-breezy.php | 10 +++---- config/filament.php | 2 +- lang/ro/document.php | 1 + routes/filament.php | 2 +- 11 files changed, 46 insertions(+), 25 deletions(-) diff --git a/app/Filament/Filters/ResourceTreeFilter.php b/app/Filament/Filters/ResourceTreeFilter.php index 7d01e02..28c8aea 100644 --- a/app/Filament/Filters/ResourceTreeFilter.php +++ b/app/Filament/Filters/ResourceTreeFilter.php @@ -6,7 +6,6 @@ use App\Models\Resource\Category; use Closure; -use Filament\Forms\Components\Fieldset; use Filament\Forms\Components\Select; use Filament\Tables\Filters\BaseFilter; diff --git a/app/Filament/Resources/DocumentResource.php b/app/Filament/Resources/DocumentResource.php index 8929d66..0a3e993 100644 --- a/app/Filament/Resources/DocumentResource.php +++ b/app/Filament/Resources/DocumentResource.php @@ -9,9 +9,12 @@ use App\Filament\Resources\DocumentResource\Pages; use App\Filament\Tables\Actions\ExportAction; use App\Models\Document; +use Closure; use Filament\Forms\Components\Card; +use Filament\Forms\Components\Checkbox; use Filament\Forms\Components\DatePicker; use Filament\Forms\Components\Grid; +use Filament\Forms\Components\Group; use Filament\Forms\Components\Section; use Filament\Forms\Components\Select; use Filament\Forms\Components\SpatieMediaLibraryFileUpload; @@ -80,10 +83,28 @@ public static function form(Form $form): Form ->label(__('document.field.signed_at')) ->required(), - DatePicker::make('expires_at') - ->label(__('document.field.expires_at')) - ->after('signed_at') - ->required(), + Group::make() + ->schema([ + DatePicker::make('expires_at') + ->label(__('document.field.expires_at')) + ->after('signed_at') + ->required(fn (Closure $get) => ! $get('never_expires')) + ->disabled(fn (Closure $get) => (bool) $get('never_expires')) + ->afterStateHydrated(function (Closure $set, $state) { + if (blank($state)) { + $set('never_expires', true); + } + }), + + Checkbox::make('never_expires') + ->label(__('document.field.never_expires')) + ->afterStateUpdated(function (Closure $set, $state) { + if ($state === true) { + $set('expires_at', null); + } + }) + ->reactive(), + ]), ]), ]), diff --git a/app/Filament/Widgets/PlatformStatsWidget.php b/app/Filament/Widgets/PlatformStatsWidget.php index 91d1e41..50e2d3d 100644 --- a/app/Filament/Widgets/PlatformStatsWidget.php +++ b/app/Filament/Widgets/PlatformStatsWidget.php @@ -33,7 +33,7 @@ class PlatformStatsWidget extends BaseWidget /** * Available counties to select. * - * @var \Illuminate\Support\Collection + * @var Collection */ public Collection $counties; diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index eca0643..159d73c 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -17,11 +17,11 @@ class Kernel extends HttpKernel */ protected $middleware = [ // \App\Http\Middleware\TrustHosts::class, - \App\Http\Middleware\TrustProxies::class, + Middleware\TrustProxies::class, \Illuminate\Http\Middleware\HandleCors::class, - \App\Http\Middleware\PreventRequestsDuringMaintenance::class, + Middleware\PreventRequestsDuringMaintenance::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, - \App\Http\Middleware\TrimStrings::class, + Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, ]; @@ -32,11 +32,11 @@ class Kernel extends HttpKernel */ protected $middlewareGroups = [ 'web' => [ - \App\Http\Middleware\EncryptCookies::class, + Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, - \App\Http\Middleware\VerifyCsrfToken::class, + Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], @@ -55,14 +55,14 @@ class Kernel extends HttpKernel * @var array */ protected $middlewareAliases = [ - 'auth' => \App\Http\Middleware\Authenticate::class, + 'auth' => Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, - 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, + 'guest' => Middleware\RedirectIfAuthenticated::class, 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, - 'signed' => \App\Http\Middleware\ValidateSignature::class, + 'signed' => Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, ]; diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php index db7b0a0..0607807 100644 --- a/app/Http/Middleware/RedirectIfAuthenticated.php +++ b/app/Http/Middleware/RedirectIfAuthenticated.php @@ -14,7 +14,7 @@ class RedirectIfAuthenticated /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request + * @param Request $request * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next * @param string|null ...$guards * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse diff --git a/app/Models/Scopes/AlphabeticalOrder.php b/app/Models/Scopes/AlphabeticalOrder.php index ec4e158..e215753 100644 --- a/app/Models/Scopes/AlphabeticalOrder.php +++ b/app/Models/Scopes/AlphabeticalOrder.php @@ -23,8 +23,8 @@ public function __construct(string $column = 'name', string $direction = 'asc') /** * Apply the scope to a given Eloquent query builder. * - * @param \Illuminate\Database\Eloquent\Builder $builder - * @param \Illuminate\Database\Eloquent\Model $model + * @param Builder $builder + * @param Model $model * @return void */ public function apply(Builder $builder, Model $model): void diff --git a/app/Rules/EnumCollection.php b/app/Rules/EnumCollection.php index 4e2d989..f92cad7 100644 --- a/app/Rules/EnumCollection.php +++ b/app/Rules/EnumCollection.php @@ -14,7 +14,7 @@ class EnumCollection implements ValidationRule /** * The enum validation rule. * - * @var \Illuminate\Validation\Rules\Enum + * @var Enum */ protected Enum $rule; diff --git a/config/filament-breezy.php b/config/filament-breezy.php index a6fa1c4..382bf0d 100644 --- a/config/filament-breezy.php +++ b/config/filament-breezy.php @@ -82,27 +82,27 @@ |-------------------------------------------------------------------------- | Path to registration Livewire component. */ - 'registration_component_path' => \JeffGreco13\FilamentBreezy\Http\Livewire\Auth\Register::class, + 'registration_component_path' => JeffGreco13\FilamentBreezy\Http\Livewire\Auth\Register::class, /* |-------------------------------------------------------------------------- | Path to password reset Livewire component. */ - 'password_reset_component_path' => \JeffGreco13\FilamentBreezy\Http\Livewire\Auth\ResetPassword::class, + 'password_reset_component_path' => JeffGreco13\FilamentBreezy\Http\Livewire\Auth\ResetPassword::class, /* |-------------------------------------------------------------------------- | Path to email verification Livewire component. */ - 'email_verification_component_path' => \JeffGreco13\FilamentBreezy\Http\Livewire\Auth\Verify::class, + 'email_verification_component_path' => JeffGreco13\FilamentBreezy\Http\Livewire\Auth\Verify::class, /* |-------------------------------------------------------------------------- | Path to email verification Controller component. */ - 'email_verification_controller_path' => \JeffGreco13\FilamentBreezy\Http\Controllers\EmailVerificationController::class, + 'email_verification_controller_path' => JeffGreco13\FilamentBreezy\Http\Controllers\EmailVerificationController::class, /* |-------------------------------------------------------------------------- | Path to Profile page component. */ - 'profile_page_component_path' => \App\Filament\Pages\Auth\Settings::class, + 'profile_page_component_path' => App\Filament\Pages\Auth\Settings::class, /* |-------------------------------------------------------------------------- | Where to redirect the user after registration. diff --git a/config/filament.php b/config/filament.php index a48d113..97a9dbd 100644 --- a/config/filament.php +++ b/config/filament.php @@ -276,7 +276,7 @@ | */ - 'default_avatar_provider' => \Filament\AvatarProviders\UiAvatarsProvider::class, + 'default_avatar_provider' => Filament\AvatarProviders\UiAvatarsProvider::class, /* |-------------------------------------------------------------------------- diff --git a/lang/ro/document.php b/lang/ro/document.php index 8611bb9..5927914 100644 --- a/lang/ro/document.php +++ b/lang/ro/document.php @@ -16,6 +16,7 @@ 'signed_at' => 'Dată semnare', 'expires_at' => 'Dată expirare', 'document' => 'Document', + 'never_expires' => 'Încheiat pe perioadă nedeterminată', ], 'type' => [ diff --git a/routes/filament.php b/routes/filament.php index c6ef4d8..48f318b 100644 --- a/routes/filament.php +++ b/routes/filament.php @@ -4,4 +4,4 @@ use Illuminate\Support\Facades\Route; -Route::get('/welcome/{user}', \App\Http\Livewire\Welcome::class)->name('auth.welcome'); +Route::get('/welcome/{user}', App\Http\Livewire\Welcome::class)->name('auth.welcome');