Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [LAR-28] add articles livewire page #171

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Actions/Article/CreateArticleAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Actions\Article;

use App\Data\Article\CreateArticleData;
use App\Data\CreateArticleData;
use App\Gamify\Points\ArticleCreated;
use App\Models\Article;
use App\Notifications\PostArticleToTelegram;
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Discussion/CreateDiscussionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Actions\Discussion;

use App\Data\Discussion\CreateDiscussionData;
use App\Data\CreateDiscussionData;
use App\Gamify\Points\DiscussionCreated;
use App\Models\Discussion;
use App\Notifications\PostDiscussionToTelegram;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Data\Article;
namespace App\Data;

use Carbon\Carbon;
use Illuminate\Http\UploadedFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Data\Discussion;
namespace App\Data;

use Spatie\LaravelData\Data;

Expand Down
40 changes: 0 additions & 40 deletions app/Http/Controllers/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
namespace App\Http\Controllers;

use App\Models\Article;
use App\Models\User;
use App\Policies\ArticlePolicy;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

final class ArticlesController extends Controller
{
Expand All @@ -18,43 +15,6 @@ public function __construct()
$this->middleware(['auth', 'verified'], ['except' => ['index', 'show']]);
}

public function index(): View
{
return view('articles.index');
}

public function show(Article $article): View
{
/** @var User $user */
$user = Auth::user();

views($article)->record();

/** @var Article $article */
$article = Cache::remember('post-'.$article->id, now()->addHour(), fn () => $article);

abort_unless(
$article->isPublished() || ($user && $article->isAuthoredBy($user)) || ($user && $user->hasAnyRole(['admin', 'moderator'])), // @phpstan-ignore-line
404
);

$image = $article->getFirstMediaUrl('media');
// @phpstan-ignore-next-line
seo()
->title($article->title)
->description($article->excerpt(100))
->image($image)
->twitterTitle($article->title)
->twitterDescription($article->excerpt(100))
->twitterImage($image)
->twitterSite('laravelcm')
->withUrl();

return view('articles.show', [
'article' => $article->loadCount('views'),
]);
}

public function create(): View
{
return view('articles.new');
Expand Down
78 changes: 0 additions & 78 deletions app/Livewire/Articles/Browse.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Livewire/Articles/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Livewire\Articles;

use App\Actions\Article\CreateArticleAction;
use App\Data\Article\CreateArticleData;
use App\Data\CreateArticleData;
use App\Models\Tag;
use App\Models\User;
use App\Traits\WithArticleAttributes;
Expand Down
2 changes: 1 addition & 1 deletion app/Livewire/Discussions/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Livewire\Discussions;

use App\Actions\Discussion\CreateDiscussionAction;
use App\Data\Discussion\CreateDiscussionData;
use App\Data\CreateDiscussionData;
use App\Models\Tag;
use App\Traits\WithTagsAssociation;
use Illuminate\Contracts\View\View;
Expand Down
32 changes: 32 additions & 0 deletions app/Livewire/Pages/Articles/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Pages\Articles;

use App\Models\Article;
use App\Models\Tag;
use App\Traits\WithInfiniteScroll;
use Illuminate\Contracts\View\View;
use Livewire\Component;

final class Index extends Component
{
use WithInfiniteScroll;

public function render(): View
{
return view('livewire.pages.articles.index', [
'articles' => Article::with(['tags', 'user', 'user.transactions'])
->withCount(['views', 'reactions'])
->scopes(['published', 'notPinned'])
->orderByDesc('sponsored_at')
->orderByDesc('published_at')
->paginate($this->perPage),
'tags' => Tag::query()->whereHas('articles', function ($query): void {
$query->published();
})->orderBy('name')->get(),
])
->title(__('pages/article.title'));
}
}
52 changes: 52 additions & 0 deletions app/Livewire/Pages/Articles/SinglePost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Pages\Articles;

use App\Models\Article;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

final class SinglePost extends Component
{
public Article $article;

public function mount(Article $article): void
{
/** @var User $user */
$user = Auth::user();
views($article)->cooldown(now()->addHours(2))->record();

$article = $article->load(['media', 'user'])->loadCount('views');

abort_unless(
$article->isPublished() || ($user && $article->isAuthoredBy($user)) || ($user && $user->hasAnyRole(['admin', 'moderator'])), // @phpstan-ignore-line
404
);

$image = empty($article->getFirstMediaUrl('media'))
? $article->getFirstMediaUrl('media')
: asset('/images/socialcard.png');

// @phpstan-ignore-next-line
seo()
->title($article->title)
->description($article->excerpt(150))
->image($image)
->twitterTitle($article->title)
->twitterDescription($article->excerpt(150))
->twitterImage($image)
->twitterSite('laravelcm')
->withUrl();

$this->article = $article;
}

public function render(): View
{
return view('livewire.pages.articles.single-post')->title($this->article->title);
}
}
33 changes: 33 additions & 0 deletions app/Livewire/Pages/Articles/SingleTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Pages\Articles;

use App\Models\Article;
use App\Models\Tag;
use App\Traits\WithInfiniteScroll;
use Illuminate\Contracts\View\View;
use Livewire\Component;

final class SingleTag extends Component
{
use WithInfiniteScroll;

public Tag $tag;

public function render(): View
{
return view('livewire.pages.articles.tag', [
'articles' => Article::with(['tags', 'user', 'user.transactions'])
->whereHas('tags', function ($query): void {
$query->where('id', $this->tag->id);
})
->withCount(['views', 'reactions'])
->scopes(['published', 'notPinned'])
->orderByDesc('sponsored_at')
->orderByDesc('published_at')
->paginate($this->perPage),
])->title($this->tag->name);
}
}
9 changes: 0 additions & 9 deletions app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ final class Article extends Model implements HasMedia, ReactableInterface, Viewa
'is_pinned' => 'boolean',
];

protected $with = [
'media',
];

protected bool $removeViewsOnDelete = true;

public function getRouteKeyName(): string
Expand Down Expand Up @@ -115,11 +111,6 @@ public function registerMediaCollections(): void
->acceptsMimeTypes(['image/jpg', 'image/jpeg', 'image/png']);
}

public function showToc(): bool
{
return $this->show_toc;
}

public function submittedAt(): ?Carbon
{
return $this->submitted_at;
Expand Down
19 changes: 7 additions & 12 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
namespace App\Models;

use App\Traits\HasSlug;
use App\Traits\ModelHelpers;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

/**
* @property-read int $id
* @property string $name
* @property string $slug
* @property string | null $description
* @property array $concerns
*/
final class Tag extends Model
{
use HasFactory;
use HasSlug;
use ModelHelpers;

public $timestamps = false;

Expand All @@ -29,16 +34,6 @@ final class Tag extends Model
'concerns' => 'array',
];

public function id(): int
{
return $this->id;
}

public function name(): string
{
return $this->name;
}

public function articles(): MorphToMany
{
return $this->morphedByMany(Article::class, 'taggable');
Expand Down
20 changes: 0 additions & 20 deletions app/Traits/ModelHelpers.php

This file was deleted.

Loading
Loading