Skip to content

Commit

Permalink
Update unsaved-data-changes-alert.blade.php
Browse files Browse the repository at this point in the history
  • Loading branch information
danharrin committed Apr 18, 2024
1 parent f901196 commit 4b8ddda
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docs/02-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function form(Form $form): Form

##### Casting the price to an integer

Filament stores currency values as integers (not floats) to avoid rounding and precision issues — a widely-accepted approach in the Laravel community. However, this requires creating a cast in Laravel that transforms the float into an integer when retrieved and back to an integer when stored in the database. Use the following artisan command to create the cast:
Filament stores currency values as integers (not floats) to avoid rounding and precision issues — a widely-accepted approach in the Laravel community. However, this requires creating a cast in Laravel that transforms the integer into a float when retrieved and back to an integer when stored in the database. Use the following artisan command to create the cast:

```bash
php artisan make:cast MoneyCast
Expand Down
2 changes: 0 additions & 2 deletions docs/09-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ public function panel(Panel $panel): Panel
}
```

> Please note: this feature is not compatible with [SPA mode](#spa-mode).
## Enabling database transactions

By default, Filament does not wrap operations in database transactions, and allows the user to enable this themselves when they have tested to ensure that their operations are safe to be wrapped in a transaction. However, you can enable database transactions at once for all operations by using the `databaseTransactions()` method:
Expand Down
7 changes: 7 additions & 0 deletions resources/lang/en/unsaved-changes-alert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [

'body' => 'You have unsaved changes. Are you sure you want to leave this page?',

];
8 changes: 8 additions & 0 deletions resources/lang/lt/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@

],

'avatar' => [
'alt' => ':name avataras',
],

'logo' => [
'alt' => ':name logotipas',
],

];
25 changes: 25 additions & 0 deletions resources/lang/lt/pages/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,29 @@

'title' => 'Pagrindinis puslapis',

'actions' => [

'filter' => [

'label' => 'Filtras',

'modal' => [

'heading' => 'Filtras',

'actions' => [

'apply' => [

'label' => 'Taikyti',

],

],

],

],

],
];
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,75 @@
use Filament\Support\Facades\FilamentView;
@endphp

@if ($this->hasUnsavedDataChangesAlert() && (! FilamentView::hasSpaMode()))
@script
<script>
window.addEventListener('beforeunload', (event) => {
if (
window.jsMd5(
JSON.stringify($wire.data).replace(/\\/g, ''),
) === $wire.savedDataHash ||
$wire?.__instance?.effects?.redirect
) {
return
@if ($this->hasUnsavedDataChangesAlert())
@if (FilamentView::hasSpaMode())
@script
<script>
let formSubmitted = false
document.addEventListener(
'submit',
() => (formSubmitted = true),
)
shouldPreventNavigation = () => {
if (formSubmitted) {
return
}
return (
window.jsMd5(
JSON.stringify($wire.data).replace(/\\/g, ''),
) !== $wire.savedDataHash ||
$wire?.__instance?.effects?.redirect
)
}
const showUnsavedChangesAlert = () => {
return confirm(@js(__('filament-panels::unsaved-changes-alert.body')))
}
event.preventDefault()
event.returnValue = true
})
</script>
@endscript
document.addEventListener('livewire:navigate', (event) => {
if (typeof @this !== 'undefined') {
if (!shouldPreventNavigation()) {
return
}
if (showUnsavedChangesAlert()) {
return
}
event.preventDefault()
}
})
window.addEventListener('beforeunload', (event) => {
if (!shouldPreventNavigation()) {
return
}
event.preventDefault()
event.returnValue = true
})
</script>
@endscript
@else
@script
<script>
window.addEventListener('beforeunload', (event) => {
if (
window.jsMd5(
JSON.stringify($wire.data).replace(/\\/g, ''),
) === $wire.savedDataHash ||
$wire?.__instance?.effects?.redirect
) {
return
}
event.preventDefault()
event.returnValue = true
})
</script>
@endscript
@endif
@endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
@script
<script>
window.addEventListener('beforeunload', (event) => {
if (typeof @this === 'undefined') {
return
}
if (
[
...(@js($this instanceof \Filament\Actions\Contracts\HasActions) ? $wire.mountedActions ?? [] : []),
Expand Down
8 changes: 4 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
->name("{$panelId}.")
->prefix($panel->getPath())
->group(function () use ($panel, $hasTenancy, $tenantDomain, $tenantRoutePrefix, $tenantSlugAttribute) {
if ($routes = $panel->getRoutes()) {
foreach ($panel->getRoutes() as $routes) {
$routes($panel);
}

Expand Down Expand Up @@ -55,7 +55,7 @@

Route::middleware($panel->getAuthMiddleware())
->group(function () use ($panel, $hasTenancy, $tenantDomain, $tenantRoutePrefix, $tenantSlugAttribute): void {
if ($routes = $panel->getAuthenticatedRoutes()) {
foreach ($panel->getAuthenticatedRoutes() as $routes) {
$routes($panel);
}

Expand Down Expand Up @@ -109,7 +109,7 @@

$routeGroup
->group(function () use ($panel): void {
if ($routes = $panel->getAuthenticatedTenantRoutes()) {
foreach ($panel->getAuthenticatedTenantRoutes() as $routes) {
$routes($panel);
}

Expand Down Expand Up @@ -161,7 +161,7 @@

$routeGroup
->group(function () use ($panel): void {
if ($routes = $panel->getTenantRoutes()) {
foreach ($panel->getTenantRoutes() as $routes) {
$routes($panel);
}
});
Expand Down
48 changes: 36 additions & 12 deletions src/Panel/Concerns/HasRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,25 @@

trait HasRoutes
{
protected Closure | Native | null $routes = null;
/**
* @var array<Closure | Native>
*/
protected array $routes = [];

protected Closure | Native | null $authenticatedRoutes = null;
/**
* @var array<Closure | Native>
*/
protected array $authenticatedRoutes = [];

protected Closure | Native | null $tenantRoutes = null;
/**
* @var array<Closure | Native>
*/
protected array $tenantRoutes = [];

protected Closure | Native | null $authenticatedTenantRoutes = null;
/**
* @var array<Closure | Native>
*/
protected array $authenticatedTenantRoutes = [];

protected string | Closure | null $homeUrl = null;

Expand Down Expand Up @@ -61,28 +73,28 @@ public function homeUrl(string | Closure | null $url): static

public function routes(?Closure $routes): static
{
$this->routes = $routes;
$this->routes[] = $routes;

return $this;
}

public function authenticatedRoutes(?Closure $routes): static
{
$this->authenticatedRoutes = $routes;
$this->authenticatedRoutes[] = $routes;

return $this;
}

public function tenantRoutes(?Closure $routes): static
{
$this->tenantRoutes = $routes;
$this->tenantRoutes[] = $routes;

return $this;
}

public function authenticatedTenantRoutes(?Closure $routes): static
{
$this->authenticatedTenantRoutes = $routes;
$this->authenticatedTenantRoutes[] = $routes;

return $this;
}
Expand All @@ -97,22 +109,34 @@ public function generateRouteName(string $name): string
return "filament.{$this->getId()}.{$name}";
}

public function getRoutes(): ?Closure
/**
* @return array<Closure | Native>
*/
public function getRoutes(): array
{
return $this->routes;
}

public function getAuthenticatedRoutes(): ?Closure
/**
* @return array<Closure | Native>
*/
public function getAuthenticatedRoutes(): array
{
return $this->authenticatedRoutes;
}

public function getTenantRoutes(): ?Closure
/**
* @return array<Closure | Native>
*/
public function getTenantRoutes(): array
{
return $this->tenantRoutes;
}

public function getAuthenticatedTenantRoutes(): ?Closure
/**
* @return array<Closure | Native>
*/
public function getAuthenticatedTenantRoutes(): array
{
return $this->authenticatedTenantRoutes;
}
Expand Down
9 changes: 1 addition & 8 deletions src/Panel/Concerns/HasUnsavedChangesAlerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Filament\Panel\Concerns;

use Closure;
use Exception;

trait HasUnsavedChangesAlerts
{
Expand All @@ -18,12 +17,6 @@ public function unsavedChangesAlerts(bool | Closure $condition = true): static

public function hasUnsavedChangesAlerts(): bool
{
$hasAlerts = (bool) $this->evaluate($this->hasUnsavedChangesAlerts);

if ($hasAlerts && $this->hasSpaMode()) {
throw new Exception('Unsaved changes alerts are not supported in SPA mode.');
}

return $hasAlerts;
return (bool) $this->evaluate($this->hasUnsavedChangesAlerts);
}
}

0 comments on commit 4b8ddda

Please sign in to comment.