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 --}} + +
ID | +Name | +Actions | +
---|---|---|
{{ $r->id }} | +{{ $r->name }} | ++ + + | +
No roles found. | +