From 1a7f0fe5edebd1864e408721e166466ca7f71b7e Mon Sep 17 00:00:00 2001 From: devoalda Date: Thu, 10 Aug 2023 21:32:26 +0800 Subject: [PATCH] perf(Migrated to Livewire): Moved Dashboard modules to Livewire, including loading of todo list in the dashboard --- app/Http/Controllers/DashboardController.php | 8 +- .../Livewire/Dashboard/AveTodoPerProject.php | 40 +++++ app/Http/Livewire/Dashboard/CompletedTodo.php | 29 ++++ .../Livewire/Dashboard/IncompleteTodo.php | 28 ++++ app/Http/Livewire/Dashboard/ProjectCount.php | 20 +++ app/Http/Livewire/Dashboard/Stats.php | 13 ++ app/Http/Livewire/Pomo/Timer.php | 7 + app/Http/Livewire/Todo/TodaysTodo.php | 52 ++++++ resources/views/dashboard/index.blade.php | 152 +----------------- resources/views/dashboard/load-todo.blade.php | 114 ++++++------- .../dashboard/ave-todo-per-project.blade.php | 19 +++ .../dashboard/completed-todo.blade.php | 19 +++ .../dashboard/incomplete-todo.blade.php | 19 +++ .../dashboard/project-count.blade.php | 19 +++ .../views/livewire/dashboard/stats.blade.php | 34 ++++ .../views/livewire/pomo/load-pomo.blade.php | 6 + resources/views/livewire/pomo/timer.blade.php | 7 + .../views/livewire/todo/todays-todo.blade.php | 101 ++++++++++++ 18 files changed, 476 insertions(+), 211 deletions(-) create mode 100644 app/Http/Livewire/Dashboard/AveTodoPerProject.php create mode 100644 app/Http/Livewire/Dashboard/CompletedTodo.php create mode 100644 app/Http/Livewire/Dashboard/IncompleteTodo.php create mode 100644 app/Http/Livewire/Dashboard/ProjectCount.php create mode 100644 app/Http/Livewire/Dashboard/Stats.php create mode 100644 app/Http/Livewire/Todo/TodaysTodo.php create mode 100644 resources/views/livewire/dashboard/ave-todo-per-project.blade.php create mode 100644 resources/views/livewire/dashboard/completed-todo.blade.php create mode 100644 resources/views/livewire/dashboard/incomplete-todo.blade.php create mode 100644 resources/views/livewire/dashboard/project-count.blade.php create mode 100644 resources/views/livewire/dashboard/stats.blade.php create mode 100644 resources/views/livewire/todo/todays-todo.blade.php diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 01c4b50..d37b83c 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -38,17 +38,15 @@ class DashboardController extends Controller $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']; +// $projects = $this->projects(); +// $project_count = $projects['project_count']; +// $ave_todo_count = $projects['ave_todo_count']; $todo_completed_count = $this->all_completed_todos()['completed_count']; return view('dashboard.index', compact( 'todos', 'incomplete_count', - 'project_count', - 'ave_todo_count', 'todo_completed_count' )); } diff --git a/app/Http/Livewire/Dashboard/AveTodoPerProject.php b/app/Http/Livewire/Dashboard/AveTodoPerProject.php new file mode 100644 index 0000000..e47a69b --- /dev/null +++ b/app/Http/Livewire/Dashboard/AveTodoPerProject.php @@ -0,0 +1,40 @@ +user(); + $projects = $user->projects; + $project_count = $projects->count(); + + // Average number of todos per project + $ave_todo_count = function ($projects) { + $todo_count = 0; + foreach ($projects as $project) { + $todo_count += $project->todos->count(); + } + return $todo_count / $projects->count(); + }; + + $this->ave_todo_count = $ave_todo_count($projects); + } + public function render() + { + return view('livewire.dashboard.ave-todo-per-project'); + } +} diff --git a/app/Http/Livewire/Dashboard/CompletedTodo.php b/app/Http/Livewire/Dashboard/CompletedTodo.php new file mode 100644 index 0000000..a336fd3 --- /dev/null +++ b/app/Http/Livewire/Dashboard/CompletedTodo.php @@ -0,0 +1,29 @@ +todo_completed_count = 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', '=', Auth::user()->id) + ->whereDate('due_end', '<=', strtotime('today midnight')) + ->whereNotNull('completed_at') + ->count(); + } + + public function render() + { + return view('livewire.dashboard.completed-todo'); + } +} diff --git a/app/Http/Livewire/Dashboard/IncompleteTodo.php b/app/Http/Livewire/Dashboard/IncompleteTodo.php new file mode 100644 index 0000000..fbde930 --- /dev/null +++ b/app/Http/Livewire/Dashboard/IncompleteTodo.php @@ -0,0 +1,28 @@ +incomplete_count = 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', '=', Auth::user()->id) + ->whereDate('due_end', '<=', strtotime('today midnight')) + ->whereNull('completed_at') + ->count(); + + } + public function render() + { + return view('livewire.dashboard.incomplete-todo'); + } +} diff --git a/app/Http/Livewire/Dashboard/ProjectCount.php b/app/Http/Livewire/Dashboard/ProjectCount.php new file mode 100644 index 0000000..f15bab5 --- /dev/null +++ b/app/Http/Livewire/Dashboard/ProjectCount.php @@ -0,0 +1,20 @@ +project_count = auth()->user()->projects()->count(); + } + + public function render() + { + return view('livewire.dashboard.project-count'); + } +} diff --git a/app/Http/Livewire/Dashboard/Stats.php b/app/Http/Livewire/Dashboard/Stats.php new file mode 100644 index 0000000..60568da --- /dev/null +++ b/app/Http/Livewire/Dashboard/Stats.php @@ -0,0 +1,13 @@ + 'endBreak', ]; + public function resetPomo() + { + $this->time = 25 * 60; + $this->countdown = false; + $this->break = false; + } + public function endBreak($time = null) { $this->time = $time ?? $this->time; diff --git a/app/Http/Livewire/Todo/TodaysTodo.php b/app/Http/Livewire/Todo/TodaysTodo.php new file mode 100644 index 0000000..3d19265 --- /dev/null +++ b/app/Http/Livewire/Todo/TodaysTodo.php @@ -0,0 +1,52 @@ + 'loadMore', + ]; + + public function loadMore() + { + $this->perPage += 5; + } + + public function render() + { + $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', '=', auth()->user()->id) + ->whereDate('due_end', '<=', strtotime('today midnight')) + ->whereNull('completed_at') + ->orderBy('due_end', 'asc') + ->paginate($this->perPage); + + + $todos->transform(function ($todo) { + return Todo::find($todo->id); + }); + + + $incomplete_count = 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', '=', auth()->user()->id) + ->whereDate('due_end', '<=', strtotime('today midnight')) + ->whereNull('completed_at') + ->count(); + + return view('livewire.todo.todays-todo', [ + 'todos' => $todos, + 'incomplete_count' => $incomplete_count, + ]); + } +} diff --git a/resources/views/dashboard/index.blade.php b/resources/views/dashboard/index.blade.php index ce303a5..43b276b 100644 --- a/resources/views/dashboard/index.blade.php +++ b/resources/views/dashboard/index.blade.php @@ -5,158 +5,10 @@ - -
-

- - - - - Your Stats -

-
-
-

- - - - - Total incomplete Todos -

-

- {{ $incomplete_count }} -

-
- -
-

- - - - - Total Completed Todos -

-

- {{ $todo_completed_count }} -

-
+ - -
-

- - - - - Average Todos per Project -

-

- {{ $ave_todo_count }} -

-
+ - -
-

- - - - - Total Projects -

-

- {{ $project_count }} -

-
- - - - -
- -
- - - -
-

- Today's Todos - ({{ $incomplete_count }}) -

-
- @include('dashboard.load-todo') -
- - -
- - diff --git a/resources/views/dashboard/load-todo.blade.php b/resources/views/dashboard/load-todo.blade.php index 377d20e..71d12ff 100644 --- a/resources/views/dashboard/load-todo.blade.php +++ b/resources/views/dashboard/load-todo.blade.php @@ -1,61 +1,63 @@ -@foreach ($todos as $todo) - -
-
-
- @csrf - @method('PUT') - -
- +
+ @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 +
+
+ @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 + @if ($timeRemaining !== null) + @if ($due->isFuture()) +

{{ $timeRemaining }} remaining

+ @else +

{{ $timeRemaining }} ago

+ @endif + @endif +
-
- -@endforeach + + @endforeach +
diff --git a/resources/views/livewire/dashboard/ave-todo-per-project.blade.php b/resources/views/livewire/dashboard/ave-todo-per-project.blade.php new file mode 100644 index 0000000..bac09a8 --- /dev/null +++ b/resources/views/livewire/dashboard/ave-todo-per-project.blade.php @@ -0,0 +1,19 @@ +
+

+ + + + + Average Todos per Project +

+

+ {{ $ave_todo_count }} +

+
diff --git a/resources/views/livewire/dashboard/completed-todo.blade.php b/resources/views/livewire/dashboard/completed-todo.blade.php new file mode 100644 index 0000000..4e53f28 --- /dev/null +++ b/resources/views/livewire/dashboard/completed-todo.blade.php @@ -0,0 +1,19 @@ +
+

+ + + + + Total Completed Todos +

+

+ {{ $todo_completed_count }} +

+
diff --git a/resources/views/livewire/dashboard/incomplete-todo.blade.php b/resources/views/livewire/dashboard/incomplete-todo.blade.php new file mode 100644 index 0000000..845f94d --- /dev/null +++ b/resources/views/livewire/dashboard/incomplete-todo.blade.php @@ -0,0 +1,19 @@ +
+

+ + + + + Total incomplete Todos +

+

+ {{ $incomplete_count }} +

+
diff --git a/resources/views/livewire/dashboard/project-count.blade.php b/resources/views/livewire/dashboard/project-count.blade.php new file mode 100644 index 0000000..7c4090e --- /dev/null +++ b/resources/views/livewire/dashboard/project-count.blade.php @@ -0,0 +1,19 @@ +
+

+ + + + + Total Projects +

+

+ {{ $project_count }} +

+
diff --git a/resources/views/livewire/dashboard/stats.blade.php b/resources/views/livewire/dashboard/stats.blade.php new file mode 100644 index 0000000..b161681 --- /dev/null +++ b/resources/views/livewire/dashboard/stats.blade.php @@ -0,0 +1,34 @@ + +
+

+ + + + + Your Stats +

+
+ + + + + + + + + + + + + + +
+ +
diff --git a/resources/views/livewire/pomo/load-pomo.blade.php b/resources/views/livewire/pomo/load-pomo.blade.php index 6da7b1c..296d84c 100644 --- a/resources/views/livewire/pomo/load-pomo.blade.php +++ b/resources/views/livewire/pomo/load-pomo.blade.php @@ -45,6 +45,12 @@
+
+
+ Loading... +
+
+ + + @else + +
+ Congratulations! You've reached the end of the list. +
+ @endif + +