Refactor interaction and user deletion logic
Consolidated repeated methods for interaction/user deletion into reusable components to improve maintainability. Introduced a `ConfirmDialog` for consistent confirmation UI and streamlined associated logic across pages. Removed redundant methods and enhanced admin-specific page security checks.
This commit is contained in:
@@ -81,31 +81,5 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RemoveInteractionAsync
|
||||
|
||||
/// <summary>
|
||||
/// Removes the interaction using the specified arg (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="arg">The arg</param>
|
||||
private async Task RemoveInteractionAsync(Guid arg)
|
||||
{
|
||||
var confirm = await Message.Confirm("Interaktion wirklich löschen?", "Bestätigen", o =>
|
||||
{
|
||||
o.ConfirmButtonText = "Ja, wirklich!";
|
||||
o.CancelButtonText = "Abbrechen";
|
||||
o.ShowMessageIcon = false;
|
||||
});
|
||||
|
||||
if (confirm)
|
||||
{
|
||||
await ProspectService.RemoveInteraction(arg);
|
||||
await LoadProspects();
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,9 @@
|
||||
@using FoodsharingSiegen.Shared.Helper
|
||||
@inherits FsBase
|
||||
|
||||
<PageTitle>Freischalten - @AppSettings.Terms.Title</PageTitle>
|
||||
<PageTitle>Alle (Admin) - @AppSettings.Terms.Title</PageTitle>
|
||||
|
||||
<h2>Freischalten</h2>
|
||||
<h2>Alle (Admin)</h2>
|
||||
|
||||
@{
|
||||
var filterList = ProspectList.ApplyFilter(Filter);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using FoodsharingSiegen.Contracts.Entity;
|
||||
using FoodsharingSiegen.Contracts.Helper;
|
||||
using FoodsharingSiegen.Contracts.Model;
|
||||
using FoodsharingSiegen.Server.Data.Service;
|
||||
using FoodsharingSiegen.Server.Dialogs;
|
||||
@@ -43,6 +44,8 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
/// <inheritdoc />
|
||||
protected override async Task InitializeDataAsync()
|
||||
{
|
||||
if(!CurrentUser.IsAdmin()) NavigationManager.NavigateTo("/");
|
||||
|
||||
// Load prospects
|
||||
await LoadProspects();
|
||||
}
|
||||
@@ -64,45 +67,5 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method OnUpdateProspect
|
||||
|
||||
/// <summary>
|
||||
/// Ons the update prospect using the specified prospect (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="prospect">The prospect</param>
|
||||
private async Task OnUpdateProspect(Prospect prospect)
|
||||
{
|
||||
var updateProspectR = await ProspectService.UpdateAsync(prospect);
|
||||
if (updateProspectR.Success) await LoadProspects();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RemoveInteraction
|
||||
|
||||
/// <summary>
|
||||
/// Removes the interaction using the specified arg (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="arg">The arg</param>
|
||||
private async Task RemoveInteraction(Guid arg)
|
||||
{
|
||||
var confirm = await Message.Confirm("Interaktion wirklich löschen?", "Bestätigen", o =>
|
||||
{
|
||||
o.ConfirmButtonText = "Ja, wirklich!";
|
||||
o.CancelButtonText = "Abbrechen";
|
||||
o.ShowMessageIcon = false;
|
||||
});
|
||||
|
||||
if (confirm)
|
||||
{
|
||||
await ProspectService.RemoveInteraction(arg);
|
||||
await LoadProspects();
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -56,31 +56,5 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RemoveInteraction
|
||||
|
||||
/// <summary>
|
||||
/// Removes the interaction using the specified arg (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="arg">The arg</param>
|
||||
private async Task RemoveInteraction(Guid arg)
|
||||
{
|
||||
var confirm = await Message.Confirm("Interaktion wirklich löschen?", "Bestätigen", o =>
|
||||
{
|
||||
o.ConfirmButtonText = "Ja, wirklich!";
|
||||
o.CancelButtonText = "Abbrechen";
|
||||
o.ShowMessageIcon = false;
|
||||
});
|
||||
|
||||
if (confirm)
|
||||
{
|
||||
await ProspectService.RemoveInteraction(arg);
|
||||
await LoadProspects();
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -68,45 +68,5 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method OnUpdateProspect
|
||||
|
||||
/// <summary>
|
||||
/// Ons the update prospect using the specified prospect (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="prospect">The prospect</param>
|
||||
private async Task OnUpdateProspect(Prospect prospect)
|
||||
{
|
||||
var updateProspectR = await ProspectService.UpdateAsync(prospect);
|
||||
if (updateProspectR.Success) await LoadProspects();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RemoveInteraction
|
||||
|
||||
/// <summary>
|
||||
/// Removes the interaction using the specified arg (a. beging, 11.04.2022)
|
||||
/// </summary>
|
||||
/// <param name="arg">The arg</param>
|
||||
private async Task RemoveInteraction(Guid arg)
|
||||
{
|
||||
var confirm = await Message.Confirm("Interaktion wirklich löschen?", "Bestätigen", o =>
|
||||
{
|
||||
o.ConfirmButtonText = "Ja, wirklich!";
|
||||
o.CancelButtonText = "Abbrechen";
|
||||
o.ShowMessageIcon = false;
|
||||
});
|
||||
|
||||
if (confirm)
|
||||
{
|
||||
await ProspectService.RemoveInteraction(arg);
|
||||
await LoadProspects();
|
||||
}
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -38,10 +38,7 @@
|
||||
PopupTitleTemplate="PopupTitleTemplate"
|
||||
RowInserted="RowInserted"
|
||||
RowUpdated="RowUpdated"
|
||||
RowRemoving="RowRemoving"
|
||||
RowRemoved="RowRemoved"
|
||||
PageSize="50"
|
||||
|
||||
@bind-SelectedRow="SelectedUser"
|
||||
RowDoubleClicked="arg => UserDataGrid?.Edit(arg.Item)!"
|
||||
Editable
|
||||
@@ -59,7 +56,7 @@
|
||||
</Button>
|
||||
</EditCommandTemplate>
|
||||
<DeleteCommandTemplate>
|
||||
<Button Size="Size.ExtraSmall" Color="Color.Danger" Clicked="@context.Clicked" Class="mr-1" Style="min-width: auto;">
|
||||
<Button Size="Size.ExtraSmall" Color="Color.Danger" Clicked="() => RemoveUserAsync(context.Item)" Class="mr-1" Style="min-width: auto;">
|
||||
<i class="oi oi-trash"></i>
|
||||
</Button>
|
||||
</DeleteCommandTemplate>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using Blazorise;
|
||||
using Blazorise.DataGrid;
|
||||
using FoodsharingSiegen.Contracts.Entity;
|
||||
using FoodsharingSiegen.Contracts.Helper;
|
||||
@@ -63,6 +62,7 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
/// <inheritdoc />
|
||||
protected override async Task InitializeDataAsync()
|
||||
{
|
||||
if (!CurrentUser.IsAdmin()) NavigationManager.NavigateTo("/");
|
||||
await LoadUsers();
|
||||
}
|
||||
|
||||
@@ -98,6 +98,33 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RemoveUserAsync
|
||||
|
||||
/// <summary>
|
||||
/// Removes the specified user if they are not an admin, after confirming the action with a dialog.
|
||||
/// </summary>
|
||||
/// <param name="user">The user to be removed.</param>
|
||||
/// <returns>A task that represents the asynchronous remove operation.</returns>
|
||||
private async Task RemoveUserAsync(User user)
|
||||
{
|
||||
if (user.IsAdmin())
|
||||
{
|
||||
await Notification.Error("Admins können nicht gelöscht werden!");
|
||||
return;
|
||||
}
|
||||
|
||||
await ConfirmDialog.ShowAsync(ModalService, "Bestätigen", $"User {user.Mail} löschen?", async () =>
|
||||
{
|
||||
var removeR = await UserService.RemoveAsync(user.Id);
|
||||
if (!removeR.Success)
|
||||
await Notification.Error($"Löschen: {removeR.ErrorMessage}")!;
|
||||
else
|
||||
await LoadUsers();
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RowInserted
|
||||
|
||||
/// <summary>
|
||||
@@ -115,51 +142,6 @@ namespace FoodsharingSiegen.Server.Pages
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RowRemoved
|
||||
|
||||
/// <summary>
|
||||
/// Rows the removed using the specified arg (a. beging, 08.02.2023)
|
||||
/// </summary>
|
||||
/// <param name="arg">The arg</param>
|
||||
private async Task RowRemoved(User arg)
|
||||
{
|
||||
var removeR = await UserService.RemoveAsync(arg.Id);
|
||||
if (!removeR.Success)
|
||||
await Notification.Error($"Löschen: {removeR.ErrorMessage}")!;
|
||||
else
|
||||
await LoadUsers();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RowRemoving
|
||||
|
||||
/// <summary>
|
||||
/// Rows the removing using the specified arg (a. beging, 08.02.2023)
|
||||
/// </summary>
|
||||
/// <param name="arg">The arg</param>
|
||||
private async Task RowRemoving(CancellableRowChange<User> arg)
|
||||
{
|
||||
if (arg.Item.IsAdmin())
|
||||
{
|
||||
await Notification.Error("Admins können nicht gelöscht werden!");
|
||||
arg.Cancel = true;
|
||||
return;
|
||||
}
|
||||
|
||||
var confirm = await Message.Confirm($"User {arg.Item.Mail} löschen?", "Bestätigen", o =>
|
||||
{
|
||||
o.ConfirmButtonText = "Löschen";
|
||||
o.CancelButtonText = "Abbrechen";
|
||||
o.ShowMessageIcon = false;
|
||||
o.ConfirmButtonColor = Color.Danger;
|
||||
});
|
||||
|
||||
arg.Cancel = !confirm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method RowUpdated
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user