89 lines
2.5 KiB
PHP
89 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
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', [
|
|
'users' => $this->paginatedUsers,
|
|
'totalPages' => $this->calculateTotalPages(), // Now using the method
|
|
'currentPage' => $this->currentPage,
|
|
'expandEllipsis' => $this->expandEllipsis,
|
|
]);
|
|
}
|
|
}
|