From 59ffb4bc327350aa3c542a7d228455b3462e426a Mon Sep 17 00:00:00 2001 From: devoalda Date: Tue, 8 Aug 2023 19:46:57 +0800 Subject: [PATCH] feature(Infinite Scrolling): Implemented Infinite Scrolling with AJAX in Dashboard --- app/Http/Controllers/DashboardController.php | 28 +++++- resources/views/dashboard.blade.php | 97 +++++++------------ resources/views/dashboard/load-todo.blade.php | 61 ++++++++++++ resources/views/layouts/app.blade.php | 1 + 4 files changed, 120 insertions(+), 67 deletions(-) create mode 100644 resources/views/dashboard/load-todo.blade.php diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 9a2b6a4..812ccfd 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -4,20 +4,40 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Response; class DashboardController extends Controller { - public function index() + public function index(Request $request) { + $user = auth()->user(); + $data = $this->all_incomplete_todos(); - $todos = $data['todos']; - $incomplete_count = $data['incomplete_count']; - // Convert all todo to Todo model + $todos = DB::table('todos') + ->join('project_todo', 'todos.id', '=', 'project_todo.todo_id') + ->join('project_user', 'project_todo.project_id', '=', 'project_user.project_id') + ->where('project_user.user_id', '=', $user->id) + ->whereDate('due_end', '<=', strtotime('today midnight')) + ->whereNull('completed_at') + ->orderBy('due_end', 'asc') + ->paginate(5); + $todos->transform(function ($todo) { return \App\Models\Todo::find($todo->id); }); + if ($request->ajax()){ + $view = view('dashboard.load-todo', compact('todos'))->render(); + return Response::json([ + 'view' => $view, + 'nextPageUrl' => $todos->nextPageUrl(), + ]); + } + + $incomplete_count = $data['incomplete_count']; + // Convert all todo to Todo model + $projects = $this->projects(); $project_count = $projects['project_count']; $ave_todo_count = $projects['ave_todo_count']; diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 163fd3e..da3fc84 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -119,74 +119,45 @@ Today's Todos ({{ $incomplete_count }}) -
- @foreach ($todos as $todo) - -
-
-
- @csrf - @method('PUT') - -
- - {{ $todo->title }} - - -
-
- @php - if ($todo->due_start && $todo->due_end) { - $due = \Carbon\Carbon::parse($todo->due_end); - $now = now(); - $timeRemaining = $due->isFuture() ? $now->diffForHumans($due, true) : - $due->diffForHumans($now, - true); - } elseif ($todo->due_start) { - $due = \Carbon\Carbon::parse($todo->due_start); - $now = now(); - $timeRemaining = $due->isFuture() ? $now->diffForHumans($due, true) : - $due->diffForHumans($now, - true); - } elseif ($todo->due_end) { - $due = \Carbon\Carbon::parse($todo->due_end); - $now = now(); - $timeRemaining = $due->isFuture() ? $now->diffForHumans($due, true) : - $due->diffForHumans($now, - true); - } else { - // If there is no due_start or due_end, set $timeRemaining to null - $timeRemaining = null; - } - @endphp - - @if ($timeRemaining !== null) - @if ($due->isFuture()) -

{{ $timeRemaining }} remaining

- @else -

{{ $timeRemaining }} ago

- @endif - @endif -
-
-
- @endforeach +
+ @include('dashboard.load-todo')
-
+ + + diff --git a/resources/views/dashboard/load-todo.blade.php b/resources/views/dashboard/load-todo.blade.php new file mode 100644 index 0000000..377d20e --- /dev/null +++ b/resources/views/dashboard/load-todo.blade.php @@ -0,0 +1,61 @@ +@foreach ($todos as $todo) + +
+
+
+ @csrf + @method('PUT') + +
+ + {{ $todo->title }} + + +
+
+ @php + if ($todo->due_start && $todo->due_end) { + $due = \Carbon\Carbon::parse($todo->due_end); + $now = now(); + $timeRemaining = $due->isFuture() ? $now->diffForHumans($due, true) : + $due->diffForHumans($now, + true); + } elseif ($todo->due_start) { + $due = \Carbon\Carbon::parse($todo->due_start); + $now = now(); + $timeRemaining = $due->isFuture() ? $now->diffForHumans($due, true) : + $due->diffForHumans($now, + true); + } elseif ($todo->due_end) { + $due = \Carbon\Carbon::parse($todo->due_end); + $now = now(); + $timeRemaining = $due->isFuture() ? $now->diffForHumans($due, true) : + $due->diffForHumans($now, + true); + } else { + // If there is no due_start or due_end, set $timeRemaining to null + $timeRemaining = null; + } + @endphp + + @if ($timeRemaining !== null) + @if ($due->isFuture()) +

{{ $timeRemaining }} remaining

+ @else +

{{ $timeRemaining }} ago

+ @endif + @endif +
+
+
+@endforeach diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index a7ce9bd..b8ebd41 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -14,6 +14,7 @@ @vite(['resources/css/app.css', 'resources/js/app.js']) @livewireStyles +