2026-05-09 23:32:22 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Livewire;
|
|
|
|
|
|
|
|
|
|
use App\Models\Project;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2026-06-18 16:22:59 +02:00
|
|
|
use Livewire\Attributes\On;
|
|
|
|
|
use Livewire\Component;
|
2026-05-09 23:32:22 +02:00
|
|
|
|
|
|
|
|
class ProjectUsers extends Component
|
|
|
|
|
{
|
|
|
|
|
public Project $project;
|
|
|
|
|
public $allUsers = [];
|
|
|
|
|
public $selectedUserId = '';
|
|
|
|
|
public $selectedRole = 'viewer';
|
|
|
|
|
|
|
|
|
|
public function mount(Project $project)
|
|
|
|
|
{
|
|
|
|
|
$this->project = $project;
|
2026-06-18 16:22:59 +02:00
|
|
|
$this->loadAvailable();
|
2026-05-09 23:32:22 +02:00
|
|
|
}
|
|
|
|
|
|
2026-06-18 16:22:59 +02:00
|
|
|
/** Users not yet assigned to the project (for the dropdown). */
|
|
|
|
|
public function loadAvailable(): void
|
2026-05-09 23:32:22 +02:00
|
|
|
{
|
2026-06-18 16:22:59 +02:00
|
|
|
$assignedIds = $this->project->users()->pluck('users.id')->toArray();
|
2026-05-09 23:32:22 +02:00
|
|
|
$this->allUsers = User::whereNotIn('id', $assignedIds)->orderBy('name')->get();
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-18 16:22:59 +02:00
|
|
|
/** Reload the dropdown when the embedded table changes assignments. */
|
|
|
|
|
#[On('project-users-changed')]
|
|
|
|
|
public function onUsersChanged(): void
|
|
|
|
|
{
|
|
|
|
|
$this->loadAvailable();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 23:32:22 +02:00
|
|
|
public function assignUser()
|
|
|
|
|
{
|
2026-06-18 16:22:59 +02:00
|
|
|
abort_unless(Auth::user()->can('assign users'), 403);
|
2026-05-09 23:32:22 +02:00
|
|
|
|
|
|
|
|
$this->validate([
|
|
|
|
|
'selectedUserId' => 'required|exists:users,id',
|
2026-06-18 16:22:59 +02:00
|
|
|
'selectedRole' => 'required|in:' . implode(',', array_keys(ProjectUsersTable::ROLES)),
|
2026-05-09 23:32:22 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->project->users()->attach($this->selectedUserId, [
|
2026-06-18 16:22:59 +02:00
|
|
|
'role_in_project' => $this->selectedRole,
|
2026-05-09 23:32:22 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->reset(['selectedUserId', 'selectedRole']);
|
2026-06-18 16:22:59 +02:00
|
|
|
$this->loadAvailable();
|
|
|
|
|
$this->dispatch('project-users-changed');
|
2026-05-09 23:32:22 +02:00
|
|
|
$this->dispatch('notify', 'Usuario asignado al proyecto.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
2026-06-18 16:22:59 +02:00
|
|
|
return view('livewire.project-users', [
|
|
|
|
|
'roles' => ProjectUsersTable::ROLES,
|
|
|
|
|
]);
|
2026-05-09 23:32:22 +02:00
|
|
|
}
|
2026-06-18 16:22:59 +02:00
|
|
|
}
|