Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Jun 4, 2024
1 parent 5a354ef commit 5af8be4
Show file tree
Hide file tree
Showing 20 changed files with 4,614 additions and 3,252 deletions.
39 changes: 39 additions & 0 deletions app/Events/AnnouncePost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class AnnouncePost implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public int $post
) {
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new Channel('newsfeed'),
];
}
}
52 changes: 14 additions & 38 deletions app/Livewire/NewsFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
use Livewire\Attributes\Computed;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;
use Livewire\WithPagination;

Expand All @@ -23,18 +22,14 @@ class NewsFeed extends Component implements HasForms
use InteractsWithForms;
use WithPagination;

private int $perPage = 10;

public Collection $posts;

public ?array $filters = [];

protected $listeners = [
'reload' => 'reload',
];

public function mount(): void
{
$this->posts = collect();

$this->loadPosts();

$this->form->fill();
}

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

public function render()
{
return view('livewire.news-feed');
}

private function loadPosts(bool $more = false): void
{
$this->query()
->limit($this->perPage)
->when($more, fn (Builder $query) => $query->offset($this->posts->count()))
->get()
->each(fn (Post $post) => $this->posts->push(
$post->toNewsFeedItem()
));
}

public function loadMore(): void
{
$this->loadPosts(true);
}

#[Computed]
public function total(): int
{
return $this->query()->count();
return view('livewire.news-feed', [
'posts' => $this->getPosts(),
]);
}

#[Computed]
public function hasMore(): bool
public function reload(): void
{
return $this->posts->count() < $this->total;
$this->reset('filters');
$this->resetPage();
}

private function query(): Builder
protected function getPosts(): LengthAwarePaginator
{
return Post::query()
->with('author.media', 'electionDay', 'media')
->when(data_get($this->filters, 'country'), fn (Builder $query, array $countries) => $query->whereIn('country', $countries))
->when(data_get($this->filters, 'author'), fn (Builder $query, array $authors) => $query->whereIn('author_id', $authors))
->when(data_get($this->filters, 'day'), fn (Builder $query, array $days) => $query->whereIn('election_day_id', $days))
->onlyPublished()
->orderByDesc('published_at');
->orderByDesc('published_at')
->paginate();
}
}
33 changes: 33 additions & 0 deletions app/Livewire/NewsFeedUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Livewire;

use Livewire\Component;

class NewsFeedUpdater extends Component
{
public bool $banner = false;

protected $listeners = [
'echo:newsfeed,AnnouncePost' => 'showBanner',
'reload' => '$refresh',
];

public function showBanner(): void
{
$this->banner = true;
}

public function render()
{
return view('livewire.news-feed-updater');
}

public function reload(): void
{
$this->dispatch('reload');
$this->banner = false;
}
}
42 changes: 17 additions & 25 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
namespace App\Models;

use App\Concerns\Publishable;
use App\DataTransferObjects\NewsFeedItem;
use App\DataTransferObjects\NewsFeedItemAuthor;
use App\DataTransferObjects\NewsFeedItemEmbed;
use App\DataTransferObjects\NewsFeedItemMedia;
use App\Enums\Country;
use App\Events\AnnouncePost;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -62,27 +59,22 @@ public function electionDay(): BelongsTo
return $this->belongsTo(ElectionDay::class);
}

public function toNewsFeedItem(): NewsFeedItem
protected static function booted(): void
{
return new NewsFeedItem(
id: $this->id,
title: $this->title,
content: $this->content,
publishedAt: $this->published_at,
author: new NewsFeedItemAuthor(
name: $this->author->name,
avatar: $this->author->getFilamentAvatarUrl(),
),
embeds: $this->embeds
->map(fn (array $embed) => new NewsFeedItemEmbed(html: $embed['html']))
->all(),
media: $this->getMedia()
->map(fn (Media $media) => new NewsFeedItemMedia(
name: $media->name,
url: $media->getUrl(),
thumb: $media->getUrl('thumb'),
))
->all(),
);
static::created(function (Post $post) {
if ($post->published_at?->isPast()) {
AnnouncePost::dispatch($post->id);
}
});

static::updated(function (Post $post) {
if ($post->isDirty('published_at') && $post->published_at?->isPast()) {
AnnouncePost::dispatch($post->id);
}
});

static::deleted(function (Post $post) {
AnnouncePost::dispatch($post->id);
});
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"filament/spatie-laravel-media-library-plugin": "^3.2",
"jeffgreco13/filament-breezy": "^2.4",
"laravel/framework": "^11.10",
"laravel/pulse": "^1.2",
"laravel/reverb": "@beta",
"laravel/tinker": "^2.9",
"livewire/livewire": "^3.5",
"maatwebsite/excel": "^3.1",
Expand Down
Loading

0 comments on commit 5af8be4

Please sign in to comment.