From 99d37aafb6dc30df82d753c0a4dc8e37899cf618 Mon Sep 17 00:00:00 2001
From: devoalda
Date: Tue, 8 Aug 2023 22:18:21 +0800
Subject: [PATCH] perf(Project Loading):
Added Lazy load for project
Restructured some files
---
app/Http/Controllers/DashboardController.php | 2 +-
app/Http/Controllers/ProjectController.php | 15 +++-
app/Http/Livewire/Pomo/Pomos.php | 8 ++
.../index.blade.php} | 61 +++++++--------
.../livewire/dashboard/pomo-count.blade.php | 2 +-
.../views/livewire/pomo/load-pomo.blade.php | 46 +++++++++++
resources/views/livewire/pomo/pomos.blade.php | 49 +-----------
resources/views/project/index.blade.php | 78 +++++++++----------
.../views/project/load-projects.blade.php | 42 ++++++++++
routes/web.php | 2 +-
10 files changed, 179 insertions(+), 126 deletions(-)
rename resources/views/{dashboard.blade.php => dashboard/index.blade.php} (82%)
create mode 100644 resources/views/livewire/pomo/load-pomo.blade.php
create mode 100644 resources/views/project/load-projects.blade.php
diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php
index 812ccfd..267fc72 100644
--- a/app/Http/Controllers/DashboardController.php
+++ b/app/Http/Controllers/DashboardController.php
@@ -44,7 +44,7 @@ class DashboardController extends Controller
$todo_completed_count = $this->all_completed_todos()['completed_count'];
- return view('dashboard', compact(
+ return view('dashboard.index', compact(
'todos',
'incomplete_count',
'project_count',
diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php
index f84bcbb..4dac2cd 100644
--- a/app/Http/Controllers/ProjectController.php
+++ b/app/Http/Controllers/ProjectController.php
@@ -8,9 +8,12 @@ use App\Models\Project;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
+use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use App\Models\User;
+use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Response;
class ProjectController extends Controller
@@ -18,15 +21,23 @@ class ProjectController extends Controller
/**
* Display Listing of all Projects.
*/
- public function index(): Application|Factory|View
+ public function index(Request $request): Application|Factory|View|JsonResponse
{
$user = User::find(auth()->user()->id);
- $projects = $user->projects;
+ $projects = $user->projects()->paginate(4);
// Aggregate all todos for all projects
$todos = $projects->map(function ($project) {
return $project->todos;
})->flatten();
+ if ($request->ajax()){
+ $view = view('project.load-projects', compact('projects'))->render();
+ return Response::json([
+ 'view' => $view,
+ 'nextPageUrl' => $projects->nextPageUrl(),
+ ]);
+ }
+
return view('project.index', [
'projects' => $projects,
'todos' => $todos->whereNull('completed_at')->values(),
diff --git a/app/Http/Livewire/Pomo/Pomos.php b/app/Http/Livewire/Pomo/Pomos.php
index d76580d..525761b 100644
--- a/app/Http/Livewire/Pomo/Pomos.php
+++ b/app/Http/Livewire/Pomo/Pomos.php
@@ -12,6 +12,14 @@ use App\Models\{
class Pomos extends Component
{
+ public $perPage = 10;
+
+ public function loadMore()
+ {
+ $this->perPage += 10;
+ }
+
+
public function render()
{
$user = User::find(auth()->id());
diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard/index.blade.php
similarity index 82%
rename from resources/views/dashboard.blade.php
rename to resources/views/dashboard/index.blade.php
index da3fc84..ce303a5 100644
--- a/resources/views/dashboard.blade.php
+++ b/resources/views/dashboard/index.blade.php
@@ -105,16 +105,16 @@
-
+
-
+
-
+
Today's Todos
({{ $incomplete_count }})
@@ -122,42 +122,41 @@
@include('dashboard.load-todo')
-
-
-
-
- {{ $todos->links() }}
+
+
+ {{ $todos->links() }}
+
diff --git a/resources/views/livewire/dashboard/pomo-count.blade.php b/resources/views/livewire/dashboard/pomo-count.blade.php
index dfdc3d0..461910b 100644
--- a/resources/views/livewire/dashboard/pomo-count.blade.php
+++ b/resources/views/livewire/dashboard/pomo-count.blade.php
@@ -1,6 +1,6 @@
-
+
diff --git a/resources/views/project/index.blade.php b/resources/views/project/index.blade.php
index fe4a90b..509640a 100644
--- a/resources/views/project/index.blade.php
+++ b/resources/views/project/index.blade.php
@@ -11,53 +11,16 @@
-
- @foreach($projects as $project)
-
- @endforeach
+
+ @include('project.load-projects')
+
+ {{ $projects->links() }}
+
+
@include('todo.todo_list')
diff --git a/resources/views/project/load-projects.blade.php b/resources/views/project/load-projects.blade.php
new file mode 100644
index 0000000..d8d2e30
--- /dev/null
+++ b/resources/views/project/load-projects.blade.php
@@ -0,0 +1,42 @@
+@foreach($projects as $project)
+
+@endforeach
diff --git a/routes/web.php b/routes/web.php
index 6cb7bf1..9d9d86b 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -19,7 +19,7 @@ use Illuminate\Support\Facades\Route;
*/
Route::get('/', function () {
- return redirect(route('project.index'));
+ return redirect(route('dashboard'));
});
Route::get('/dashboard', [DashboardController::class, 'index'])