|
|
|
@ -12,39 +12,50 @@ class Manager extends Component |
|
|
|
|
use WithPagination; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Number of items per page for pagination. |
|
|
|
|
* Các tùy chọn số bản ghi mỗi trang. |
|
|
|
|
* |
|
|
|
|
* @var int[] |
|
|
|
|
*/ |
|
|
|
|
public array $perPageOptions = [5, 10, 25, 50, 100]; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Số bản ghi mỗi trang (mặc định). |
|
|
|
|
*/ |
|
|
|
|
public int $perPage = 10; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Pagination theme (bootstrap or tailwind). |
|
|
|
|
* Giao diện phân trang (bootstrap hoặc tailwind). |
|
|
|
|
*/ |
|
|
|
|
protected string $paginationTheme = 'bootstrap'; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Current UI mode: 'index' (list view) or 'form' (create/edit view). |
|
|
|
|
* Chế độ UI: 'index' (danh sách) hoặc 'form' (tạo/sửa). |
|
|
|
|
*/ |
|
|
|
|
public string $mode = 'index'; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* ID of the permission currently being edited. |
|
|
|
|
* Null when creating a new permission. |
|
|
|
|
* ID của permission đang sửa (null khi tạo mới). |
|
|
|
|
*/ |
|
|
|
|
public ?int $editingId = null; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Current search query string. |
|
|
|
|
* Chuỗi tìm kiếm hiện tại. |
|
|
|
|
*/ |
|
|
|
|
public string $search = ''; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Permission name used in the create/edit form. |
|
|
|
|
* Tên permission dùng trong form. |
|
|
|
|
*/ |
|
|
|
|
public string $name = ''; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Handle internal Livewire event to switch back to the index (list) view. |
|
|
|
|
* Khi thay đổi số bản ghi mỗi trang, reset về trang 1. |
|
|
|
|
*/ |
|
|
|
|
public function updatedPerPage(int $value): void |
|
|
|
|
{ |
|
|
|
|
$this->resetPage(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[On('permissionSaved')] |
|
|
|
|
public function showIndex(): void |
|
|
|
|
{ |
|
|
|
@ -54,23 +65,11 @@ class Manager extends Component |
|
|
|
|
$this->editingId = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Reset pagination whenever the search term changes. |
|
|
|
|
* |
|
|
|
|
* @param string $value |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
public function updatedSearch(string $value): void |
|
|
|
|
{ |
|
|
|
|
$this->resetPage(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Switch to the form view for creating or editing a permission. |
|
|
|
|
* |
|
|
|
|
* @param int|null $id Permission ID to edit, or null to create new. |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
public function showForm(?int $id = null): void |
|
|
|
|
{ |
|
|
|
|
$this->mode = 'form'; |
|
|
|
@ -80,12 +79,6 @@ class Manager extends Component |
|
|
|
|
: ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Delete a permission by its ID. |
|
|
|
|
* |
|
|
|
|
* @param int $id |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
public function delete(int $id): void |
|
|
|
|
{ |
|
|
|
|
Permission::findOrFail($id)->delete(); |
|
|
|
@ -93,11 +86,6 @@ class Manager extends Component |
|
|
|
|
$this->resetPage(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Validate and save the permission (create or update). |
|
|
|
|
* |
|
|
|
|
* @return void |
|
|
|
|
*/ |
|
|
|
|
public function save(): void |
|
|
|
|
{ |
|
|
|
|
$uniqueRule = 'unique:permissions,name' |
|
|
|
@ -113,16 +101,9 @@ class Manager extends Component |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
session()->flash('message', 'Permission saved.'); |
|
|
|
|
|
|
|
|
|
// Trigger internal Livewire event to return to the list view |
|
|
|
|
$this->dispatch('permissionSaved'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Compute dynamic browser title based on current mode and editing state. |
|
|
|
|
* |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
protected function computeTitle(): string |
|
|
|
|
{ |
|
|
|
|
if ($this->mode === 'index') { |
|
|
|
@ -136,21 +117,11 @@ class Manager extends Component |
|
|
|
|
return 'Create Permission'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Expose computed title as a Livewire property. |
|
|
|
|
* |
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
public function getTitleProperty(): string |
|
|
|
|
{ |
|
|
|
|
return $this->computeTitle(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Render the component view with paginated permissions and dynamic title. |
|
|
|
|
* |
|
|
|
|
* @return \Illuminate\View\View |
|
|
|
|
*/ |
|
|
|
|
public function render() |
|
|
|
|
{ |
|
|
|
|
$permissions = Permission::query() |
|
|
|
@ -160,7 +131,6 @@ class Manager extends Component |
|
|
|
|
|
|
|
|
|
return view('components.permission.manager', [ |
|
|
|
|
'permissions' => $permissions, |
|
|
|
|
// Blade can now access {{ $title }} via the computed property |
|
|
|
|
'title' => $this->title, |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|