Add loading Permission

pull/2/head
sundayenglish 4 weeks ago
parent 046c6abedc
commit 30e940aaf8
  1. 68
      app/Components/Permission/Manager.php
  2. 32
      resources/views/components/permission/manager.blade.php

@ -12,39 +12,50 @@ class Manager extends Component
use WithPagination; 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; public int $perPage = 10;
/** /**
* Pagination theme (bootstrap or tailwind). * Giao diện phân trang (bootstrap hoặc tailwind).
*/ */
protected string $paginationTheme = 'bootstrap'; 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'; public string $mode = 'index';
/** /**
* ID of the permission currently being edited. * ID của permission đang sửa (null khi tạo mới).
* Null when creating a new permission.
*/ */
public ?int $editingId = null; public ?int $editingId = null;
/** /**
* Current search query string. * Chuỗi tìm kiếm hiện tại.
*/ */
public string $search = ''; public string $search = '';
/** /**
* Permission name used in the create/edit form. * Tên permission dùng trong form.
*/ */
public string $name = ''; 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')] #[On('permissionSaved')]
public function showIndex(): void public function showIndex(): void
{ {
@ -54,23 +65,11 @@ class Manager extends Component
$this->editingId = null; $this->editingId = null;
} }
/**
* Reset pagination whenever the search term changes.
*
* @param string $value
* @return void
*/
public function updatedSearch(string $value): void public function updatedSearch(string $value): void
{ {
$this->resetPage(); $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 public function showForm(?int $id = null): void
{ {
$this->mode = 'form'; $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 public function delete(int $id): void
{ {
Permission::findOrFail($id)->delete(); Permission::findOrFail($id)->delete();
@ -93,11 +86,6 @@ class Manager extends Component
$this->resetPage(); $this->resetPage();
} }
/**
* Validate and save the permission (create or update).
*
* @return void
*/
public function save(): void public function save(): void
{ {
$uniqueRule = 'unique:permissions,name' $uniqueRule = 'unique:permissions,name'
@ -113,16 +101,9 @@ class Manager extends Component
); );
session()->flash('message', 'Permission saved.'); session()->flash('message', 'Permission saved.');
// Trigger internal Livewire event to return to the list view
$this->dispatch('permissionSaved'); $this->dispatch('permissionSaved');
} }
/**
* Compute dynamic browser title based on current mode and editing state.
*
* @return string
*/
protected function computeTitle(): string protected function computeTitle(): string
{ {
if ($this->mode === 'index') { if ($this->mode === 'index') {
@ -136,21 +117,11 @@ class Manager extends Component
return 'Create Permission'; return 'Create Permission';
} }
/**
* Expose computed title as a Livewire property.
*
* @return string
*/
public function getTitleProperty(): string public function getTitleProperty(): string
{ {
return $this->computeTitle(); return $this->computeTitle();
} }
/**
* Render the component view with paginated permissions and dynamic title.
*
* @return \Illuminate\View\View
*/
public function render() public function render()
{ {
$permissions = Permission::query() $permissions = Permission::query()
@ -160,7 +131,6 @@ class Manager extends Component
return view('components.permission.manager', [ return view('components.permission.manager', [
'permissions' => $permissions, 'permissions' => $permissions,
// Blade can now access {{ $title }} via the computed property
'title' => $this->title, 'title' => $this->title,
]); ]);
} }

@ -10,6 +10,18 @@
@if ($mode === 'index') @if ($mode === 'index')
<div class="d-flex align-items-center"> <div class="d-flex align-items-center">
{{-- 1. Dropdown chọn số bản ghi --}}
<div class="me-2">
<select wire:model="perPage" class="form-select form-select-sm"
style="width: 70px; display:inline-block;">
@foreach ($perPageOptions as $opt)
<option value="{{ $opt }}">{{ $opt }}</option>
@endforeach
</select>
</div>
{{-- 2. Search box --}}
<div class="input-group input-group-outline me-2"> <div class="input-group input-group-outline me-2">
<input type="text" wire:model.debounce.500ms="search" wire:keydown.enter="resetPage" <input type="text" wire:model.debounce.500ms="search" wire:keydown.enter="resetPage"
class="form-control" placeholder="Search..."> class="form-control" placeholder="Search...">
@ -17,7 +29,8 @@
<button wire:click="resetPage" class="btn btn-sm btn-outline-success me-2 m-0"> <button wire:click="resetPage" class="btn btn-sm btn-outline-success me-2 m-0">
<i class="fas fa-search me-1" style="font-size: 1.2rem;"></i> <i class="fas fa-search me-1" style="font-size: 1.2rem;"></i>
</button> </button>
<!-- Create lại bình thường -->
{{-- 3. Create button --}}
<button wire:click="showForm" class="btn btn-sm btn-info m-0"> <button wire:click="showForm" class="btn btn-sm btn-info m-0">
Create Create
</button> </button>
@ -59,12 +72,11 @@
class="btn btn-sm btn-outline-info me-1"> class="btn btn-sm btn-outline-info me-1">
Edit Edit
</button> </button>
<!-- Delete with confirmation -->
<button x-data <button x-data
@click.prevent=" @click.prevent="
if (confirm('Bạn có chắc chắn muốn xóa permission này?')) { if (confirm('Bạn có chắc chắn muốn xóa permission này?')) {
$wire.delete({{ $p->id }}) $wire.delete({{ $p->id }})
} }
" "
class="btn btn-sm btn-danger"> class="btn btn-sm btn-danger">
Delete Delete
@ -92,7 +104,6 @@
{{-- ACTUAL FORM --}} {{-- ACTUAL FORM --}}
<div wire:loading.remove x-data> <div wire:loading.remove x-data>
{{-- <h6 class="mb-3">{{ $editingId ? 'Edit Permission' : 'Create Permission' }}</h6> --}}
<form> <form>
<div class="mb-3"> <div class="mb-3">
<label class="form-label">Name</label> <label class="form-label">Name</label>
@ -106,13 +117,12 @@
<button wire:click="showIndex" type="button" class="btn btn-secondary btn-sm me-2"> <button wire:click="showIndex" type="button" class="btn btn-secondary btn-sm me-2">
Cancel Cancel
</button> </button>
<!-- Chỉ confirm khi submit form -->
<button type="button" <button type="button"
@click.prevent=" @click.prevent="
if (confirm('Bạn có chắc chắn muốn {{ $editingId ? 'cập nhật' : 'tạo' }} permission này?')) { if (confirm('Bạn có chắc chắn muốn {{ $editingId ? 'cập nhật' : 'tạo' }} permission này?')) {
$wire.save() $wire.save()
} }
" "
class="btn btn-info btn-sm"> class="btn btn-info btn-sm">
{{ $editingId ? 'Update' : 'Create' }} {{ $editingId ? 'Update' : 'Create' }}
</button> </button>

Loading…
Cancel
Save