mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 11:14:27 +00:00
Add delete button for versions page.
This commit is contained in:
@@ -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>
|
||||
</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>
|
||||
@@ -1,3 +1,4 @@
|
||||
using BlazorBootstrap;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DbContext = Aaru.Server.Database.DbContext;
|
||||
using Version = Aaru.Server.Database.Models.Version;
|
||||
@@ -6,8 +7,10 @@ namespace Aaru.Server.Components.Admin.Pages.Versions;
|
||||
|
||||
public partial class View
|
||||
{
|
||||
bool _initialized;
|
||||
List<Version> _items;
|
||||
private int _deleteId;
|
||||
private Modal? _deleteModal;
|
||||
bool _initialized;
|
||||
List<Version> _items;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnInitializedAsync()
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user