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:
Andre Beging
2025-03-28 23:55:12 +01:00
parent 83257d1d2a
commit 027a36ce17
12 changed files with 174 additions and 234 deletions

View File

@@ -1,4 +1,4 @@
<Blazorise.ThemeProvider Theme="@theme"> <Blazorise.ThemeProvider Theme="@_theme">
<Router AppAssembly="@typeof(App).Assembly"> <Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData"> <Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(DefaultLayout)"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(DefaultLayout)">
@@ -13,15 +13,14 @@
</NotFound> </NotFound>
</Router> </Router>
<MessageProvider />
<ModalProvider Centered="true" /> <ModalProvider Centered="true" />
</Blazorise.ThemeProvider> </Blazorise.ThemeProvider>
@code{ @code{
private Theme theme = new() private readonly Theme _theme = new()
{ {
ColorOptions = new ThemeColorOptions ColorOptions = new()
{ {
Primary = "#64ae24", Primary = "#64ae24",
Secondary = "#533a20", Secondary = "#533a20",

View File

@@ -31,12 +31,6 @@ namespace FoodsharingSiegen.Server.BaseClasses
[Inject] [Inject]
protected AuthService AuthService { get; set; } = null!; protected AuthService AuthService { get; set; } = null!;
/// <summary>
/// Gets or sets the value of the message (ab)
/// </summary>
[Inject]
protected IMessageService Message { get; set; } = null!;
/// <summary> /// <summary>
/// Gets or sets the modal service for handling modals within the application /// Gets or sets the modal service for handling modals within the application
/// </summary> /// </summary>

View File

@@ -8,14 +8,33 @@ namespace FoodsharingSiegen.Server.Controls
{ {
public partial class ProspectContainer public partial class ProspectContainer
{ {
[Parameter] public Prospect? Prospect { get; set; } #region Dependencies
[Parameter] public ProspectStateFilter StateFilter { get; set; } /// <summary>
/// Gets or sets the value of the prospect service (ab)
/// </summary>
[Inject]
public ProspectService ProspectService { get; set; } = null!;
[Parameter] public string? CssClass { get; set; } #endregion
[Parameter] public Func<Task>? OnDataChanged { get; set; } #region Parameters
[Parameter]
public string? CssClass { get; set; }
[Parameter]
public Func<Task>? OnDataChanged { get; set; }
[Parameter]
public Prospect? Prospect { get; set; }
[Parameter]
public ProspectStateFilter StateFilter { get; set; }
#endregion
#region Private Method AddInteraction
private async Task AddInteraction(InteractionType type) private async Task AddInteraction(InteractionType type)
{ {
@@ -25,42 +44,47 @@ namespace FoodsharingSiegen.Server.Controls
await InteractionDialog.ShowAsync(ModalService, new(type, Prospect.Id, headerText, OnDataChanged)); await InteractionDialog.ShowAsync(ModalService, new(type, Prospect.Id, headerText, OnDataChanged));
} }
} }
private List<Interaction> GetTyped(InteractionType type) #endregion
{
return Prospect?.Interactions?.Where(x => x.Type == type).ToList() ?? []; #region Private Method EditProspectAsync
}
private async Task EditProspectAsync() private async Task EditProspectAsync()
{ {
await EditProspectDialog.ShowAsync(ModalService, () => InvokeAsync(StateHasChanged), Prospect); await EditProspectDialog.ShowAsync(ModalService, () => InvokeAsync(StateHasChanged), Prospect);
} }
/// <summary> #endregion
/// Gets or sets the value of the prospect service (ab)
/// </summary>
[Inject]
public ProspectService ProspectService { get; set; } = null!;
#region Private Method GetTyped
private List<Interaction> GetTyped(InteractionType type)
{
return Prospect?.Interactions?.Where(x => x.Type == type).ToList() ?? [];
}
#endregion
#region Private Method RemoveInteraction
/// <summary>
/// Removes a specified interaction by its identifier. Displays a confirmation dialog to the user before performing the removal.
/// </summary>
/// <param name="arg">The unique identifier of the interaction to be removed.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
private async Task RemoveInteraction(Guid arg) private async Task RemoveInteraction(Guid arg)
{ {
var type = Prospect?.Interactions.FirstOrDefault(x => x.Id == arg)?.Type; var type = Prospect?.Interactions.FirstOrDefault(x => x.Id == arg)?.Type;
var typeName = type != null ? type.Value.Translate(AppSettings) : "Interaktion"; var typeName = type != null ? type.Value.Translate(AppSettings) : "Interaktion";
var confirm = await Message.Confirm($"{typeName} wirklich entfernen?", "Bestätigen", o => await ConfirmDialog.ShowAsync(ModalService, "Bestätigen", $"{typeName} wirklich entfernen?", async () =>
{
o.ConfirmButtonText = "Ja, wirklich!";
o.CancelButtonText = "Abbrechen";
o.ShowMessageIcon = false;
});
if (confirm)
{ {
var removeR = await ProspectService.RemoveInteraction(arg); var removeR = await ProspectService.RemoveInteraction(arg);
if(removeR.Success && OnDataChanged != null) await OnDataChanged(); if (removeR.Success && OnDataChanged != null) await OnDataChanged();
} });
} }
#endregion
} }
} }

View File

@@ -0,0 +1,10 @@
@inherits FsBase
<div class="mt-1 mb-3">
@Message
</div>
<div class="d-flex justify-content-end">
<Button Color="Color.Secondary" Clicked="@ModalService.Hide">Abbrechen</Button>
<Button Color="Color.Primary" Clicked="@ConfirmClickAsync" Class="ml-2">OK</Button>
</div>

View File

@@ -0,0 +1,63 @@
using Blazorise;
using FoodsharingSiegen.Server.BaseClasses;
using Microsoft.AspNetCore.Components;
namespace FoodsharingSiegen.Server.Dialogs
{
public partial class ConfirmDialog : FsBase
{
#region Parameters
[Parameter]
public Func<Task>? OnConfirm { get; set; }
[Parameter]
public string? Message { get; set; }
#endregion
#region Public Method ShowAsync
/// <summary>
/// Displays the confirm dialog with the specified title, message, and confirmation action.
/// </summary>
/// <param name="modalService">The modal service used to display the dialog.</param>
/// <param name="title">The title of the confirmation dialog. Defaults to "Bestätigen" if null.</param>
/// <param name="message">The message displayed in the confirmation dialog.</param>
/// <param name="onConfirm">The action to invoke when the user confirms.</param>
/// <returns>A task that represents the asynchronous operation of displaying the dialog.</returns>
public static async Task ShowAsync(IModalService modalService, string? title, string? message, Func<Task> onConfirm)
{
title ??= "Bestätigen";
var x = new Action<ModalProviderParameterBuilder<ConfirmDialog>>(b =>
{
b.Add(nameof(OnConfirm), onConfirm);
b.Add(nameof(Message), message);
});
var options = new ModalInstanceOptions
{
Size = ModalSize.Small
};
await modalService.Show(title, x, options);
}
#endregion
#region Private Method ConfirmClickAsync
/// <summary>
/// Invokes the confirmation action if it is set.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
private async Task ConfirmClickAsync()
{
if (OnConfirm != null) await OnConfirm.Invoke();
await ModalService.Hide();
}
#endregion
}
}

View File

@@ -81,31 +81,5 @@ namespace FoodsharingSiegen.Server.Pages
} }
#endregion #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
} }
} }

View File

@@ -3,9 +3,9 @@
@using FoodsharingSiegen.Shared.Helper @using FoodsharingSiegen.Shared.Helper
@inherits FsBase @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); var filterList = ProspectList.ApplyFilter(Filter);

View File

@@ -1,4 +1,5 @@
using FoodsharingSiegen.Contracts.Entity; using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Helper;
using FoodsharingSiegen.Contracts.Model; using FoodsharingSiegen.Contracts.Model;
using FoodsharingSiegen.Server.Data.Service; using FoodsharingSiegen.Server.Data.Service;
using FoodsharingSiegen.Server.Dialogs; using FoodsharingSiegen.Server.Dialogs;
@@ -43,6 +44,8 @@ namespace FoodsharingSiegen.Server.Pages
/// <inheritdoc /> /// <inheritdoc />
protected override async Task InitializeDataAsync() protected override async Task InitializeDataAsync()
{ {
if(!CurrentUser.IsAdmin()) NavigationManager.NavigateTo("/");
// Load prospects // Load prospects
await LoadProspects(); await LoadProspects();
} }
@@ -64,45 +67,5 @@ namespace FoodsharingSiegen.Server.Pages
} }
#endregion #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
} }
} }

View File

@@ -56,31 +56,5 @@ namespace FoodsharingSiegen.Server.Pages
} }
#endregion #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
} }
} }

View File

@@ -68,45 +68,5 @@ namespace FoodsharingSiegen.Server.Pages
} }
#endregion #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
} }
} }

View File

@@ -38,10 +38,7 @@
PopupTitleTemplate="PopupTitleTemplate" PopupTitleTemplate="PopupTitleTemplate"
RowInserted="RowInserted" RowInserted="RowInserted"
RowUpdated="RowUpdated" RowUpdated="RowUpdated"
RowRemoving="RowRemoving"
RowRemoved="RowRemoved"
PageSize="50" PageSize="50"
@bind-SelectedRow="SelectedUser" @bind-SelectedRow="SelectedUser"
RowDoubleClicked="arg => UserDataGrid?.Edit(arg.Item)!" RowDoubleClicked="arg => UserDataGrid?.Edit(arg.Item)!"
Editable Editable
@@ -59,7 +56,7 @@
</Button> </Button>
</EditCommandTemplate> </EditCommandTemplate>
<DeleteCommandTemplate> <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> <i class="oi oi-trash"></i>
</Button> </Button>
</DeleteCommandTemplate> </DeleteCommandTemplate>

View File

@@ -1,4 +1,3 @@
using Blazorise;
using Blazorise.DataGrid; using Blazorise.DataGrid;
using FoodsharingSiegen.Contracts.Entity; using FoodsharingSiegen.Contracts.Entity;
using FoodsharingSiegen.Contracts.Helper; using FoodsharingSiegen.Contracts.Helper;
@@ -63,6 +62,7 @@ namespace FoodsharingSiegen.Server.Pages
/// <inheritdoc /> /// <inheritdoc />
protected override async Task InitializeDataAsync() protected override async Task InitializeDataAsync()
{ {
if (!CurrentUser.IsAdmin()) NavigationManager.NavigateTo("/");
await LoadUsers(); await LoadUsers();
} }
@@ -98,6 +98,33 @@ namespace FoodsharingSiegen.Server.Pages
#endregion #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 #region Private Method RowInserted
/// <summary> /// <summary>
@@ -115,51 +142,6 @@ namespace FoodsharingSiegen.Server.Pages
#endregion #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 #region Private Method RowUpdated
/// <summary> /// <summary>