diff --git a/app/Components/Role/Manager.php b/app/Components/Role/Manager.php new file mode 100644 index 0000000..5d9fdfe --- /dev/null +++ b/app/Components/Role/Manager.php @@ -0,0 +1,137 @@ +resetPage(); + } + + #[On('roleSaved')] + public function showIndex(): void + { + $this->mode = 'index'; + $this->resetPage(); + $this->name = ''; + $this->editingId = null; + } + + public function updatedSearch(string $value): void + { + $this->resetPage(); + } + + public function showForm(?int $id = null): void + { + $this->mode = 'form'; + $this->editingId = $id; + $this->name = $id + ? Role::findOrFail($id)->name + : ''; + } + + public function delete(int $id): void + { + Role::findOrFail($id)->delete(); + session()->flash('message', 'Role deleted.'); + $this->resetPage(); + } + + public function save(): void + { + $uniqueRule = 'unique:roles,name' + . ($this->editingId ? ',' . $this->editingId : ''); + + $this->validate([ + 'name' => "required|string|{$uniqueRule}", + ]); + + Role::updateOrCreate( + ['id' => $this->editingId], + ['name' => $this->name] + ); + + session()->flash('message', 'Role saved.'); + $this->dispatch('roleSaved'); + } + + protected function computeTitle(): string + { + if ($this->mode === 'index') { + return 'Roles'; + } + + if ($this->mode === 'form' && $this->editingId) { + return "Edit Role: {$this->name}"; + } + + return 'Create Role'; + } + + public function getTitleProperty(): string + { + return $this->computeTitle(); + } + + public function render() + { + $roles = Role::query() + ->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%")) + ->orderByDesc('id') + ->paginate($this->perPage); + + return view('components.role.manager', [ + 'roles' => $roles, + 'title' => $this->title, + ]); + } +} diff --git a/app/Http/Controllers/RoleController.php b/app/Http/Controllers/RoleController.php new file mode 100644 index 0000000..fddfae5 --- /dev/null +++ b/app/Http/Controllers/RoleController.php @@ -0,0 +1,13 @@ + +@endsection diff --git a/resources/views/components/role/manager.blade.php b/resources/views/components/role/manager.blade.php new file mode 100644 index 0000000..1f92114 --- /dev/null +++ b/resources/views/components/role/manager.blade.php @@ -0,0 +1,175 @@ +{{-- resources/views/livewire/role-manager.blade.php --}} + +
+
+
+ + {{-- Header --}} +
+
{{ $title }}
+ + @if ($mode === 'index') +
+ + {{-- 1. Dropdown chọn số bản ghi --}} +
+ +
+ + {{-- 2. Search box --}} +
+ +
+ + + {{-- 3. Create button --}} + +
+ @else + + @endif +
+ + {{-- Body --}} +
+ @if (session()->has('message')) +
{{ session('message') }}
+ @endif + + @if ($mode === 'index') + {{-- SKELETON TABLE --}} + + + {{-- ACTUAL TABLE --}} +
+ + + + + + + + + + @forelse($roles as $r) + + + + + + @empty + + + + @endforelse + +
IDNameActions
{{ $r->id }}{{ $r->name }} + + +
No roles found.
+
+ + {{-- Pagination --}} +
+ @if ($roles->lastPage() > 1) + {{ $roles->links() }} + @endif +
+ @else + {{-- SKELETON FORM --}} + + + {{-- ACTUAL FORM --}} +
+
+
+ + + @error('name') +
{{ $message }}
+ @enderror +
+
+ + +
+
+
+ @endif + +
+
+
+ + @script + + @endscript +
diff --git a/routes/web.php b/routes/web.php index 8ebac75..23d6328 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,14 +1,21 @@ name('dashboard') + ->middleware('auth'); -// Dashboard route (Livewire) for authenticated users only -Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard'); Route::middleware('auth')->group(function () { + // Permissions Route::get('/permissions', [PermissionController::class, 'manager']) ->name('permissions.index'); + + // Roles + Route::get('/roles', [RoleController::class, 'manager']) + ->name('roles.index'); });