From ea33a7c8b46a20ab6ab622bc7e65fc7e918468be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Wed, 9 Apr 2025 10:33:59 +0100 Subject: [PATCH] fix: shelter attributes actions --- .../Resources/ShelterAttributeResource.php | 28 ++++++++++-- .../Actions/ListAction.php | 45 +++++++++++++++++++ .../Actions/UnlistAction.php | 45 +++++++++++++++++++ .../Widgets/ReferToShelterWidget.php | 10 +++-- app/Livewire/RequestPage.php | 6 ++- app/Models/Shelter.php | 12 +++++ app/Models/ShelterAttribute.php | 24 ++++++++++ lang/en/app.php | 16 +++++++ .../shelter-radio-card-content.blade.php | 2 +- 9 files changed, 180 insertions(+), 8 deletions(-) create mode 100644 app/Filament/Admin/Resources/ShelterAttributeResource/Actions/ListAction.php create mode 100644 app/Filament/Admin/Resources/ShelterAttributeResource/Actions/UnlistAction.php diff --git a/app/Filament/Admin/Resources/ShelterAttributeResource.php b/app/Filament/Admin/Resources/ShelterAttributeResource.php index 0e58693..55d3e81 100644 --- a/app/Filament/Admin/Resources/ShelterAttributeResource.php +++ b/app/Filament/Admin/Resources/ShelterAttributeResource.php @@ -4,6 +4,8 @@ namespace App\Filament\Admin\Resources; +use App\Filament\Admin\Resources\ShelterAttributeResource\Actions\ListAction; +use App\Filament\Admin\Resources\ShelterAttributeResource\Actions\UnlistAction; use App\Filament\Admin\Resources\ShelterAttributeResource\Pages; use App\Forms\Components\TableRepeater; use App\Models\ShelterAttribute; @@ -104,7 +106,11 @@ public static function table(Table $table): Table TextColumn::make('shelter_variables_count') ->label(__('app.field.variables')) - ->counts('shelterVariables'), + ->counts('shelterVariables') + ->sortable() + ->numeric() + ->alignRight() + ->shrink(), // TextColumn::make('shelters_count') // ->label(__('app.field.usage')) @@ -113,13 +119,29 @@ public static function table(Table $table): Table IconColumn::make('is_enabled') ->label(__('app.field.active')) - ->boolean(), + ->boolean() + ->shrink(), + + IconColumn::make('is_listed') + ->label(__('app.field.is_listed')) + ->boolean() + ->shrink(), ]) ->filters([ // ]) ->actions([ - Tables\Actions\ViewAction::make(), + Tables\Actions\ActionGroup::make([ + Tables\Actions\ViewAction::make(), + + Tables\Actions\EditAction::make(), + + ListAction::make(), + + UnlistAction::make(), + + Tables\Actions\DeleteAction::make(), + ]), ]); } diff --git a/app/Filament/Admin/Resources/ShelterAttributeResource/Actions/ListAction.php b/app/Filament/Admin/Resources/ShelterAttributeResource/Actions/ListAction.php new file mode 100644 index 0000000..34d2fff --- /dev/null +++ b/app/Filament/Admin/Resources/ShelterAttributeResource/Actions/ListAction.php @@ -0,0 +1,45 @@ +visible(fn (ShelterAttribute $record) => $record->isUnlisted() && Filament::auth()->user()->can('update', $record)); + + $this->label(__('app.attribute.actions.list.button')); + + $this->color('success'); + + $this->icon('heroicon-o-eye'); + + $this->outlined(); + + $this->requiresConfirmation(); + + $this->modalHeading(__('app.attribute.actions.list.confirm.title')); + + $this->modalDescription(__('app.attribute.actions.list.confirm.description')); + + $this->action(function (ShelterAttribute $record) { + $record->list(); + $this->success(); + }); + + $this->successNotificationTitle(__('app.attribute.actions.list.confirm.success')); + } +} diff --git a/app/Filament/Admin/Resources/ShelterAttributeResource/Actions/UnlistAction.php b/app/Filament/Admin/Resources/ShelterAttributeResource/Actions/UnlistAction.php new file mode 100644 index 0000000..f4e4845 --- /dev/null +++ b/app/Filament/Admin/Resources/ShelterAttributeResource/Actions/UnlistAction.php @@ -0,0 +1,45 @@ +visible(fn (ShelterAttribute $record) => $record->isListed() && Filament::auth()->user()->can('update', $record)); + + $this->label(__('app.attribute.actions.unlist.button')); + + $this->color('danger'); + + $this->icon('heroicon-o-eye-slash'); + + $this->outlined(); + + $this->requiresConfirmation(); + + $this->modalHeading(__('app.attribute.actions.unlist.confirm.title')); + + $this->modalDescription(__('app.attribute.actions.unlist.confirm.description')); + + $this->action(function (ShelterAttribute $record) { + $record->unlist(); + $this->success(); + }); + + $this->successNotificationTitle(__('app.attribute.actions.unlist.confirm.success')); + } +} diff --git a/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php b/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php index 7fb714e..67057fd 100644 --- a/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php +++ b/app/Filament/Shelter/Resources/RequestResource/Widgets/ReferToShelterWidget.php @@ -40,7 +40,11 @@ public function table(Table $table): Table fn () => Shelter::query() ->whereListed() ->whereNot('id', Filament::getTenant()->id) - ->with('shelterVariables') + ->with([ + 'country', + 'location', + 'shelterVariables', + ]) ) ->contentGrid([ 'md' => 2, @@ -53,9 +57,9 @@ public function table(Table $table): Table ->weight(FontWeight::Bold) ->searchable(), - TextColumn::make('address') + TextColumn::make('full_address') ->color(Color::Gray) - ->searchable(), + ->searchable('address'), Split::make([ TextColumn::make('capacity') diff --git a/app/Livewire/RequestPage.php b/app/Livewire/RequestPage.php index 0d91d38..f2ebcc2 100644 --- a/app/Livewire/RequestPage.php +++ b/app/Livewire/RequestPage.php @@ -93,7 +93,11 @@ public function form(Form $form): Form { $shelters = Shelter::query() ->whereListed() - ->with('shelterVariables') + ->with([ + 'country', + 'location', + 'shelterVariables', + ]) ->when( data_get($this->data, 'filters.variables'), fn (Builder $query, array $variables) => $query->whereHasShelterVariables($variables) diff --git a/app/Models/Shelter.php b/app/Models/Shelter.php index 19b8440..10637a3 100644 --- a/app/Models/Shelter.php +++ b/app/Models/Shelter.php @@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Support\Str; class Shelter extends Model { @@ -116,6 +117,17 @@ public function availableCapacity(): Attribute ); } + public function fullAddress(): Attribute + { + return Attribute::make( + get: fn (mixed $value) => Str::trim(collect([ + $this->address, + $this->location->name, + $this->country->name, + ])->filter()->implode(', ')), + ); + } + public function isListed(): bool { return $this->is_listed; diff --git a/app/Models/ShelterAttribute.php b/app/Models/ShelterAttribute.php index 41d3794..7ed657d 100644 --- a/app/Models/ShelterAttribute.php +++ b/app/Models/ShelterAttribute.php @@ -67,4 +67,28 @@ public function scopeWhereListed(Builder $query): Builder ->where('is_enabled', true) ->where('is_listed', true); } + + public function isListed(): bool + { + return $this->is_listed; + } + + public function isUnlisted(): bool + { + return ! $this->isListed(); + } + + public function list(): bool + { + return $this->update([ + 'is_listed' => true, + ]); + } + + public function unlist(): bool + { + return $this->update([ + 'is_listed' => false, + ]); + } } diff --git a/lang/en/app.php b/lang/en/app.php index 68e5bee..f33ea46 100644 --- a/lang/en/app.php +++ b/lang/en/app.php @@ -500,6 +500,22 @@ 'success' => 'Attribute deactivated successfully.', ], ], + 'list' => [ + 'button' => 'List attribute', + 'confirm' => [ + 'description' => 'Once a attribute is listed, it will be displayed in the public request form. In order to eliminate the attribute from this lists, it needs to be unlisted.', + 'title' => 'List shelter attribute', + 'success' => 'Shelter attribute listed successfully.', + ], + ], + 'unlist' => [ + 'button' => 'Unlist attribute', + 'confirm' => [ + 'description' => 'Once a attribute is unlisted, it will no longer be displayed in the public request form. To include the attribute in this lists again, it needs to be listed.', + 'title' => 'Unlist shelter attribute', + 'success' => 'Shelter attribute unlisted successfully.', + ], + ], ], ], ]; diff --git a/resources/views/forms/components/shelter-radio-card-content.blade.php b/resources/views/forms/components/shelter-radio-card-content.blade.php index 3dda1ef..4269731 100644 --- a/resources/views/forms/components/shelter-radio-card-content.blade.php +++ b/resources/views/forms/components/shelter-radio-card-content.blade.php @@ -1,5 +1,5 @@
- {{ $shelter->address }} + {{ $shelter->full_address }}