Skip to content

Commit

Permalink
Merge pull request #13 from tv2regionerne/feature/websockets
Browse files Browse the repository at this point in the history
Add websocket updates
  • Loading branch information
Sylvester Damgaard authored Apr 4, 2024
2 parents 46b33af + 0a27428 commit 1f56859
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
11 changes: 11 additions & 0 deletions public/build/assets/curated-collections-addon-b5e8b3b2.js

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions public/build/assets/curated-collections-addon-f8811ef0.js

This file was deleted.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"css": [
"assets/curated-collections-addon-4475c9e7.css"
],
"file": "assets/curated-collections-addon-f8811ef0.js",
"file": "assets/curated-collections-addon-b5e8b3b2.js",
"isEntry": true,
"src": "resources/js/curated-collections-addon.js"
}
Expand Down
17 changes: 17 additions & 0 deletions resources/js/components/curated-collections/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</div>

<view-tab
ref="tab"
:handle="handle"
:breadcrumbUrl="breadcrumbUrl"
:collections="collections"
Expand Down Expand Up @@ -67,5 +68,21 @@ export default {
}
},
mounted() {
Statamic.$echo.booted(() => {
this.$echo
.private(`curated-collections-private.${this.handle.toLowerCase()}`)
.listen('.CuratedCollections.CuratedCollectionUpdated', event => this.curatedCollectionPushed(event));
});
},
methods: {
curatedCollectionPushed() {
this.$refs.tab.request();
},
},
}
</script>
24 changes: 18 additions & 6 deletions src/Events/CuratedCollectionUpdatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
namespace Tv2regionerne\StatamicCuratedCollection\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
use Statamic\Events\Event;

class CuratedCollectionUpdatedEvent
class CuratedCollectionUpdatedEvent extends Event implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
use InteractsWithSockets, SerializesModels;

public string $tag;
public string $handle;

/**
* Create a new event instance.
*/
public function __construct(string $tag)
public function __construct(string $handle)
{
$this->tag = strtolower($tag);
$this->handle = strtolower($handle);
}

public function broadcastOn()
{
return new PrivateChannel('curated-collections-private.'.$this->handle);
}

public function broadcastAs()
{
return 'CuratedCollections.CuratedCollectionUpdated';
}
}
18 changes: 14 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tv2regionerne\StatamicCuratedCollection;

use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\View;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function boot()
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

$this->bootApi();
$this->bootBroadcasting();

View::composer(['statamic-curated-collections::curated-collections.blueprints.edit'], FieldComposer::class);
}
Expand All @@ -74,7 +76,6 @@ public function bootAddon()
->bootCache();

Nav::extend(function ($nav) {

$children = [];
rescue(function () use (&$nav, &$children) {
foreach (CuratedCollection::query()->orderBy('title')->get() as $list) {
Expand Down Expand Up @@ -120,7 +121,6 @@ protected function bootPermissions(): self
});
});
});

});

return $this;
Expand All @@ -146,7 +146,6 @@ private function bootApi(): self
Route::get('/', [CuratedCollectionEntriesController::class, 'index']);
Route::post('reorder', [CuratedCollectionEntriesController::class, 'reorder']);
});

});
});
});
Expand All @@ -163,10 +162,21 @@ private function bootCache(): self
});

Event::listen(function (Events\CuratedCollectionUpdatedEvent $event) {
CacheStore::invalidateContent(['curated_collections:'.$event->tag]);
CacheStore::invalidateContent(['curated_collections:'.$event->handle]);
});
}

return $this;
}

private function bootBroadcasting()
{
Broadcast::channel('curated-collections-private.{handle}', function ($user, string $id) {
$user = \Statamic\Facades\User::fromUser($user);

return $user->isSuper() || $user->can('access cp');
}, ['guards' => ['web']]);

return $this;
}
}

0 comments on commit 1f56859

Please sign in to comment.