Skip to content

Commit

Permalink
Trigger searchable when records are updated. (#1699)
Browse files Browse the repository at this point in the history
There are numerous places in the panel, like relationship managers,
which don't trigger it's owner record's Observers and as a consequence
it's not always clear if a searchable record has been updated on the
index.

Whilst the use of observers will still work this PR looks to also hook
into various `after` triggers in the panel to manually call `searchable`
on the affected model.
  • Loading branch information
alecritson authored May 7, 2024
1 parent f02eb78 commit 3e85ee9
Show file tree
Hide file tree
Showing 30 changed files with 498 additions and 23 deletions.
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ChildCollectionCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ChildCollectionCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/CollectionProductAttached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class CollectionProductAttached
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/CollectionProductDetached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class CollectionProductDetached
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/CustomerAddressEdited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class CustomerAddressEdited
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/CustomerUserEdited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class CustomerUserEdited
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ModelChannelsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ModelChannelsUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ModelMediaUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ModelMediaUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ModelPricesUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ModelPricesUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ModelUrlsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ModelUrlsUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ProductAssociationsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProductAssociationsUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ProductCollectionsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProductCollectionsUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ProductCustomerGroupsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProductCustomerGroupsUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ProductPricingUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProductPricingUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
21 changes: 21 additions & 0 deletions packages/admin/src/Events/ProductVariantOptionsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Lunar\Admin\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ProductVariantOptionsUpdated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Model $model
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Lunar\Admin\Events\ChildCollectionCreated;
use Lunar\Admin\Filament\Resources\CollectionResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\Tables\Actions\Collections\CreateChildCollection;
Expand Down Expand Up @@ -73,7 +74,9 @@ public function table(Table $table): Table
return CollectionResource::getUrl('edit', ['record' => $record]);
}),
])->headerActions([
CreateChildCollection::make('createChildCollection'),
CreateChildCollection::make('createChildCollection')->after(
fn () => ChildCollectionCreated::dispatch($this->getRecord())
),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Lunar\Admin\Events\CollectionProductAttached;
use Lunar\Admin\Events\CollectionProductDetached;
use Lunar\Admin\Filament\Resources\CollectionResource;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
Expand Down Expand Up @@ -67,7 +69,9 @@ public function table(Table $table): Table
->formatStateUsing(fn (Model $record): string => $record->translateAttribute('name'))
->label(__('lunarpanel::product.table.name.label')),
])->actions([
Tables\Actions\DetachAction::make(),
Tables\Actions\DetachAction::make()->after(
fn () => CollectionProductDetached::dispatch($this->getOwnerRecord())
),
Tables\Actions\EditAction::make()->url(
fn (Model $record) => ProductResource::getUrl('edit', [
'record' => $record,
Expand Down Expand Up @@ -96,6 +100,10 @@ public function table(Table $table): Table
$relationship->attach($product, [
'position' => $relationship->count() + 1,
]);

CollectionProductAttached::dispatch($this->getOwnerRecord());

$product->searchable();
}),
])->reorderable('position');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Lunar\Admin\Events\CustomerAddressEdited;
use Lunar\Models\Address;
use Lunar\Models\State;

Expand Down Expand Up @@ -67,6 +68,9 @@ public function table(Table $table): Table
),
])->actions([
Tables\Actions\EditAction::make('editAddress')
->after(
fn (Model $record) => CustomerAddressEdited::dispatch($record)
)
->fillForm(fn (Address $record): array => [
'title' => $record->title,
'first_name' => $record->first_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use Lunar\Admin\Events\CustomerUserEdited;

class UserRelationManager extends RelationManager
{
Expand All @@ -27,6 +29,9 @@ public function table(Table $table): Table
->label(__('lunarpanel::user.table.email.label')),
])->actions([
Tables\Actions\EditAction::make('edit')
->after(
fn (Model $record) => CustomerUserEdited::dispatch($record)
)
->form([
Group::make([
TextInput::make('email')
Expand Down
Loading

0 comments on commit 3e85ee9

Please sign in to comment.