You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 lines
3.9 KiB

<?php
namespace App\Components\User;
use Livewire\Component;
use Livewire\Attributes\On;
use Livewire\WithPagination;
use Illuminate\Validation\Rule;
use App\Models\User;
use Spatie\Permission\Models\Role;
class Manager extends Component
{
use WithPagination;
public array $perPageOptions = [10, 25, 50, 100];
public int $perPage = 10;
protected string $paginationTheme = 'bootstrap';
public string $mode = 'index';
public ?int $editingId = null;
public string $search = '';
// Form fields
public string $fullname = '';
public string $email = '';
public string $password = '';
public string $password_confirmation = '';
public array $roles = [];
public array $allRoles = [];
public function mount(): void
{
$this->allRoles = Role::all()->toArray();
}
public function updatedPerPage(int $value): void
{
$this->resetPage();
}
public function updatedSearch(string $value): void
{
$this->resetPage();
}
#[On('userSaved')]
public function showIndex(): void
{
$this->mode = 'index';
$this->resetPage();
$this->resetForm();
}
public function showForm(?int $id = null): void
{
$this->mode = 'form';
$this->editingId = $id;
if ($id) {
$user = User::with('roles')->findOrFail($id);
$this->fullname = $user->fullname;
$this->email = $user->email;
$this->roles = $user->roles->pluck('name')->toArray();
} else {
$this->resetForm();
}
}
protected function resetForm(): void
{
$this->fullname = '';
$this->email = '';
$this->password = '';
$this->password_confirmation = '';
$this->roles = [];
$this->editingId = null;
}
public function delete(int $id): void
{
User::findOrFail($id)->delete();
session()->flash('message', 'User deleted.');
$this->resetPage();
}
public function save(): void
{
$rules = [
'fullname' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')->ignore($this->editingId)],
'roles' => ['nullable', 'array'],
'roles.*' => ['string', Rule::exists('roles', 'name')],
];
if (! $this->editingId) {
$rules['password'] = ['required', 'string', 'min:6', 'confirmed'];
} elseif ($this->password) {
$rules['password'] = ['string', 'min:6', 'confirmed'];
}
$this->validate($rules);
$user = $this->editingId
? User::findOrFail($this->editingId)
: new User;
$user->fullname = $this->fullname;
$user->email = $this->email;
if ($this->password) {
$user->password = $this->password;
}
$user->save();
$user->syncRoles($this->roles);
session()->flash('message', 'User saved.');
$this->dispatch('userSaved');
}
public function render()
{
$users = User::with('roles')
->when(
$this->search,
fn($q) => $q
->where('fullname', 'like', "%{$this->search}%")
->orWhere('email', 'like', "%{$this->search}%")
)
->orderByDesc('id')
->paginate($this->perPage);
return view('components.user.manager', [
'users' => $users,
'allRoles' => collect($this->allRoles),
'title' => $this->mode === 'index'
? 'Users'
: ($this->editingId ? 'Edit User' : 'Create User'),
]);
}
}