-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAddParticipant.php
111 lines (100 loc) · 2.96 KB
/
AddParticipant.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace App\Livewire\Company;
use App\Actions\Users\AddParticipantToCompanyHandler;
use App\Models\Company;
use App\Models\User;
use Exception;
use Illuminate\View\View;
use Livewire\Component;
use LivewireUI\Modal\ModalComponent;
use Masmerise\Toaster\Toaster;
class AddParticipant extends ModalComponent
{
public $company;
public $searchValue;
public $isDropdownVisible;
public $users;
public $defaultUsers;
public $selectedUser;
public $canAssignRole;
public $assignedRole;
/**
* Initializes the component
* @return void
*/
public function mount($companyId)
{
$this->company = Company::find($companyId);
$this->searchValue = '';
$this->defaultUsers = User::role('participant')
->whereNull('company_id')
->get()
->filter(function ($user) {
return $user->hasExactRoles('participant');
});
$this->users = $this->defaultUsers;
$this->isDropdownVisible = false;
$this->canAssignRole = false;
$this->assignedRole = 'company member';
}
/**
* Triggers the filtering function if the email/name is being changed
*
* @return void
*/
public function updatedSearchValue(): void
{
$this->users = $this->defaultUsers;
if (!empty($this->searchValue)) {
$this->users = $this->users->filter(function ($user) {
$nameMatch = stripos($user->name, $this->searchValue) !== false;
$emailMatch = stripos($user->email, $this->searchValue) !== false;
return $nameMatch || $emailMatch;
});
}
}
/**
* Assigns the participant to a company or handles the failure of assigning
* @return \Illuminate\Http\RedirectResponse|void
*/
public function save()
{
try {
(new AddParticipantToCompanyHandler())->execute($this->selectedUser, $this->company, $this->assignedRole);
return redirect()->to(route('moderator.companies.show', $this->company));
} catch (Exception $exception) {
Toaster::error($exception->getMessage());
}
}
/**
* Triggered when the user chooses the assignee
*
* @param $id
* @return void
*/
public function selectUser($id): void
{
$this->selectedUser = User::find($id);
$this->searchValue = $this->selectedUser->name;
$this->updatedSearchValue();
$this->isDropdownVisible = false;
$this->canAssignRole = is_null($this->selectedUser->presenter_of);
}
/**
* Responsible for the visibility status of the dropdown
*
* @return void
*/
public function toggleDropdown(): void
{
$this->isDropdownVisible = !$this->isDropdownVisible;
}
/**
* Renders the component
* @return View
*/
public function render(): View
{
return view('livewire.company.add-participant');
}
}