Skip to content

Commit

Permalink
Merge pull request #16 from kenepa/3.x
Browse files Browse the repository at this point in the history
Filament v3 support
  • Loading branch information
Jehizkia authored Aug 8, 2023
2 parents 58fec96 + 77c0d87 commit c1cb85a
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 56 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ saves or discards their changes.

## Installation

> Version 1.x supports Filament v2.
> Version 2.x supports Filament v3.
You can install the package via composer:

```bash
Expand All @@ -29,6 +32,19 @@ Then run the installation command to publish and run migration(s)
php artisan resource-lock:install
```

Register plugin with a panel
```php
use Kenepa\ResourceLock\ResourceLockPlugin;
use Filament\Panel;

public function panel(Panel $panel): Panel
{
return $panel
// ...
->plugin(ResourceLockPlugin::make());
}
```

You can publish run the config (optional)

```bash
Expand Down Expand Up @@ -139,7 +155,7 @@ the [Spatie Permissions package](https://github.com/spatie/laravel-permission).

'unlocker' => [
'limited_access' => true,
'gate' => 'unlock-resource'
'gate' => 'unlock'
],
```

Expand All @@ -148,12 +164,13 @@ Example
```php

// Example using gates
Gate::define('unlock-resource', function (User $user, Post $post) {
// More info about gates: https://laravel.com/docs/authorization#writing-gates
Gate::define('unlock', function (User $user, Post $post) {
return $user->email === '[email protected]';
});

// Example using spatie permission package
Permission::create(['name' => 'unlock-resource']);
Permission::create(['name' => 'unlock']);
```

### Using custom models
Expand Down
16 changes: 7 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@
"homepage": "https://github.com/kenepa/resource-lock",
"license": "MIT",
"require": {
"php": "^8.0",
"filament/filament": "^2.0",
"spatie/laravel-package-tools": "^1.13.5",
"php": "^8.1",
"filament/filament": "^3.0",
"spatie/laravel-package-tools": "^1.15.0",
"illuminate/contracts": "^9.0|^10.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.0|^7.0",
"nunomaduro/collision": "^7.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"pestphp/pest-plugin-livewire": "^1.0",
"pestphp/pest-plugin-parallel": "^0.3",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"pestphp/pest-plugin-livewire": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5|^10.0",
"spatie/laravel-ray": "^1.26",
"tightenco/duster": "^1.1"
},
Expand Down
20 changes: 10 additions & 10 deletions resources/views/components/resource-lock-observer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<script>
function resourceLockObserverInit() {
Livewire.emit('resourceLockObserver::init')
Livewire.dispatch('resourceLockObserver::init')
}
window.onbeforeunload = function () {
Livewire.emit('resourceLockObserver::unload')
Livewire.dispatch('resourceLockObserver::unload')
};
window.addEventListener('close-modal', event => {
if (event.detail.id.endsWith('-table-action')) {
Livewire.emit('resourceLockObserver::unload')
Livewire.dispatch('resourceLockObserver::unload')
}
})
</script>
Expand All @@ -37,19 +37,19 @@ function resourceLockObserverInit() {
<div x-data="{url: '/'}" @open-modal.window="(event) => { url = event.detail.returnUrl}" class="flex flex-col justify-center space-y-2">

@if ($isAllowedToUnlock)
<button wire:click="$emit('resourceLockObserver::unlock')" class="filament-button filament-button-size-md inline-flex items-center justify-center py-1 gap-1 font-medium rounded-lg border transition-colors outline-none focus:ring-offset-2 focus:ring-2 focus:ring-inset min-h-[2.25rem] px-4 text-sm text-white shadow focus:ring-white border-transparent bg-danger-600 hover:bg-danger-500 focus:bg-danger-700 focus:ring-offset-danger-700 filament-page-button-action">
<button wire:click="$dispatch('resourceLockObserver::unlock')" style="--c-400:var(--danger-400);--c-500:var(--danger-500);--c-600:var(--danger-600);" class="fi-btn fi-btn-size-md relative grid-flow-col items-center justify-center font-semibold outline-none transition duration-75 focus:ring-2 disabled:pointer-events-none disabled:opacity-70 rounded-lg fi-btn-color-danger gap-1.5 px-3 py-2 text-sm inline-grid shadow-sm bg-custom-600 text-white hover:bg-custom-500 dark:bg-custom-500 dark:hover:bg-custom-400 focus:ring-custom-500/50 dark:focus:ring-custom-400/50 fi-ac-btn-action">
{{ __('resource-lock::modal.unlock_button') }}
</button>
@endif

<a class="block filament-button filament-button-size-md inline-flex items-center justify-center py-1 gap-1 font-medium rounded-lg border transition-colors outline-none focus:ring-offset-2 focus:ring-2 focus:ring-inset min-h-[2.25rem] px-4 text-sm text-white shadow focus:ring-white border-transparent bg-primary-600 hover:bg-primary-500 focus:bg-primary-700 focus:ring-offset-primary-700 filament-page-button-action"
<a style="--c-400:var(--primary-400);--c-500:var(--primary-500);--c-600:var(--primary-600);"
class="fi-btn fi-btn-size-md relative grid-flow-col items-center justify-center font-semibold outline-none transition duration-75 focus:ring-2 disabled:pointer-events-none disabled:opacity-70 rounded-lg fi-btn-color-primary gap-1.5 px-3 py-2 text-sm inline-grid shadow-sm bg-custom-600 text-white hover:bg-custom-500 dark:bg-custom-500 dark:hover:bg-custom-400 focus:ring-custom-500/50 dark:focus:ring-custom-400/50 fi-ac-btn-action"
:href="url">

<span>
{{ __('resource-lock::modal.return_button') }}
</span>

<span>
{{ __('resource-lock::modal.return_button') }}
</span>
</a>

</div>
</x-filament::modal>
</div>
41 changes: 41 additions & 0 deletions src/ResourceLockPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Kenepa\ResourceLock;

use Filament\Contracts\Plugin;
use Filament\Panel;
use Filament\Support\Facades\FilamentView;
use Illuminate\Support\Facades\Blade;
use Kenepa\ResourceLock\Resources\ResourceLockResource;
use Livewire\Livewire;

class ResourceLockPlugin implements Plugin
{
public static function make(): static
{
return app(static::class);
}

public function getId(): string
{
return 'resource-lock';
}

public function register(Panel $panel): void
{
$panel
->resources([
ResourceLockResource::class,
]);
}

public function boot(Panel $panel): void
{
Livewire::component('resource-lock-observer', Http\Livewire\ResourceLockObserver::class);

FilamentView::registerRenderHook(
'panels::body.end',
fn (): string => Blade::render('@livewire(\'resource-lock-observer\')'),
);
}
}
24 changes: 2 additions & 22 deletions src/ResourceLockServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@

namespace Kenepa\ResourceLock;

use Filament\Facades\Filament;
use Filament\PluginServiceProvider;
use Illuminate\Support\Facades\Blade;
use Kenepa\ResourceLock\Resources\ResourceLockResource;
use Livewire\Livewire;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class ResourceLockServiceProvider extends PluginServiceProvider
class ResourceLockServiceProvider extends PackageServiceProvider
{
public static string $name = 'resource-lock';

protected array $resources = [
ResourceLockResource::class,
];

public function configurePackage(Package $package): void
{
$package->name(static::$name)
Expand All @@ -32,16 +24,4 @@ public function configurePackage(Package $package): void
->askToStarRepoOnGitHub('kenepa/resource-lock');
});
}

public function packageBooted(): void
{
parent::packageBooted();

Livewire::component('resource-lock-observer', Http\Livewire\ResourceLockObserver::class);

Filament::registerRenderHook(
'body.end',
fn (): string => Blade::render('@livewire(\'resource-lock-observer\')'),
);
}
}
18 changes: 12 additions & 6 deletions src/Resources/Pages/Concerns/UsesLocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,23 @@ protected function openLockedResourceModal(): void
{
$this->getResourceLockOwner();

$this->dispatchBrowserEvent('open-modal', [
'id' => 'resourceIsLockedNotice',
'returnUrl' => $this->resourceLockReturnUrl(),
'resourceLockOwner' => $this->resourceLockOwner,
]);
$this->dispatch(
'open-modal',
id: 'resourceIsLockedNotice',
returnUrl: $this->resourceLockReturnUrl(),
resourceLockOwner: $this->resourceLockOwner
);
}

protected function closeLockedResourceModal(): void
{
$this->dispatchBrowserEvent('close-modal', [
$this->dispatch('close-modal', [
'id' => 'resourceIsLockedNotice',
]);

$this->dispatch(
'close-modal',
id: 'resourceIsLockedNotice'
);
}
}
12 changes: 6 additions & 6 deletions src/Resources/ResourceLockResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Kenepa\ResourceLock\Resources;

use Filament\Resources\Form;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\BadgeColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Gate;
use Kenepa\ResourceLock\Models\ResourceLock;
use Kenepa\ResourceLock\Resources\ResourceLockResource\ManageResourceLocks;
Expand All @@ -26,7 +25,7 @@ public static function form(Form $form): Form
]);
}

public static function table(Table $table): Table
public static function table(Table $table): Tables\Table
{
return $table
->columns([
Expand All @@ -36,7 +35,8 @@ public static function table(Table $table): Table
TextColumn::make('lockable_type'),
TextColumn::make('created_at'),
TextColumn::make('updated_at'),
BadgeColumn::make('updated_at')->label('Expired')
TextColumn::make('updated_at')->label('Expired')
->badge()
->color(static function ($record): string {
if ($record->isExpired()) {
return 'warning';
Expand Down Expand Up @@ -112,7 +112,7 @@ public static function getNavigationGroup(): ?string
return config('resource-lock.manager.navigation_group');
}

protected static function getNavigationSort(): ?int
public static function getNavigationSort(): ?int
{
return config('resource-lock.manager.navigation_sort');
}
Expand Down

0 comments on commit c1cb85a

Please sign in to comment.