Skip to content

Commit

Permalink
feat(filament): added a refresh for relation managers after every act…
Browse files Browse the repository at this point in the history
…ion (#703)
  • Loading branch information
Kyrch committed Jun 28, 2024
1 parent ea5683d commit 483bec0
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 25 deletions.
54 changes: 40 additions & 14 deletions app/Concerns/Filament/Actions/HasPivotActionLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Admin\ActionLog;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* Trait HasPivotActionLogs.
Expand All @@ -26,20 +27,45 @@ public function pivotActionLog(string $actionName, BaseRelationManager $livewire
{
$ownerRecord = $livewire->getOwnerRecord();

/** @var BelongsToMany */
$relation = $livewire->getRelationship();
$pivotClass = $relation->getPivotClass();

$pivot = $pivotClass::query()
->where($ownerRecord->getKeyName(), $ownerRecord->getKey())
->where($record->getKeyName(), $record->getKey())
->first();

ActionLog::modelPivot(
$actionName,
$livewire->getOwnerRecord(),
$record,
$pivot,
);

if ($relation instanceof BelongsToMany) {
$pivotClass = $relation->getPivotClass();

$pivot = $pivotClass::query()
->where($ownerRecord->getKeyName(), $ownerRecord->getKey())
->where($record->getKeyName(), $record->getKey())
->first();

ActionLog::modelPivot(
$actionName,
$ownerRecord,
$record,
$pivot,
);
}
}

/**
* Create the associate action log.
*
* @param string $actionName
* @param BaseRelationManager $livewire
* @param Model $record
* @return void
*/
public function associateActionLog(string $actionName, BaseRelationManager $livewire, Model $record): void
{
$ownerRecord = $livewire->getOwnerRecord();

$relation = $livewire->getRelationship();

if ($relation instanceof HasMany) {
ActionLog::modelAssociated(
$actionName,
$ownerRecord,
$record,
);
}
}
}
11 changes: 10 additions & 1 deletion app/Filament/Actions/Base/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Concerns\Filament\Actions\HasPivotActionLogs;
use App\Filament\RelationManagers\BaseRelationManager;
use Filament\Tables\Actions\CreateAction as DefaultCreateAction;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* Class CreateAction.
Expand All @@ -26,7 +28,14 @@ protected function setUp(): void

$this->after(function ($livewire, $record) {
if ($livewire instanceof BaseRelationManager) {
$this->pivotActionLog('Create and Attach', $livewire, $record);
$relationship = $livewire->getRelationship();
if ($relationship instanceof BelongsToMany) {
$this->pivotActionLog('Create and Attach', $livewire, $record);
}

if ($relationship instanceof HasMany) {
$this->associateActionLog('Create and Associate', $livewire, $record);
}
}
});
}
Expand Down
11 changes: 9 additions & 2 deletions app/Filament/Actions/Base/DetachAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Concerns\Filament\Actions\HasPivotActionLogs;
use App\Filament\RelationManagers\BaseRelationManager;
use Filament\Tables\Actions\DetachAction as DefaultDetachAction;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* Class DetachAction.
Expand All @@ -26,10 +27,16 @@ protected function setUp(): void

$this->label(__('filament.actions.base.detach'));

$this->hidden(fn ($livewire) => !($livewire instanceof BaseRelationManager));
$this->hidden(fn ($livewire) => !($livewire instanceof BaseRelationManager && $livewire->getRelationship() instanceof BelongsToMany));

$this->authorize('delete');

$this->after(fn ($livewire, $record) => $this->pivotActionLog('Detach', $livewire, $record));
$this->after(function ($livewire, $record) {
$relationship = $livewire->getRelationship();

if ($relationship instanceof BelongsToMany) {
$this->pivotActionLog('Detach', $livewire, $record);
}
});
}
}
5 changes: 4 additions & 1 deletion app/Filament/Actions/Base/EditAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Concerns\Filament\Actions\HasPivotActionLogs;
use App\Filament\RelationManagers\BaseRelationManager;
use Filament\Tables\Actions\EditAction as DefaultEditAction;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

/**
* Class EditAction.
Expand All @@ -28,7 +29,9 @@ protected function setUp(): void

$this->after(function ($livewire, $record) {
if ($livewire instanceof BaseRelationManager) {
$this->pivotActionLog('Update Attached', $livewire, $record);
if ($livewire->getRelationship() instanceof BelongsToMany) {
$this->pivotActionLog('Update Attached', $livewire, $record);
}
}
});
}
Expand Down
12 changes: 9 additions & 3 deletions app/Filament/Actions/BaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ protected function setUp(): void

$this->requiresConfirmation();

$this->afterFormValidated(fn (BaseAction $action) => $this->createActionLog($action));

$this->after(fn () => $this->finishedLog());
$this->afterFormValidated(function (BaseAction $action, $livewire) {
$this->createActionLog($action);
$livewire->dispatch('updateAllRelationManager');
});

$this->after(function ($livewire) {
$this->finishedLog();
$livewire->dispatch('updateAllRelationManager');
});

$this->modalWidth(MaxWidth::FourExtraLarge);
}
Expand Down
12 changes: 9 additions & 3 deletions app/Filament/HeaderActions/BaseHeaderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ protected function setUp(): void

$this->requiresConfirmation();

$this->afterFormValidated(fn (BaseHeaderAction $action) => $this->createActionLog($action));

$this->after(fn () => $this->finishedLog());
$this->afterFormValidated(function (BaseHeaderAction $action, $livewire) {
$this->createActionLog($action);
$livewire->dispatch('updateAllRelationManager');
});

$this->after(function ($livewire) {
$this->finishedLog();
$livewire->dispatch('updateAllRelationManager');
});

$this->modalWidth(MaxWidth::FourExtraLarge);
}
Expand Down
2 changes: 2 additions & 0 deletions app/Filament/RelationManagers/BaseRelationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract class BaseRelationManager extends RelationManager
{
protected static bool $isLazy = false;

protected $listeners = ['updateAllRelationManager' => '$refresh'];

/**
* The actions should appear in the view page.
*
Expand Down
4 changes: 3 additions & 1 deletion app/Filament/TableActions/BaseTableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ protected function setUp(): void
$this->afterFormValidated(function ($livewire, BaseTableAction $action) {
if ($livewire instanceof BaseRelationManager) {
$this->createActionLog($action, $livewire->getOwnerRecord());
$livewire->dispatch('updateAllRelationManager');
}
});

$this->after(function ($livewire, BaseTableAction $action) {
$this->after(function ($livewire) {
if ($livewire instanceof BaseRelationManager) {
$this->finishedLog();
$livewire->dispatch('updateAllRelationManager');
}
});

Expand Down
25 changes: 25 additions & 0 deletions app/Models/Admin/ActionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,31 @@ public static function modelPivot(string $action, Model $related, Model $parent,
]);
}

/**
* Register an action log for a model associated (HasMany).
*
* @param string $action
* @param Model $related
* @param Model $parent
* @return ActionLog
*/
public static function modelAssociated(string $action, Model $related, Model $parent): ActionLog
{
return ActionLog::query()->create([
ActionLog::ATTRIBUTE_BATCH_ID => Str::orderedUuid()->__toString(),
ActionLog::ATTRIBUTE_USER => ActionLog::getUser()->getKey(),
ActionLog::ATTRIBUTE_NAME => $action,
ActionLog::ATTRIBUTE_ACTIONABLE_TYPE => $related->getMorphClass(),
ActionLog::ATTRIBUTE_ACTIONABLE_ID => $related->getKey(),
ActionLog::ATTRIBUTE_TARGET_TYPE => $parent->getMorphClass(),
ActionLog::ATTRIBUTE_TARGET_ID => $parent->getKey(),
ActionLog::ATTRIBUTE_MODEL_TYPE => $related->getMorphClass(),
ActionLog::ATTRIBUTE_MODEL_ID => $related->getKey(),
ActionLog::ATTRIBUTE_STATUS => ActionLogStatus::FINISHED->value,
ActionLog::ATTRIBUTE_FINISHED_AT => Date::now(),
]);
}

/**
* Register an action log for when a model has an action executed.
*
Expand Down

0 comments on commit 483bec0

Please sign in to comment.