Skip to content

Commit

Permalink
feat: prevent navigation on entity changed
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Sep 16, 2022
1 parent 2c9be23 commit 0d49125
Showing 1 changed file with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
namespace Aguacongas.TheIdServer.BlazorApp.Pages
{
[Authorize(Policy = SharedConstants.READERPOLICY)]
public abstract class EntityModel<T> : ComponentBase, IComparer<Type> where T : class, ICloneable<T>, new()
public abstract class EntityModel<T> : ComponentBase, IDisposable, IComparer<Type> where T : class, ICloneable<T>, new()
{
const int HEADER_HEIGHT = 95;
private IDisposable _registration;
private bool disposedValue;

[Inject]
protected Notifier Notifier { get; set; }
Expand Down Expand Up @@ -101,7 +103,24 @@ protected override async Task OnInitializedAsync()

var model = await GetModelAsync()
.ConfigureAwait(false);

CreateEditContext(model);

_registration ??= NavigationManager.RegisterLocationChangingHandler(async context =>
{
if (!EditContext.IsModified())
{
return;
}
var isConfirmed = await JSRuntime.InvokeAsync<bool>("window.confirm", Localizer["Are you sure you want to leave this page?"]?.ToString())
.ConfigureAwait(false);
if (!isConfirmed)
{
context.PreventNavigation();
}
});
}

protected async Task HandleValidSubmit()
Expand Down Expand Up @@ -404,5 +423,25 @@ private Task<object> StoreAsync(Type entityType, object entity, Func<IAdminStore
var store = GetStore(entityType);
return action.Invoke(store, entity);
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_registration?.Dispose();
}

disposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

0 comments on commit 0d49125

Please sign in to comment.