Skip to content

Commit 5af8be4

Browse files
committed
wip
1 parent 5a354ef commit 5af8be4

File tree

20 files changed

+4614
-3252
lines changed

20 files changed

+4614
-3252
lines changed

app/Events/AnnouncePost.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Events;
6+
7+
use Illuminate\Broadcasting\Channel;
8+
use Illuminate\Broadcasting\InteractsWithSockets;
9+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
10+
use Illuminate\Foundation\Events\Dispatchable;
11+
use Illuminate\Queue\SerializesModels;
12+
13+
class AnnouncePost implements ShouldBroadcast
14+
{
15+
use Dispatchable;
16+
use InteractsWithSockets;
17+
use SerializesModels;
18+
19+
/**
20+
* Create a new event instance.
21+
*/
22+
public function __construct(
23+
public int $post
24+
) {
25+
//
26+
}
27+
28+
/**
29+
* Get the channels the event should broadcast on.
30+
*
31+
* @return array<int, \Illuminate\Broadcasting\Channel>
32+
*/
33+
public function broadcastOn(): array
34+
{
35+
return [
36+
new Channel('newsfeed'),
37+
];
38+
}
39+
}

app/Livewire/NewsFeed.php

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
use Filament\Forms\Contracts\HasForms;
1414
use Filament\Forms\Form;
1515
use Illuminate\Contracts\Database\Eloquent\Builder;
16-
use Illuminate\Support\Collection;
17-
use Livewire\Attributes\Computed;
16+
use Illuminate\Pagination\LengthAwarePaginator;
1817
use Livewire\Component;
1918
use Livewire\WithPagination;
2019

@@ -23,18 +22,14 @@ class NewsFeed extends Component implements HasForms
2322
use InteractsWithForms;
2423
use WithPagination;
2524

26-
private int $perPage = 10;
27-
28-
public Collection $posts;
29-
3025
public ?array $filters = [];
3126

27+
protected $listeners = [
28+
'reload' => 'reload',
29+
];
30+
3231
public function mount(): void
3332
{
34-
$this->posts = collect();
35-
36-
$this->loadPosts();
37-
3833
$this->form->fill();
3934
}
4035

@@ -73,45 +68,26 @@ public function form(Form $form): Form
7368

7469
public function render()
7570
{
76-
return view('livewire.news-feed');
77-
}
78-
79-
private function loadPosts(bool $more = false): void
80-
{
81-
$this->query()
82-
->limit($this->perPage)
83-
->when($more, fn (Builder $query) => $query->offset($this->posts->count()))
84-
->get()
85-
->each(fn (Post $post) => $this->posts->push(
86-
$post->toNewsFeedItem()
87-
));
88-
}
89-
90-
public function loadMore(): void
91-
{
92-
$this->loadPosts(true);
93-
}
94-
95-
#[Computed]
96-
public function total(): int
97-
{
98-
return $this->query()->count();
71+
return view('livewire.news-feed', [
72+
'posts' => $this->getPosts(),
73+
]);
9974
}
10075

101-
#[Computed]
102-
public function hasMore(): bool
76+
public function reload(): void
10377
{
104-
return $this->posts->count() < $this->total;
78+
$this->reset('filters');
79+
$this->resetPage();
10580
}
10681

107-
private function query(): Builder
82+
protected function getPosts(): LengthAwarePaginator
10883
{
10984
return Post::query()
11085
->with('author.media', 'electionDay', 'media')
11186
->when(data_get($this->filters, 'country'), fn (Builder $query, array $countries) => $query->whereIn('country', $countries))
11287
->when(data_get($this->filters, 'author'), fn (Builder $query, array $authors) => $query->whereIn('author_id', $authors))
11388
->when(data_get($this->filters, 'day'), fn (Builder $query, array $days) => $query->whereIn('election_day_id', $days))
11489
->onlyPublished()
115-
->orderByDesc('published_at');
90+
->orderByDesc('published_at')
91+
->paginate();
11692
}
11793
}

app/Livewire/NewsFeedUpdater.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Livewire;
6+
7+
use Livewire\Component;
8+
9+
class NewsFeedUpdater extends Component
10+
{
11+
public bool $banner = false;
12+
13+
protected $listeners = [
14+
'echo:newsfeed,AnnouncePost' => 'showBanner',
15+
'reload' => '$refresh',
16+
];
17+
18+
public function showBanner(): void
19+
{
20+
$this->banner = true;
21+
}
22+
23+
public function render()
24+
{
25+
return view('livewire.news-feed-updater');
26+
}
27+
28+
public function reload(): void
29+
{
30+
$this->dispatch('reload');
31+
$this->banner = false;
32+
}
33+
}

app/Models/Post.php

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
namespace App\Models;
66

77
use App\Concerns\Publishable;
8-
use App\DataTransferObjects\NewsFeedItem;
9-
use App\DataTransferObjects\NewsFeedItemAuthor;
10-
use App\DataTransferObjects\NewsFeedItemEmbed;
11-
use App\DataTransferObjects\NewsFeedItemMedia;
128
use App\Enums\Country;
9+
use App\Events\AnnouncePost;
1310
use Illuminate\Database\Eloquent\Factories\HasFactory;
1411
use Illuminate\Database\Eloquent\Model;
1512
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -62,27 +59,22 @@ public function electionDay(): BelongsTo
6259
return $this->belongsTo(ElectionDay::class);
6360
}
6461

65-
public function toNewsFeedItem(): NewsFeedItem
62+
protected static function booted(): void
6663
{
67-
return new NewsFeedItem(
68-
id: $this->id,
69-
title: $this->title,
70-
content: $this->content,
71-
publishedAt: $this->published_at,
72-
author: new NewsFeedItemAuthor(
73-
name: $this->author->name,
74-
avatar: $this->author->getFilamentAvatarUrl(),
75-
),
76-
embeds: $this->embeds
77-
->map(fn (array $embed) => new NewsFeedItemEmbed(html: $embed['html']))
78-
->all(),
79-
media: $this->getMedia()
80-
->map(fn (Media $media) => new NewsFeedItemMedia(
81-
name: $media->name,
82-
url: $media->getUrl(),
83-
thumb: $media->getUrl('thumb'),
84-
))
85-
->all(),
86-
);
64+
static::created(function (Post $post) {
65+
if ($post->published_at?->isPast()) {
66+
AnnouncePost::dispatch($post->id);
67+
}
68+
});
69+
70+
static::updated(function (Post $post) {
71+
if ($post->isDirty('published_at') && $post->published_at?->isPast()) {
72+
AnnouncePost::dispatch($post->id);
73+
}
74+
});
75+
76+
static::deleted(function (Post $post) {
77+
AnnouncePost::dispatch($post->id);
78+
});
8779
}
8880
}

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"filament/spatie-laravel-media-library-plugin": "^3.2",
1717
"jeffgreco13/filament-breezy": "^2.4",
1818
"laravel/framework": "^11.10",
19+
"laravel/pulse": "^1.2",
20+
"laravel/reverb": "@beta",
1921
"laravel/tinker": "^2.9",
2022
"livewire/livewire": "^3.5",
2123
"maatwebsite/excel": "^3.1",

0 commit comments

Comments
 (0)