Files
deepdrft/DeepDrftManager/Components/Shared/DeleteTrackDialog.razor
T

89 lines
2.9 KiB
Plaintext

@using System.Net.Http
@using System.Net.Http.Headers
@using Microsoft.AspNetCore.Components
@inject IHttpClientFactory HttpClientFactory
@inject IAuthSession AuthSession
<MudDialog>
<DialogContent>
<MudText Typo="Typo.body1">
Are you sure you want to delete '@TrackName'? This cannot be undone.
</MudText>
@if (!string.IsNullOrEmpty(_errorMessage))
{
<MudAlert Severity="Severity.Error" Class="mt-3" Dense="true">@_errorMessage</MudAlert>
}
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel" Disabled="_isDeleting">Cancel</MudButton>
<MudButton Color="Color.Error"
Variant="Variant.Filled"
OnClick="ConfirmAsync"
Disabled="_isDeleting">
@if (_isDeleting)
{
<MudProgressCircular Size="Size.Small" Indeterminate="true" Class="me-2" />
<span>Deleting...</span>
}
else
{
<span>Delete</span>
}
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = default!;
[Parameter] public long TrackId { get; set; }
[Parameter] public string TrackName { get; set; } = "";
[Parameter] public EventCallback OnDeleted { get; set; }
private bool _isDeleting;
private string? _errorMessage;
private async Task ConfirmAsync()
{
_isDeleting = true;
_errorMessage = null;
try
{
var client = HttpClientFactory.CreateClient("DeepDrft.API");
var token = await AuthSession.GetValidTokenAsync();
if (!string.IsNullOrEmpty(token))
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await client.DeleteAsync($"api/cms/track/{TrackId}");
if (response.IsSuccessStatusCode)
{
if (OnDeleted.HasDelegate)
{
await OnDeleted.InvokeAsync();
}
MudDialog.Close(DialogResult.Ok(true));
return;
}
_errorMessage = response.StatusCode switch
{
System.Net.HttpStatusCode.NotFound => "Track not found. It may have already been deleted.",
System.Net.HttpStatusCode.Unauthorized => "You are not authorized to delete this track.",
System.Net.HttpStatusCode.Forbidden => "You are not authorized to delete this track.",
_ => $"Delete failed ({(int)response.StatusCode})."
};
}
catch (Exception ex)
{
_errorMessage = $"Delete failed: {ex.Message}";
}
finally
{
_isDeleting = false;
}
}
private void Cancel() => MudDialog.Cancel();
}