Skip to content

Commit 646a668

Browse files
committed
refactor: Create a new trait for using the alerts
I created a new Trait for using the alers outside the actions.
1 parent fb1ed86 commit 646a668

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/Actions/Action.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use LaravelViews\Views\View;
66
use Illuminate\Support\Str;
7+
use LaravelViews\Views\Traits\WithAlerts;
78

89
abstract class Action
910
{
11+
use WithAlerts;
12+
1013
/** @var String $title Title of the action */
1114
public $title;
1215

@@ -44,29 +47,6 @@ public function renderIf($item, View $view)
4447
return true;
4548
}
4649

47-
public function success($message = null)
48-
{
49-
$this->setMessage('success', $message);
50-
}
51-
52-
public function error($message = null)
53-
{
54-
$this->setMessage('danger', $message);
55-
}
56-
57-
private function setMessage($type = 'success', $message = null)
58-
{
59-
$messages = [
60-
'success' => __('Action was executed successfully'),
61-
'danger' => __('There was an error executing this action'),
62-
];
63-
64-
$this->view->emitSelf('notify', [
65-
'message' => $message ? $message : $messages[$type],
66-
'type' => $type
67-
]);
68-
}
69-
7050
public function shouldBeConfirmed()
7151
{
7252
return method_exists($this, 'getConfirmationMessage');

src/Views/Traits/WithAlerts.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace LaravelViews\Views\Traits;
4+
5+
trait WithAlerts
6+
{
7+
public function success($message = null)
8+
{
9+
$this->setMessage('success', $message);
10+
}
11+
12+
public function error($message = null)
13+
{
14+
$this->setMessage('danger', $message);
15+
}
16+
17+
private function setMessage($type = 'success', $message = null)
18+
{
19+
$view = $this->view ? $this->view : $view;
20+
21+
$messages = [
22+
'success' => __('Action was executed successfully'),
23+
'danger' => __('There was an error executing this action'),
24+
];
25+
26+
$view->emitSelf('notify', [
27+
'message' => $message ? $message : $messages[$type],
28+
'type' => $type
29+
]);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)