diff --git a/app/Livewire/UserManagement.php b/app/Livewire/UserManagement.php index a06b058..0aaae0b 100644 --- a/app/Livewire/UserManagement.php +++ b/app/Livewire/UserManagement.php @@ -6,8 +6,83 @@ use Livewire\Component; class UserManagement extends Component { + public $search = ''; + public $sortField = 'username'; + public $sortDirection = 'asc'; + public $currentPage = 1; + public $perPage = 10; + public $expandEllipsis = false; + + protected $queryString = ['search']; + + protected $listeners = ['paginate-to' => 'setPage']; + + public function updatingSearch() + { + $this->currentPage = 1; + } + + public function sortBy($field) + { + if ($this->sortField === $field) { + $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'; + } else { + $this->sortField = $field; + $this->sortDirection = 'asc'; + } + } + + public function searchEnter() + { + $this->currentPage = 1; + } + + public function getFilteredUsersProperty() + { + $users = collect(json_decode(file_get_contents(storage_path('app/users.json')), true)); + + if ($this->search) { + $users = $users->filter(function ($user) { + return str_contains(strtolower($user['username']), strtolower($this->search)) || + str_contains(strtolower($user['first_name']), strtolower($this->search)) || + str_contains(strtolower($user['last_name']), strtolower($this->search)) || + str_contains(strtolower($user['email']), strtolower($this->search)); + }); + } + + return $users->sortBy([[$this->sortField, $this->sortDirection]])->values(); + } + + // Change this to a method instead of a property + public function calculateTotalPages() + { + return ceil($this->filteredUsers->count() / $this->perPage); + } + + public function getPaginatedUsersProperty() + { + return $this->filteredUsers + ->slice(($this->currentPage - 1) * $this->perPage, $this->perPage) + ->values(); + } + + public function setPage($page) + { + $this->currentPage = max(1, min($this->calculateTotalPages(), $page)); + $this->expandEllipsis = false; + } + public function toggleEllipsis() + { + $this->expandEllipsis = !$this->expandEllipsis; + } + public function render() { - return view('livewire.user-management.user-management'); + return view('livewire.user-management.user-management', [ + 'users' => $this->paginatedUsers, + 'totalPages' => $this->calculateTotalPages(), // Now using the method + 'currentPage' => $this->currentPage, + 'expandEllipsis' => $this->expandEllipsis, + ]); } } diff --git a/resources/views/components/pagination.blade.php b/resources/views/components/pagination.blade.php new file mode 100644 index 0000000..9daa243 --- /dev/null +++ b/resources/views/components/pagination.blade.php @@ -0,0 +1,77 @@ +@props(['currentPage', 'totalPages']) + +@if ($totalPages > 1) + +@endif diff --git a/resources/views/layouts/dashboard.blade.php b/resources/views/layouts/dashboard.blade.php index b67ed5f..fba2f71 100644 --- a/resources/views/layouts/dashboard.blade.php +++ b/resources/views/layouts/dashboard.blade.php @@ -2,9 +2,11 @@
-