Updated delete logic for voice works.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
@using AntDesign
|
||||
@using JSMR.Application.VoiceWorks.Commands.Delete
|
||||
@using JSMR.Application.VoiceWorks.Commands.SetFavorite
|
||||
@using JSMR.Application.VoiceWorks.Queries.Search
|
||||
@using JSMR.Domain.Enums
|
||||
@@ -136,6 +137,9 @@
|
||||
[Parameter]
|
||||
public required VoiceWorkSearchResult Product { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public EventCallback ProductDeleted { get; set; }
|
||||
|
||||
private string GetCardClasses(VoiceWorkSearchResult voiceWork)
|
||||
{
|
||||
List<string> classNames = ["j-card", "j-voice-work-card"];
|
||||
@@ -240,7 +244,38 @@
|
||||
{
|
||||
Title = "Are you sure you want to delete the following product?",
|
||||
Icon = icon,
|
||||
Content = Product.ProductName
|
||||
Content = Product.ProductName,
|
||||
Centered = true,
|
||||
OnOk = async (e) =>
|
||||
{
|
||||
DeleteVoiceWorkRequest request = new(
|
||||
VoiceWorkIds: [Product.VoiceWorkId]
|
||||
);
|
||||
|
||||
try
|
||||
{
|
||||
DeleteVoiceWorkResponse? response = await Client.DeleteVoiceWorkAsync(request);
|
||||
|
||||
if (response is null || response.Results[Product.VoiceWorkId] != DeleteVoiceWorkStatus.Deleted)
|
||||
return;
|
||||
|
||||
await ProductDeleted.InvokeAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
AntDesign.ConfirmOptions errorOptions = new()
|
||||
{
|
||||
Title = "Unable to delete product",
|
||||
Content = "Something went wrong while deleting this product. The product was not deleted. Check the API logs for details.",
|
||||
Centered = true,
|
||||
//Width = "70vw",
|
||||
};
|
||||
|
||||
await ModalService.ErrorAsync(errorOptions);
|
||||
|
||||
e.Cancel = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
await ModalService.ConfirmAsync(options);
|
||||
|
||||
@@ -13,7 +13,7 @@ else
|
||||
<div class="j-product-items-container">
|
||||
@foreach (var product in Products)
|
||||
{
|
||||
<JProduct Product="@product"></JProduct>
|
||||
<JProduct Product="@product" ProductDeleted="OnProductDeleted"></JProduct>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
@@ -21,4 +21,12 @@ else
|
||||
@code {
|
||||
[Parameter]
|
||||
public VoiceWorkSearchResult[]? Products { get; set; }
|
||||
|
||||
[Parameter]
|
||||
public EventCallback ProductDeleted { get; set; }
|
||||
|
||||
private async Task OnProductDeleted()
|
||||
{
|
||||
await ProductDeleted.InvokeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
9
JSMR.UI.Blazor/Exceptions/ApiException.cs
Normal file
9
JSMR.UI.Blazor/Exceptions/ApiException.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Net;
|
||||
|
||||
namespace JSMR.UI.Blazor.Exceptions;
|
||||
|
||||
public sealed class ApiException(HttpStatusCode statusCode, string message, string? responseBody = null) : Exception(message)
|
||||
{
|
||||
public HttpStatusCode StatusCode { get; } = statusCode;
|
||||
public string? ResponseBody { get; } = responseBody;
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
<JProductCollection Products="upcomingVoiceWorks"></JProductCollection>
|
||||
</BitPivotItem>
|
||||
<BitPivotItem HeaderText="@($"Announcements ({announcedVoiceWorks?.Length ?? 0})")">
|
||||
<JProductCollection Products="announcedVoiceWorks"></JProductCollection>
|
||||
<JProductCollection Products="announcedVoiceWorks" ProductDeleted="OnAnnouncedProductDeleted"></JProductCollection>
|
||||
</BitPivotItem>
|
||||
</BitPivot>
|
||||
|
||||
@@ -105,4 +105,9 @@
|
||||
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private async Task OnAnnouncedProductDeleted()
|
||||
{
|
||||
_ = LoadAnnouncedVoiceWorksAsync();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ using JSMR.Application.Tags.Queries.Search;
|
||||
using JSMR.Application.VoiceWorks.Commands.Delete;
|
||||
using JSMR.Application.VoiceWorks.Commands.SetFavorite;
|
||||
using JSMR.Application.VoiceWorks.Queries.Search;
|
||||
using JSMR.UI.Blazor.Exceptions;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
@@ -23,6 +24,18 @@ public class VoiceWorksClient(HttpClient http)
|
||||
}
|
||||
};
|
||||
|
||||
private static async Task<T?> ReadJsonOrThrowAsync<T>(HttpResponseMessage response, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
string body = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
|
||||
throw new ApiException(response.StatusCode, $"Request failed: {(int)response.StatusCode}", body);
|
||||
}
|
||||
|
||||
return await response.Content.ReadFromJsonAsync<T>(JsonOptions, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<SearchVoiceWorksResponse?> SearchAsync(SearchVoiceWorksRequest request, CancellationToken ct = default)
|
||||
{
|
||||
using var resp = await http.PostAsJsonAsync("/api/voiceworks/search", request, ct);
|
||||
@@ -56,7 +69,8 @@ public class VoiceWorksClient(HttpClient http)
|
||||
public async Task<DeleteVoiceWorkResponse?> DeleteVoiceWorkAsync(DeleteVoiceWorkRequest request, CancellationToken ct = default)
|
||||
{
|
||||
using var resp = await http.PostAsJsonAsync("/api/voicework/delete", request, ct);
|
||||
return await resp.Content.ReadFromJsonAsync<DeleteVoiceWorkResponse>(JsonOptions, cancellationToken: ct);
|
||||
//return await resp.Content.ReadFromJsonAsync<DeleteVoiceWorkResponse>(JsonOptions, cancellationToken: ct);
|
||||
return await ReadJsonOrThrowAsync<DeleteVoiceWorkResponse>(resp, ct);
|
||||
}
|
||||
|
||||
public async Task<UpdateTagStatusResponse?> UpdateTagStatusAsync(UpdateTagStatusRequest request, CancellationToken ct = default)
|
||||
|
||||
Reference in New Issue
Block a user