tracks form changes and warns users before navigation
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
BackButtonUrl="/events" />
|
||||
|
||||
<EditForm id="create-event-form" Model="EventDefinition" OnValidSubmit="OnValidSubmit" Enhance>
|
||||
<FormChangeTracker @ref="_formChangeTracker" />
|
||||
<AntiforgeryToken />
|
||||
<DataAnnotationsValidator />
|
||||
<MudGrid>
|
||||
@@ -48,17 +49,27 @@
|
||||
|
||||
<FormActions>
|
||||
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Form="create-event-form">Create</MudButton>
|
||||
<MudButton Href="/events" Variant="Variant.Text">Cancel</MudButton>
|
||||
<MudButton OnClick="HandleCancel" Variant="Variant.Text">Cancel</MudButton>
|
||||
</FormActions>
|
||||
|
||||
@code {
|
||||
[SupplyParameterFromForm]
|
||||
private EventDefinition EventDefinition { get; set; } = new();
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
|
||||
private void OnValidSubmit()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
|
||||
context.Events.Add(EventDefinition);
|
||||
context.SaveChanges();
|
||||
NavigationManager.NavigateTo("/events");
|
||||
}
|
||||
|
||||
private void HandleCancel()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo("/events");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
BackButtonUrl="/events" />
|
||||
|
||||
<EditForm id="edit-event-form" Model="EventDefinition" OnValidSubmit="OnValidSubmit" Enhance>
|
||||
<FormChangeTracker @ref="_formChangeTracker" />
|
||||
<AntiforgeryToken />
|
||||
<DataAnnotationsValidator />
|
||||
<MudGrid>
|
||||
@@ -55,7 +56,7 @@
|
||||
|
||||
<FormActions>
|
||||
<MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Form="edit-event-form">Save</MudButton>
|
||||
<MudButton Href="/events" Variant="Variant.Text">Cancel</MudButton>
|
||||
<MudButton OnClick="HandleCancel" Variant="Variant.Text">Cancel</MudButton>
|
||||
</FormActions>
|
||||
|
||||
@code {
|
||||
@@ -65,6 +66,8 @@
|
||||
[SupplyParameterFromForm]
|
||||
private EventDefinition? EventDefinition { get; set; }
|
||||
|
||||
private FormChangeTracker? _formChangeTracker;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
EventDefinition ??= await context.Events.FirstOrDefaultAsync(m => m.Id == Id);
|
||||
@@ -84,6 +87,8 @@
|
||||
try
|
||||
{
|
||||
context.SaveChangesAsync();
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo("/events");
|
||||
}
|
||||
catch (DbUpdateConcurrencyException)
|
||||
{
|
||||
@@ -96,7 +101,11 @@
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCancel()
|
||||
{
|
||||
_formChangeTracker?.AllowNavigation();
|
||||
NavigationManager.NavigateTo("/events");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user