Skip to content

Commit 4223edd

Browse files
committed
chore: coding style
1 parent 0ecf890 commit 4223edd

File tree

3 files changed

+65
-65
lines changed

3 files changed

+65
-65
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use SweetAlert2\Laravel\Traits\WithSweetAlert;
9090
class LivewireExample extends Component
9191
{
9292
use WithSweetAlert;
93-
93+
9494
public function save(): void
9595
{
9696
// same as `Swal.fire()` in JS, same options: https://sweetalert2.github.io/#configuration
@@ -109,7 +109,7 @@ The full list of options can be found in the [SweetAlert2 documentation](https:/
109109
#### livewire-example.blade.php
110110
```html
111111
<div>
112-
<button type="button" wire:click="save">Save</button>
112+
<button type="button" wire:click="save">Save</button>
113113
</div>
114114
```
115115
@@ -180,28 +180,32 @@ Depending on whether you use the Swal class or the WithSweetAlert trait, within
180180
### Livewire components
181181
182182
#### Realtime
183+
183184
1. The `$this->swalFire()` method will dispatch a [Livewire event](https://livewire.laravel.com/docs/events) with the options to the browser window within the current request.
184185
2. The blade partial template will listen for the Livewire event and will render the SweetAlert2 popup.
185186
186187
This works on the first request and subsequent Livewire update requests.
188+
187189
#### First request or after redirect
190+
188191
1. The `Swal::fire()` method will pass the options to the [flashed session](https://laravel.com/docs/12.x/session#flash-data).
189192
2. The blade partial template will show a popup on the first request.
190193
191194
This works only on the first request, not on Livewire update requests.
192195
193196
This is ideal for showing messages after redirecting the user from a Livewire component, for example if they lack permissions to view a page:
197+
194198
```php
195199
public function boot()
196200
{
197-
if (Gate::denies('viewAny', Appointments::class)) {
198-
Swal::error([
199-
'title' => 'Unauthorized',
200-
'text' => 'You aren\'t authorized to view appointments!',
201-
'confirmButtonText' => 'Close'
202-
]);
203-
return redirect()->route('index');
204-
}
201+
if (Gate::denies('viewAny', Appointments::class)) {
202+
Swal::error([
203+
'title' => 'Unauthorized',
204+
'text' => 'You aren\'t authorized to view appointments!',
205+
'confirmButtonText' => 'Close'
206+
]);
207+
return redirect()->route('index');
208+
}
205209
}
206210
```
207211

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
@php use SweetAlert2\Laravel\Swal; @endphp
1+
@use('SweetAlert2\Laravel\Swal')
22
@if(session()->has(Swal::SESSION_KEY))
3-
<script type="module">
4-
let Swal;
5-
const getSweetAlert2 = async () => {
6-
// If SweetAlert2 is already loaded, use it
7-
if (window.Swal) {
8-
return window.Swal;
9-
}
3+
<script type="module">
4+
let Swal;
5+
const getSweetAlert2 = async () => {
6+
// If SweetAlert2 is already loaded, use it
7+
if (window.Swal) {
8+
return window.Swal;
9+
}
1010
11-
// Otherwise, dynamically import it from CDN
12-
try {
13-
return (await import('https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.esm.all.min.js')).default;
14-
}
11+
// Otherwise, dynamically import it from CDN
12+
try {
13+
return (await import('https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.esm.all.min.js')).default;
14+
}
1515
16-
// Fallback in case of an error (e.g., network issues)
17-
catch (error) {
18-
console.error('Failed to load SweetAlert2:', error);
19-
return {
20-
fire: () => {
21-
}
22-
};
23-
}
24-
};
25-
(async () => {
26-
Swal = await getSweetAlert2();
27-
Swal.fire(@json(session(Swal::SESSION_KEY)));
28-
})();
29-
</script>
16+
// Fallback in case of an error (e.g., network issues)
17+
catch (error) {
18+
console.error('Failed to load SweetAlert2:', error);
19+
return { fire: () => {} };
20+
}
21+
};
22+
23+
(async () => {
24+
Swal = await getSweetAlert2();
25+
Swal.fire(@json(session(Swal::SESSION_KEY)));
26+
})();
27+
</script>
3028
@endif
3129

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
@php use SweetAlert2\Laravel\Swal; @endphp
1+
@use('SweetAlert2\Laravel\Swal')
22
<script type="module">
3-
let Swal;
4-
const getSweetAlert2 = async () => {
5-
// If SweetAlert2 is already loaded, use it
6-
if (window.Swal) {
7-
return window.Swal;
8-
}
3+
let Swal;
4+
const getSweetAlert2 = async () => {
5+
// If SweetAlert2 is already loaded, use it
6+
if (window.Swal) {
7+
return window.Swal;
8+
}
99
10-
// Otherwise, dynamically import it from CDN
11-
try {
12-
return (await import('https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.esm.all.min.js')).default;
13-
}
10+
// Otherwise, dynamically import it from CDN
11+
try {
12+
return (await import('https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.esm.all.min.js')).default;
13+
}
1414
15-
// Fallback in case of an error (e.g., network issues)
16-
catch (error) {
17-
console.error('Failed to load SweetAlert2:', error);
18-
return {
19-
fire: () => {
20-
}
21-
};
22-
}
23-
};
24-
@if(session()->has(Swal::SESSION_KEY))
15+
// Fallback in case of an error (e.g., network issues)
16+
catch (error) {
17+
console.error('Failed to load SweetAlert2:', error);
18+
return { fire: () => {} };
19+
}
20+
};
21+
22+
@if(session()->has(Swal::SESSION_KEY))
2523
(async () => {
2624
Swal = await getSweetAlert2();
2725
Swal.fire(@json(session(Swal::SESSION_KEY)));
2826
})();
29-
@endif
27+
@endif
3028
31-
window.addEventListener('{{ Swal::SESSION_KEY }}', async (event) => {
32-
if (!event.detail || typeof event.detail !== 'object') {
33-
return;
34-
}
35-
Swal = Swal || await getSweetAlert2();
36-
Swal.fire(event.detail);
37-
});
29+
window.addEventListener('{{ Swal::SESSION_KEY }}', async (event) => {
30+
if (!event.detail || typeof event.detail !== 'object') {
31+
return;
32+
}
33+
Swal = Swal || await getSweetAlert2();
34+
Swal.fire(event.detail);
35+
});
3836
</script>
3937

0 commit comments

Comments
 (0)