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:
@@ -1,4 +1,4 @@
|
||||
<Blazorise.ThemeProvider Theme="@theme">
|
||||
<Blazorise.ThemeProvider Theme="@_theme">
|
||||
<Router AppAssembly="@typeof(App).Assembly">
|
||||
<Found Context="routeData">
|
||||
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(DefaultLayout)">
|
||||
@@ -13,15 +13,14 @@
|
||||
</NotFound>
|
||||
</Router>
|
||||
|
||||
<MessageProvider />
|
||||
<ModalProvider Centered="true" />
|
||||
</Blazorise.ThemeProvider>
|
||||
|
||||
|
||||
@code{
|
||||
private Theme theme = new()
|
||||
private readonly Theme _theme = new()
|
||||
{
|
||||
ColorOptions = new ThemeColorOptions
|
||||
ColorOptions = new()
|
||||
{
|
||||
Primary = "#64ae24",
|
||||
Secondary = "#533a20",
|
||||
|
||||
@@ -31,12 +31,6 @@ namespace FoodsharingSiegen.Server.BaseClasses
|
||||
[Inject]
|
||||
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>
|
||||
/// Gets or sets the modal service for handling modals within the application
|
||||
/// </summary>
|
||||
|
||||
@@ -8,59 +8,83 @@ namespace FoodsharingSiegen.Server.Controls
|
||||
{
|
||||
public partial class ProspectContainer
|
||||
{
|
||||
[Parameter] public Prospect? Prospect { get; set; }
|
||||
|
||||
[Parameter] public ProspectStateFilter StateFilter { get; set; }
|
||||
|
||||
[Parameter] public string? CssClass { get; set; }
|
||||
|
||||
[Parameter] public Func<Task>? OnDataChanged { get; set; }
|
||||
|
||||
|
||||
private async Task AddInteraction(InteractionType type)
|
||||
{
|
||||
if (Prospect != null && OnDataChanged != null)
|
||||
{
|
||||
var headerText = $"{type.Translate(AppSettings)} eintragen";
|
||||
|
||||
await InteractionDialog.ShowAsync(ModalService, new(type, Prospect.Id, headerText, OnDataChanged));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private List<Interaction> GetTyped(InteractionType type)
|
||||
{
|
||||
return Prospect?.Interactions?.Where(x => x.Type == type).ToList() ?? [];
|
||||
}
|
||||
|
||||
private async Task EditProspectAsync()
|
||||
{
|
||||
await EditProspectDialog.ShowAsync(ModalService, () => InvokeAsync(StateHasChanged), Prospect);
|
||||
}
|
||||
#region Dependencies
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the value of the prospect service (ab)
|
||||
/// </summary>
|
||||
[Inject]
|
||||
public ProspectService ProspectService { get; set; } = null!;
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#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)
|
||||
{
|
||||
if (Prospect != null && OnDataChanged != null)
|
||||
{
|
||||
var headerText = $"{type.Translate(AppSettings)} eintragen";
|
||||
|
||||
await InteractionDialog.ShowAsync(ModalService, new(type, Prospect.Id, headerText, OnDataChanged));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method EditProspectAsync
|
||||
|
||||
private async Task EditProspectAsync()
|
||||
{
|
||||
await EditProspectDialog.ShowAsync(ModalService, () => InvokeAsync(StateHasChanged), Prospect);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#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)
|
||||
{
|
||||
var type = Prospect?.Interactions.FirstOrDefault(x => x.Id == arg)?.Type;
|
||||
var typeName = type != null ? type.Value.Translate(AppSettings) : "Interaktion";
|
||||
|
||||
var confirm = await Message.Confirm($"{typeName} wirklich entfernen?", "Bestätigen", o =>
|
||||
{
|
||||
o.ConfirmButtonText = "Ja, wirklich!";
|
||||
o.CancelButtonText = "Abbrechen";
|
||||
o.ShowMessageIcon = false;
|
||||
});
|
||||
|
||||
if (confirm)
|
||||
await ConfirmDialog.ShowAsync(ModalService, "Bestätigen", $"{typeName} wirklich entfernen?", async () =>
|
||||
{
|
||||
var removeR = await ProspectService.RemoveInteraction(arg);
|
||||
if(removeR.Success && OnDataChanged != null) await OnDataChanged();
|
||||
}
|
||||
if (removeR.Success && OnDataChanged != null) await OnDataChanged();
|
||||
});
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
10
FoodsharingSiegen.Server/Dialogs/ConfirmDialog.razor
Normal file
10
FoodsharingSiegen.Server/Dialogs/ConfirmDialog.razor
Normal 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>
|
||||
63
FoodsharingSiegen.Server/Dialogs/ConfirmDialog.razor.cs
Normal file
63
FoodsharingSiegen.Server/Dialogs/ConfirmDialog.razor.cs
Normal 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
|
||||
}
|
||||
}
|
||||
@@ -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