BlazorStart/Start/Client/Store/Features/DeleteContainer/DeleteContainerEffects.cs
2021-12-03 16:44:02 -08:00

57 lines
1.8 KiB
C#

using System.Threading.Tasks;
using Fluxor;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Start.Client.Store.Features.CurrentContainer;
using Start.Shared.Api;
using System.Net;
using Start.Client.Store.State;
using Start.Client.Store.Features.ContainersList;
namespace Start.Client.Store.Features.DeleteContainer {
public class DeleteContainerEffects {
public IBookmarkContainersApi BookmarkContainersApi { get; init; }
public IState<RootState> RootState { get; set; }
public DeleteContainerEffects(IBookmarkContainersApi bookmarkContainersApi,
IState<RootState> rootState) {
this.BookmarkContainersApi = bookmarkContainersApi;
this.RootState = rootState;
}
[EffectMethod]
public async Task SubmitDeleteContainer(SubmitDeleteContainerAction action,
IDispatcher dispatch) {
dispatch.Dispatch(new FetchDeleteContainerFormAction());
try {
System.Net.Http.HttpResponseMessage? apiResponse = await this.BookmarkContainersApi
.DeleteBookmarkContainer(action.ContainerIdToDelete);
if (apiResponse == null) {
dispatch.Dispatch(
new ErrorFetchingDeleteContainerAction("Failed to submit request"));
return;
}
if (apiResponse.StatusCode == HttpStatusCode.NotFound) {
dispatch.Dispatch(new ErrorFetchingDeleteContainerAction(
"The bookmark container to delete doesn't exist"));
return;
}
if (!apiResponse.IsSuccessStatusCode) {
dispatch.Dispatch(new ErrorFetchingDeleteContainerAction(
"There was an error deleting the bookmark container"));
return;
}
dispatch.Dispatch(new RemoveContainerFromListAction(action.ContainerIdToDelete));
dispatch.Dispatch(new FixCurrentContainerAction());
dispatch.Dispatch(new RecievedDeleteContainerAction());
} catch (AccessTokenNotAvailableException e) {
e.Redirect();
}
}
}
}