Add delete button for versions page.

This commit is contained in:
2025-09-11 18:57:19 +01:00
parent cc91eea61d
commit 5389463cc0
2 changed files with 62 additions and 3 deletions

View File

@@ -2,6 +2,7 @@
@using Version = Aaru.Server.Database.Models.Version
@attribute [Authorize]
@layout AdminLayout
@rendermode InteractiveServer
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
@@ -26,6 +27,9 @@
<th class="fw-bold bg-secondary text-light">
@DisplayNameHelper.GetDisplayName(typeof(Version), nameof(Version.Count))
</th>
<th class="fw-bold bg-secondary text-light">
Actions
</th>
</tr>
</thead>
<tbody>
@@ -38,8 +42,23 @@
<td>
@item.Count
</td>
<td>
<button class="btn btn-danger btn-sm" @onclick="async () => await ShowDeleteModal(item.Id)">
<i class="bi bi-trash"></i> Delete
</button>
</td>
</tr>
}
</tbody>
</table>
</section>
<BlazorBootstrap.Modal @ref="_deleteModal" Title="Delete Version" Size="ModalSize.Small">
<BodyTemplate>
<div class="text-danger">Are you sure you want to delete this version?</div>
</BodyTemplate>
<FooterTemplate>
<button class="btn btn-secondary" @onclick="HideDeleteModal">Cancel</button>
<button class="btn btn-danger" @onclick="ConfirmDelete">Delete</button>
</FooterTemplate>
</BlazorBootstrap.Modal>

View File

@@ -1,3 +1,4 @@
using BlazorBootstrap;
using Microsoft.EntityFrameworkCore;
using DbContext = Aaru.Server.Database.DbContext;
using Version = Aaru.Server.Database.Models.Version;
@@ -6,6 +7,8 @@ namespace Aaru.Server.Components.Admin.Pages.Versions;
public partial class View
{
private int _deleteId;
private Modal? _deleteModal;
bool _initialized;
List<Version> _items;
@@ -24,4 +27,41 @@ public partial class View
StateHasChanged();
}
private async Task ShowDeleteModal(int id)
{
_deleteId = id;
if(_deleteModal != null) await _deleteModal.ShowAsync();
}
private async Task HideDeleteModal()
{
if(_deleteModal != null) await _deleteModal.HideAsync();
}
private async Task ConfirmDelete()
{
await DeleteVersionAsync(_deleteId);
await HideDeleteModal();
await RefreshItemsAsync();
}
private async Task DeleteVersionAsync(int id)
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
Version? version = await ctx.Versions.FindAsync(id);
if(version is not null)
{
ctx.Versions.Remove(version);
await ctx.SaveChangesAsync();
}
}
private async Task RefreshItemsAsync()
{
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
_items = await ctx.Versions.OrderBy(static v => v.Name).ToListAsync();
StateHasChanged();
}
}