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 0858cef commit 5a354ef
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 28 deletions.
41 changes: 41 additions & 0 deletions app/DataTransferObjects/NewsFeedItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\DataTransferObjects;

use Carbon\Carbon;
use Livewire\Wireable;

readonly class NewsFeedItem implements Wireable
{
public function __construct(
public int $id,
public string $title,
public string $content,
public Carbon $publishedAt,
public NewsFeedItemAuthor $author,
public array $embeds = [],
public array $media = [],
) {
//
}

public function toLivewire(): array
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'publishedAt' => $this->publishedAt,
'author' => $this->author,
'embeds' => $this->embeds,
'media' => $this->media,
];
}

public static function fromLivewire($value): static
{
return new static(...$value);
}
}
30 changes: 30 additions & 0 deletions app/DataTransferObjects/NewsFeedItemAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\DataTransferObjects;

use Livewire\Wireable;

readonly class NewsFeedItemAuthor implements Wireable
{
public function __construct(
public string $name,
public string $avatar,
) {
//
}

public function toLivewire(): array
{
return [
'name' => $this->name,
'avatar' => $this->avatar,
];
}

public static function fromLivewire($value): static
{
return new static(...$value);
}
}
28 changes: 28 additions & 0 deletions app/DataTransferObjects/NewsFeedItemEmbed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\DataTransferObjects;

use Livewire\Wireable;

readonly class NewsFeedItemEmbed implements Wireable
{
public function __construct(
public string $html,
) {
//
}

public function toLivewire(): array
{
return [
'html' => $this->html,
];
}

public static function fromLivewire($value): static
{
return new static(...$value);
}
}
32 changes: 32 additions & 0 deletions app/DataTransferObjects/NewsFeedItemMedia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\DataTransferObjects;

use Livewire\Wireable;

readonly class NewsFeedItemMedia implements Wireable
{
public function __construct(
public string $name,
public string $url,
public string $thumb,
) {
//
}

public function toLivewire(): array
{
return [
'name' => $this->name,
'url' => $this->url,
'thumbs' => $this->thumb,
];
}

public static function fromLivewire($value): static
{
return new static(...$value);
}
}
2 changes: 1 addition & 1 deletion app/Filament/Resources/ElectionDayResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ElectionDayResource extends Resource

public static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
return __('admin.navigation.newsfeed');
}

public static function form(Form $form): Form
Expand Down
9 changes: 8 additions & 1 deletion app/Filament/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PostResource extends Resource

public static function getNavigationGroup(): ?string
{
return __('navigation.group.manage');
return __('admin.navigation.newsfeed');
}

public static function form(Form $form): Form
Expand All @@ -55,6 +55,13 @@ public static function form(Form $form): Form
->required()
->preload(),

Select::make('election_day_id')
->relationship('electionDay', 'date')
->getOptionLabelFromRecordUsing(fn (ElectionDay $record) => $record->date->toDateString())
// ->formatStateUsing(fn (Post $record) => dd($record) && $record->date?->toDateString())
->required()
->preload(),

DateTimePicker::make('published_at')
->nullable(),

Expand Down
46 changes: 38 additions & 8 deletions app/Livewire/NewsFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Livewire\WithPagination;

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

private int $perPage = 10;

public Collection $posts;

public ?array $filters = [];

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

$this->loadPosts();

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

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

public function render()
{
return view('livewire.news-feed', [
'posts' => $this->getPosts(),
]);
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();
}

#[Computed]
public function hasMore(): bool
{
return $this->posts->count() < $this->total;
}

protected function getPosts(): LengthAwarePaginator
private function query(): Builder
{
return Post::query()
->with('author.media', 'electionDay')
->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')
->paginate();
->orderByDesc('published_at');
}
}
12 changes: 12 additions & 0 deletions app/Models/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Spatie\MediaLibrary\MediaCollections\Models\Media as Model;

class Media extends Model
{
//
}
31 changes: 31 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
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 Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -23,6 +27,9 @@ class Post extends Model implements HasMedia
'title',
'content',
'embeds',
'author_id',
'country',
'election_day_id',
];

protected function casts(): array
Expand Down Expand Up @@ -54,4 +61,28 @@ public function electionDay(): BelongsTo
{
return $this->belongsTo(ElectionDay::class);
}

public function toNewsFeedItem(): NewsFeedItem
{
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(),
);
}
}
20 changes: 19 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

namespace App\Providers;

use App\Models\ElectionDay;
use App\Models\Media;
use App\Models\Post;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;

Expand All @@ -27,7 +33,9 @@ public function register(): void
*/
public function boot(): void
{
//
$this->enforceMorphMap();

Gate::define('viewPulse', fn (User $user) => $user->isAdmin());
}

protected function registerCountries(): void
Expand Down Expand Up @@ -201,4 +209,14 @@ protected function registerLanguages(): void
],
]);
}

protected function enforceMorphMap(): void
{
Relation::enforceMorphMap([
'electionDay' => ElectionDay::class,
'media' => Media::class,
'post' => Post::class,
'user' => User::class,
]);
}
}
2 changes: 1 addition & 1 deletion config/media-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/*
* The fully qualified class name of the media model.
*/
'media_model' => Spatie\MediaLibrary\MediaCollections\Models\Media::class,
'media_model' => App\Models\Media::class,

/*
* When enabled, media collections will be serialised using the default
Expand Down
12 changes: 12 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

return [

'navigation' => [
'newsfeed' => 'Newsfeed',
'counters' => 'Counters',
],

];
Loading

0 comments on commit 5a354ef

Please sign in to comment.