Skip to content

Commit b17d8b5

Browse files
committed
Resolve Sonar duplicated lines
1 parent 0d3d512 commit b17d8b5

8 files changed

Lines changed: 96 additions & 122 deletions

File tree

app/Core/CrudDialogHelper.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Core;
4+
5+
use Exception;
6+
use Filament\Notifications\Actions\Action;
7+
use Filament\Notifications\Notification;
8+
9+
trait CrudDialogHelper
10+
{
11+
12+
public bool $deleteConfirmationOpened = false;
13+
14+
/**
15+
* Delete confirmation function
16+
*
17+
* @param string $title
18+
* @param string $body
19+
* @param string $deleteEvent
20+
* @param string $cancelEvent
21+
* @return void
22+
* @throws Exception
23+
*/
24+
public function deleteConfirmation(string $title, string $body, string $deleteEvent, string $cancelEvent): void
25+
{
26+
$this->deleteConfirmationOpened = true;
27+
Notification::make()
28+
->warning()
29+
->title($title)
30+
->body($body)
31+
->actions([
32+
Action::make('confirm')
33+
->label(__('Confirm'))
34+
->color('danger')
35+
->button()
36+
->close()
37+
->emit($deleteEvent),
38+
Action::make('cancel')
39+
->label(__('Cancel'))
40+
->close()
41+
->emit($cancelEvent)
42+
])
43+
->persistent()
44+
->send();
45+
}
46+
47+
}

app/Http/Livewire/Administration/CompaniesDialog.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Livewire\Administration;
44

5+
use App\Core\CrudDialogHelper;
56
use App\Models\CompanyUser;
67
use App\Models\Icon;
78
use App\Models\Company;
@@ -28,9 +29,9 @@
2829
class CompaniesDialog extends Component implements HasForms
2930
{
3031
use WithFileUploads, InteractsWithForms;
32+
use CrudDialogHelper;
3133

3234
public Company $company;
33-
public bool $deleteConfirmationOpened = false;
3435

3536
protected $listeners = ['doDeleteCompany', 'cancelDeleteCompany'];
3637

@@ -197,24 +198,11 @@ public function cancelDeleteCompany(): void
197198
*/
198199
public function deleteCompany(): void
199200
{
200-
$this->deleteConfirmationOpened = true;
201-
Notification::make()
202-
->warning()
203-
->title(__('Company deletion'))
204-
->body(__('Are you sure you want to delete this company?'))
205-
->actions([
206-
Action::make('confirm')
207-
->label(__('Confirm'))
208-
->color('danger')
209-
->button()
210-
->close()
211-
->emit('doDeleteCompany'),
212-
Action::make('cancel')
213-
->label(__('Cancel'))
214-
->close()
215-
->emit('cancelDeleteCompany')
216-
])
217-
->persistent()
218-
->send();
201+
$this->deleteConfirmation(
202+
__('Company deletion'),
203+
__('Are you sure you want to delete this company?'),
204+
'doDeleteCompany',
205+
'cancelDeleteCompany'
206+
);
219207
}
220208
}

app/Http/Livewire/Administration/TicketPrioritiesDialog.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Livewire\Administration;
44

5+
use App\Core\CrudDialogHelper;
56
use App\Models\Icon;
67
use App\Models\TicketPriority;
78
use Closure;
@@ -21,9 +22,9 @@
2122
class TicketPrioritiesDialog extends Component implements HasForms
2223
{
2324
use InteractsWithForms;
25+
use CrudDialogHelper;
2426

2527
public TicketPriority $priority;
26-
public bool $deleteConfirmationOpened = false;
2728

2829
protected $listeners = ['doDeletePriority', 'cancelDeletePriority'];
2930

@@ -181,24 +182,11 @@ public function cancelDeletePriority(): void
181182
*/
182183
public function deletePriority(): void
183184
{
184-
$this->deleteConfirmationOpened = true;
185-
Notification::make()
186-
->warning()
187-
->title(__('Priority deletion'))
188-
->body(__('Are you sure you want to delete this priority?'))
189-
->actions([
190-
Action::make('confirm')
191-
->label(__('Confirm'))
192-
->color('danger')
193-
->button()
194-
->close()
195-
->emit('doDeletePriority'),
196-
Action::make('cancel')
197-
->label(__('Cancel'))
198-
->close()
199-
->emit('cancelDeletePriority')
200-
])
201-
->persistent()
202-
->send();
185+
$this->deleteConfirmation(
186+
__('Priority deletion'),
187+
__('Are you sure you want to delete this priority?'),
188+
'doDeletePriority',
189+
'cancelDeletePriority'
190+
);
203191
}
204192
}

app/Http/Livewire/Administration/TicketStatusesDialog.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Livewire\Administration;
44

5+
use App\Core\CrudDialogHelper;
56
use App\Models\TicketStatus;
67
use Filament\Forms\Components\Checkbox;
78
use Filament\Forms\Components\ColorPicker;
@@ -17,9 +18,9 @@
1718
class TicketStatusesDialog extends Component implements HasForms
1819
{
1920
use InteractsWithForms;
21+
use CrudDialogHelper;
2022

2123
public TicketStatus $status;
22-
public bool $deleteConfirmationOpened = false;
2324

2425
protected $listeners = ['doDeleteStatus', 'cancelDeleteStatus'];
2526

@@ -155,24 +156,11 @@ public function cancelDeleteStatus(): void
155156
*/
156157
public function deleteStatus(): void
157158
{
158-
$this->deleteConfirmationOpened = true;
159-
Notification::make()
160-
->warning()
161-
->title(__('Status deletion'))
162-
->body(__('Are you sure you want to delete this status?'))
163-
->actions([
164-
Action::make('confirm')
165-
->label(__('Confirm'))
166-
->color('danger')
167-
->button()
168-
->close()
169-
->emit('doDeleteStatus'),
170-
Action::make('cancel')
171-
->label(__('Cancel'))
172-
->close()
173-
->emit('cancelDeleteStatus')
174-
])
175-
->persistent()
176-
->send();
159+
$this->deleteConfirmation(
160+
__('Status deletion'),
161+
__('Are you sure you want to delete this status?'),
162+
'doDeleteStatus',
163+
'cancelDeleteStatus'
164+
);
177165
}
178166
}

app/Http/Livewire/Administration/TicketTypesDialog.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Livewire\Administration;
44

5+
use App\Core\CrudDialogHelper;
56
use App\Models\Icon;
67
use App\Models\TicketType;
78
use Closure;
@@ -10,7 +11,6 @@
1011
use Filament\Forms\Components\TextInput;
1112
use Filament\Forms\Concerns\InteractsWithForms;
1213
use Filament\Forms\Contracts\HasForms;
13-
use Filament\Notifications\Actions\Action;
1414
use Filament\Notifications\Notification;
1515
use Illuminate\Support\HtmlString;
1616
use Illuminate\Support\Str;
@@ -20,9 +20,9 @@
2020
class TicketTypesDialog extends Component implements HasForms
2121
{
2222
use InteractsWithForms;
23+
use CrudDialogHelper;
2324

2425
public TicketType $type;
25-
public bool $deleteConfirmationOpened = false;
2626

2727
protected $listeners = ['doDeleteType', 'cancelDeleteType'];
2828

@@ -171,24 +171,11 @@ public function cancelDeleteType(): void
171171
*/
172172
public function deleteType(): void
173173
{
174-
$this->deleteConfirmationOpened = true;
175-
Notification::make()
176-
->warning()
177-
->title(__('Type deletion'))
178-
->body(__('Are you sure you want to delete this type?'))
179-
->actions([
180-
Action::make('confirm')
181-
->label(__('Confirm'))
182-
->color('danger')
183-
->button()
184-
->close()
185-
->emit('doDeleteType'),
186-
Action::make('cancel')
187-
->label(__('Cancel'))
188-
->close()
189-
->emit('cancelDeleteType')
190-
])
191-
->persistent()
192-
->send();
174+
$this->deleteConfirmation(
175+
__('Type deletion'),
176+
__('Are you sure you want to delete this type?'),
177+
'doDeleteType',
178+
'cancelDeleteType'
179+
);
193180
}
194181
}

app/Http/Livewire/Administration/UsersDialog.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Livewire\Administration;
44

5+
use App\Core\CrudDialogHelper;
56
use App\Models\CompanyUser;
67
use App\Models\User;
78
use App\Notifications\UserCreatedNotification;
@@ -23,9 +24,9 @@
2324
class UsersDialog extends Component implements HasForms
2425
{
2526
use InteractsWithForms;
27+
use CrudDialogHelper;
2628

2729
public User $user;
28-
public bool $deleteConfirmationOpened = false;
2930

3031
protected $listeners = ['doDeleteUser', 'cancelDeleteUser'];
3132

@@ -260,24 +261,11 @@ public function cancelDeleteUser(): void
260261
*/
261262
public function deleteUser(): void
262263
{
263-
$this->deleteConfirmationOpened = true;
264-
Notification::make()
265-
->warning()
266-
->title(__('User deletion'))
267-
->body(__('Are you sure you want to delete this user?'))
268-
->actions([
269-
Action::make('confirm')
270-
->label(__('Confirm'))
271-
->color('danger')
272-
->button()
273-
->close()
274-
->emit('doDeleteUser'),
275-
Action::make('cancel')
276-
->label(__('Cancel'))
277-
->close()
278-
->emit('cancelDeleteUser')
279-
])
280-
->persistent()
281-
->send();
264+
$this->deleteConfirmation(
265+
__('User deletion'),
266+
__('Are you sure you want to delete this user?'),
267+
'doDeleteUser',
268+
'cancelDeleteUser'
269+
);
282270
}
283271
}

app/Http/Livewire/ProjectsDialog.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Livewire;
44

5+
use App\Core\CrudDialogHelper;
56
use App\Models\Project;
67
use App\Models\User;
78
use App\Notifications\ProjectCreatedNotification;
@@ -18,9 +19,9 @@
1819
class ProjectsDialog extends Component implements HasForms
1920
{
2021
use InteractsWithForms;
22+
use CrudDialogHelper;
2123

2224
public Project $project;
23-
public bool $deleteConfirmationOpened = false;
2425

2526
protected $listeners = ['doDeleteProject', 'cancelDeleteProject'];
2627

@@ -148,24 +149,11 @@ public function cancelDeleteProject(): void
148149
*/
149150
public function deleteProject(): void
150151
{
151-
$this->deleteConfirmationOpened = true;
152-
Notification::make()
153-
->warning()
154-
->title(__('Project deletion'))
155-
->body(__('Are you sure you want to delete this project?'))
156-
->actions([
157-
Action::make('confirm')
158-
->label(__('Confirm'))
159-
->color('danger')
160-
->button()
161-
->close()
162-
->emit('doDeleteProject'),
163-
Action::make('cancel')
164-
->label(__('Cancel'))
165-
->close()
166-
->emit('cancelDeleteProject')
167-
])
168-
->persistent()
169-
->send();
152+
$this->deleteConfirmation(
153+
__('Project deletion'),
154+
__('Are you sure you want to delete this project?'),
155+
'doDeleteProject',
156+
'cancelDeleteProject'
157+
);
170158
}
171159
}

resources/views/livewire/administration/companies-dialog.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class="rounded-lg flex flex-row justify-center items-center text-center gap-2
1818
@if($company->id && auth()->user()->can('Delete companies'))
1919
<button type="button"
2020
wire:loading.attr="disabled" {!! $deleteConfirmationOpened ? 'disabled' : '' !!}
21-
wire:click="deletePriority"
21+
wire:click="deleteCompany"
2222
class="rounded-lg flex flex-row justify-center items-center text-center text-white bg-red-700
2323
disabled:bg-red-400 bg-opacity-90 hover:bg-opacity-100 shadow hover:shadow-lg
2424
disabled:hover:shadow px-10 py-3 text-sm mt-5"

0 commit comments

Comments
 (0)