Skip to content

Commit 9923726

Browse files
committed
Add new photo only feed
1 parent c681e37 commit 9923726

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

app/Livewire/Pages/Feed.php

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Livewire\Pages;
44

55
use App\Models\Post;
6+
use App\Models\PostStatus;
67
use Livewire\Component;
78

89
class Feed extends Component
@@ -13,11 +14,14 @@ class Feed extends Component
1314

1415
public bool $isLastPage = false;
1516

17+
public int $newPhotoCount = 0;
18+
1619
public function mount()
1720
{
1821
$results = Post::orderByDesc('date_taken')->paginate(10, ['*'], 'page', $this->page);
1922
$this->isLastPage = $results->onLastPage();
2023
$this->posts = $results->items();
24+
$this->newPhotoCount = PostStatus::all()->count();
2125
}
2226

2327
public function loadMore()

app/Livewire/Pages/NewFeed.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Livewire\Pages;
4+
5+
use App\Models\Post;
6+
use Livewire\Component;
7+
8+
class NewFeed extends Component
9+
{
10+
/** @var Post[] */
11+
public array $posts;
12+
13+
public int $page = 1;
14+
15+
public bool $isLastPage = false;
16+
17+
public function mount()
18+
{
19+
$results = Post::has('postStatus')->orderByDesc('date_taken')->paginate(10, ['*'], 'page', $this->page);
20+
$this->isLastPage = $results->onLastPage();
21+
$this->posts = $results->items();
22+
}
23+
24+
public function loadMore()
25+
{
26+
$this->page++;
27+
$results = Post::has('postStatus')->orderByDesc('date_taken')->paginate(10, ['*'], 'page', $this->page);
28+
$this->isLastPage = $results->onLastPage();
29+
$this->posts = array_merge($this->posts, $results->items());
30+
}
31+
32+
public function render()
33+
{
34+
return view('livewire.pages.new-feed');
35+
}
36+
}

resources/views/livewire/pages/feed.blade.php

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
@if(count($posts) === 0)
33
🤷 Oops... No photos found
44
@endif
5+
@if($newPhotoCount > 0)
6+
<a href="{{ route('new-feed') }}" role="alert" class="alert w-full sm:max-w-md">
7+
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
8+
class="stroke-info shrink-0 w-6 h-6">
9+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
10+
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
11+
</svg>
12+
<span>{{ $newPhotoCount }} new {{ $newPhotoCount === 1 ? 'photo' : 'photos' }}. Click to view.</span>
13+
</a>
14+
@endif
515
@foreach($posts as $post)
616
<x-photo-card wire:key="post-{{ $post->id }}" :post="$post"/>
717
@endforeach
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<x-main-content-layout>
2+
<h1 class="text-2xl font-bold mb-4">New Photos</h1>
3+
@if(count($posts) === 0)
4+
🤷 Oops... No new photos found
5+
@endif
6+
@foreach($posts as $post)
7+
<x-photo-card wire:key="post-{{ $post->id }}" :post="$post"/>
8+
@endforeach
9+
@if(!$isLastPage)
10+
<div x-intersect.margin.500px="$wire.loadMore()">Scroll to load more 👇</div>
11+
@endif
12+
<div wire:loading wire:target="loadMore">
13+
<span class="loading loading-dots loading-sm"></span>
14+
</div>
15+
</x-main-content-layout>
16+

routes/web.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Route::get('/about', \App\Livewire\Pages\About::class)->name('about');
1919
Route::get('whats-new', \App\Livewire\Pages\WhatsNew::class)->name('whats-new');
2020
Route::get('/feed', \App\Livewire\Pages\Feed::class)->name('feed')->middleware('auth:web');
21+
Route::get('/feed/new', \App\Livewire\Pages\NewFeed::class)->name('new-feed')->middleware('auth:web');
2122
Route::get('photos/{post}', \App\Livewire\Pages\SinglePhoto::class)->name('single-photo')->middleware('auth:web');
2223
Route::get('tags/{slug}', \App\Livewire\Pages\TagFeed::class)->name('tag')->middleware('auth:web');
2324
Route::get('/signup', \App\Livewire\Pages\NotificationsSignup::class)->name('signup')->middleware('auth:web');

0 commit comments

Comments
 (0)