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

infinity scroll for posts #2

Open
wants to merge 1 commit into
base: master
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
6 changes: 3 additions & 3 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public function list(Request $request)
{
$posts = Post::with('user')
->orderBy('id', 'desc')
->simplePaginate(5);
->simplePaginate(2);

$test = view('post.list', [
'posts' => $posts,
])->fragmentsIf(!$request->isMethodSafe(), [
])->fragmentsIf($request->has('page'), [
"posts",
"more",
// "more",
]);

return $test;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 101 additions & 12 deletions resources/views/post/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
style="background-image: url('/img/fire.svg'); background-repeat: no-repeat; background-position: right bottom">
<ul class="nav flex-column">
<li class="nav-item mb-3"><a href="{{ route('post') }}" class="nav-link p-0 link-dark">Соблюдение
принципов
SOLID при
работе с
фреймворком
Laravel</a>
принципов
SOLID при
работе с
фреймворком
Laravel</a>
</li>
<li class="nav-item mb-3"><a href="{{ route('post') }}" class="nav-link p-0 link-dark">Как начать
работать с
очередями в
Laravel</a>
работать с
очередями в
Laravel</a>
</li>
<li class="nav-item mb-3"><a href="{{ route('post') }}" class="nav-link p-0 link-dark">Laravel и
ULID</a></li>
ULID</a></li>
<li class="nav-item mb-3"><a href="{{ route('post') }}" class="nav-link p-0 link-dark">Eloquent и
Blade: советы
по повышению
производительности</a>
Blade: советы
по повышению
производительности</a>
</li>
<li class="nav-item mb-3"><a href="{{ route('post') }}" class="nav-link p-0 link-dark">About</a>
</li>
Expand All @@ -37,6 +37,7 @@

<div class="container my-5">
<div class="row">
{{--
<x-stream target="posts" action="append" class="col-xl-8 col-md-12 mx-auto">
@foreach($posts as $post)
<div class="bg-body-tertiary mb-4 px-5 py-4">
Expand Down Expand Up @@ -122,6 +123,94 @@ class="text-body-secondary ms-auto user-select-none small">{{ $post->created_at-
</form>
@endif
</x-stream>
--}}
<div class="col-xl-8 col-md-12 mx-auto">
@fragment('posts')
<turbo-frame id="post-page-{{$posts->currentPage()}}" target="_top">
@foreach($posts as $post)
<div class="bg-body-tertiary mb-4 px-5 py-4">

<div class="d-flex align-items-center justify-content-between mb-3">
<div class="d-flex align-items-center">
<div class="avatar avatar-sm me-3">
<a href="#!">
<img class="avatar-img rounded-circle"
src="{{ $post->user->avatar }}"
alt="{{ $post->user->title }}">
</a>
</div>

<div class="small">
<h6 class="mb-0 me-4">
<a href="#!"
class="text-body-secondary text-decoration-none">{{ $post->user->name }}</a>
</h6>
<p class="mb-0 small">Разработчик laravel.su</p>
</div>
</div>
<div class="dropdown">
<a href="#" class="text-secondary btn btn-link py-1 px-2"
data-bs-toggle="dropdown" aria-expanded="false">
<x-icon path="bs.three-dots"/>
</a>
</div>
</div>

<div class="position-relative">
<a href="{{ route('post') }}"
class="position-absolute start-0 end-0 top-0 bottom-0"></a>


<h4 class="mb-3">{{$post->title}}</h4>

<p>{{$post->preview()}} </p>

<!-- Card img -->
<img class="card-img"
src="https://uploads.tickettailor.com/c_crop,dpr_1.0,h_635,q_100,w_2000,x_0,y_75/c_scale,g_center,h_204,q_85,w_640/v1/production/userfiles/ls82tsupch7otycjjngi.jpg?_a=BAAARODQ"
alt="Post">
</div>


<div class="d-flex align-items-center mt-4">

<a class="d-flex align-items-center text-body-secondary text-decoration-none me-4"
href="#!">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor"
class="bi bi-heart"
viewBox="0 0 16 16">
<path
d="m8 2.748-.717-.737C5.6.281 2.514.878 1.4 3.053c-.523 1.023-.641 2.5.314 4.385.92 1.815 2.834 3.989 6.286 6.357 3.452-2.368 5.365-4.542 6.286-6.357.955-1.886.838-3.362.314-4.385C13.486.878 10.4.28 8.717 2.01L8 2.748zM8 15C-7.333 4.868 3.279-3.04 7.824 1.143c.06.055.119.112.176.171a3.12 3.12 0 0 1 .176-.17C12.72-3.042 23.333 4.867 8 15z"/>
</svg>
<span class="ms-2">56</span>
</a>

<a class="d-flex align-items-center text-body-secondary text-decoration-none" href="#!">
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" fill="currentColor"
class="bi bi-chat"
viewBox="0 0 16 16">
<path
d="M2.678 11.894a1 1 0 0 1 .287.801 10.97 10.97 0 0 1-.398 2c1.395-.323 2.247-.697 2.634-.893a1 1 0 0 1 .71-.074A8.06 8.06 0 0 0 8 14c3.996 0 7-2.807 7-6 0-3.192-3.004-6-7-6S1 4.808 1 8c0 1.468.617 2.83 1.678 3.894zm-.493 3.905a21.682 21.682 0 0 1-.713.129c-.2.032-.352-.176-.273-.362a9.68 9.68 0 0 0 .244-.637l.003-.01c.248-.72.45-1.548.524-2.319C.743 11.37 0 9.76 0 8c0-3.866 3.582-7 8-7s8 3.134 8 7-3.582 7-8 7a9.06 9.06 0 0 1-2.347-.306c-.52.263-1.639.742-3.468 1.105z"/>
</svg>
<span class="ms-2">131</span>
</a>

<span
class="text-body-secondary ms-auto user-select-none small">{{ $post->created_at->diffForHumans() }}</span>
</div>
</div>
@endforeach
@if($posts->hasMorePages())
<turbo-frame id="post-page-{{$posts->currentPage() +1}}"
src="{{route('feed.more',['page'=>$posts->currentPage() +1])}}"
loading="lazy"
target="_top"
></turbo-frame>
@endif
</turbo-frame>

@endfragment
</div>
</div>
</div>

Expand Down
6 changes: 4 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
Route::get('/feed', [PostController::class, 'list'])
->name('feed');

Route::post('/feed', [PostController::class, 'list'])
->middleware(\App\Http\Middleware\TurboStream::class);
//так из-за того что фрейм с lazy будет длеать get, а не post на адрес указанный в src
Route::get('/feed/list', [PostController::class, 'list'])
->middleware(\App\Http\Middleware\TurboRedirect::class)
->name('feed.more');

/*
Route::prefix('/stream')->middleware(\App\Http\Middleware\TurboStream::class)->group(function () {
Expand Down