2021-12-05 23:50:48 +00:00
|
|
|
|
using Start.Shared.Api;
|
|
|
|
|
using Fluxor;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
|
|
|
|
|
using Refit;
|
|
|
|
|
using Start.Shared;
|
|
|
|
|
using Start.Client.Store.Features.CurrentContainer;
|
|
|
|
|
using System;
|
2022-04-19 20:04:38 +00:00
|
|
|
|
using System.Linq;
|
2021-12-05 23:50:48 +00:00
|
|
|
|
|
|
|
|
|
namespace Start.Client.Store.Features.CreateGroup {
|
|
|
|
|
public class CreateGroupEffects {
|
|
|
|
|
public IBookmarkGroupsApi BookmarkGroupsApi { get; init; }
|
2022-04-19 20:04:38 +00:00
|
|
|
|
public IBookmarkContainersApi BookmarkContainersApi { get; init; }
|
2021-12-05 23:50:48 +00:00
|
|
|
|
|
2022-04-19 20:04:38 +00:00
|
|
|
|
public CreateGroupEffects(IBookmarkGroupsApi bookmarksApi,
|
|
|
|
|
IBookmarkContainersApi bookmarkContainersApi) {
|
2021-12-05 23:50:48 +00:00
|
|
|
|
this.BookmarkGroupsApi = bookmarksApi;
|
2022-04-19 20:04:38 +00:00
|
|
|
|
this.BookmarkContainersApi = bookmarkContainersApi;
|
2021-12-05 23:50:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[EffectMethod]
|
|
|
|
|
public async Task SubmitCreateBookmarkGroup(SubmitCreateGroupAction action,
|
|
|
|
|
IDispatcher dispatch) {
|
|
|
|
|
dispatch.Dispatch(new FetchCreateGroupAction());
|
|
|
|
|
|
|
|
|
|
try {
|
2022-04-19 20:04:38 +00:00
|
|
|
|
ApiResponse<BookmarkContainerDto?>? containerResponse = await this
|
|
|
|
|
.BookmarkContainersApi
|
|
|
|
|
.GetBookmarkContainer(action.NewGroup.BookmarkContainerId);
|
|
|
|
|
|
|
|
|
|
if (containerResponse == null || containerResponse.Content == null) {
|
|
|
|
|
dispatch.Dispatch(new ErrorFetchingCreateGroupAction(
|
|
|
|
|
"There was an error checking the new group's bookmark container"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sortOrder = !(containerResponse.Content.BookmarkGroups?.Any() ?? false)
|
|
|
|
|
? 0
|
|
|
|
|
: containerResponse.Content.BookmarkGroups.Max(g => g.SortOrder) + 1;
|
|
|
|
|
|
2021-12-05 23:50:48 +00:00
|
|
|
|
ApiResponse<BookmarkGroupDto?> apiResponse = await this.BookmarkGroupsApi
|
2022-04-19 20:04:38 +00:00
|
|
|
|
.CreateBookmarkGroup(action.NewGroup.Title, action.NewGroup.Color, sortOrder,
|
2021-12-05 23:50:48 +00:00
|
|
|
|
action.NewGroup.BookmarkContainerId);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Status code: " + apiResponse.StatusCode);
|
|
|
|
|
|
|
|
|
|
if (!apiResponse.IsSuccessStatusCode) {
|
|
|
|
|
dispatch.Dispatch(new ErrorFetchingCreateGroupAction(
|
|
|
|
|
"Error creating bookmark group"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 00:27:13 +00:00
|
|
|
|
if (apiResponse.Content == null) {
|
|
|
|
|
dispatch.Dispatch(new ErrorFetchingCreateGroupAction(
|
|
|
|
|
"Error creating bookmark group"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dispatch.Dispatch(new AddBookmarkGroupAction(apiResponse.Content));
|
2021-12-05 23:50:48 +00:00
|
|
|
|
dispatch.Dispatch(new RecievedCreateGroupAction());
|
|
|
|
|
dispatch.Dispatch(new HideCreateGroupFormAction());
|
|
|
|
|
} catch (AccessTokenNotAvailableException e) {
|
|
|
|
|
e.Redirect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|