-
Notifications
You must be signed in to change notification settings - Fork 2
/
CreateBooth.php
142 lines (121 loc) · 3.49 KB
/
CreateBooth.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace App\Livewire\Booth;
use App\Livewire\Forms\BoothForm;
use App\Models\Booth;
use App\Models\Company;
use App\Models\User;
use Illuminate\View\View;
use Livewire\Attributes\Validate;
use Livewire\Component;
class CreateBooth extends Component
{
#[Validate(['required', 'numeric', 'min:1', 'max:10'])]
public $width;
#[Validate(['required', 'numeric', 'min:1', 'max:10'])]
public $length;
#[Validate(['nullable', 'max:255'])]
public $additionalInformation;
public $company;
public $companies;
public $companyId;
public $searchValue;
public $isDropdownVisible;
public $users;
public $selectedUser;
/**
* Initializes the component
* @return void
*/
public function mount()
{
$this->companies = Company::whereDoesntHave('booth')->where('is_approved', '=', '1')->get();
$this->company = $this->companies->first();
$this->isDropdownVisible = false;
$this->users = optional($this->company)->users;
$this->searchValue = '';
}
/**
* Creates the entity
* @return void
*/
public function save()
{
$this->validate();
$this->validate([
'company' => 'required',
'selectedUser' => 'required'
]);
$boothData = [
'width' => $this->width,
'length' => $this->length,
'additional_information' => $this->additionalInformation,
'company_id' => $this->company->id,
'is_approved' => true
];
Booth::create($boothData);
$this->selectedUser->assignRole('booth owner');
if ($this->selectedUser->hasRole('pending booth owner')) {
$this->selectedUser->removeRole('pending booth owner');
}
$this->redirect(route('moderator.booths.index'));
}
/**
* Triggers the filtering function if the email/name is being changed
*
* @return void
*/
public function updatedSearchValue() : void
{
$this->users = $this->company->users;
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;
});
}
}
/**
* Handles the update of the company id
* @return void
*/
public function updatedCompanyId() : void
{
$this->company = Company::find($this->companyId);
$this->users = $this->company->users;
$this->searchValue = '';
$this->updatedSearchValue();
$this->selectedUser = null;
$this->isDropdownVisible = false;
}
/**
* 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;
}
/**
* 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.booth.create-booth');
}
}