Add create bookmark

This commit is contained in:
Neil Brommer 2021-12-13 16:27:13 -08:00
parent 64b893b778
commit 55625b1be4
19 changed files with 382 additions and 13 deletions

View file

@ -0,0 +1,35 @@
using Start.Shared;
namespace Start.Client.Store.Features.CreateBookmark {
public class ShowCreateBookmarkFormAction {
public int GroupId { get; init; }
public string GroupTitle { get; init; }
public ShowCreateBookmarkFormAction(int groupId, string groupTitle) {
this.GroupId = groupId;
this.GroupTitle = groupTitle;
}
}
public class HideCreateBookmarkFormAction { }
public class FetchCreateBookmarkAction { }
public class ReceivedCreateBookmarkAction { }
public class ErrorFetchingCreateBookmarkAction {
public string ErrorMessage { get; init; }
public ErrorFetchingCreateBookmarkAction(string errorMessage) {
this.ErrorMessage = errorMessage;
}
}
public class SubmitCreateBookmarkAction {
public BookmarkDto NewBookmark { get; init; }
public SubmitCreateBookmarkAction(BookmarkDto newBookmark) {
this.NewBookmark = newBookmark;
}
}
}

View file

@ -0,0 +1,45 @@
using System.Threading.Tasks;
using Fluxor;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Start.Shared.Api;
using Start.Client.Store.Features.CurrentContainer;
namespace Start.Client.Store.Features.CreateBookmark {
public class CreateBookmarkEffects {
public IBookmarksApi BookmarksApi { get; init; }
public CreateBookmarkEffects(IBookmarksApi bookmarksApi) {
this.BookmarksApi = bookmarksApi;
}
[EffectMethod]
public async Task SubmitCreateBookmark(SubmitCreateBookmarkAction action,
IDispatcher dispatch) {
dispatch.Dispatch(new FetchCreateBookmarkAction());
try {
Refit.ApiResponse<Start.Shared.BookmarkDto?>? apiResponse = await this.BookmarksApi
.CreateBookmark(action.NewBookmark.Title, action.NewBookmark.Url,
action.NewBookmark.Notes, action.NewBookmark.BookmarkGroupId);
if (!apiResponse.IsSuccessStatusCode) {
dispatch.Dispatch(new ErrorFetchingCreateBookmarkAction(
"Error creating bookmark group: Status code " + apiResponse.StatusCode.ToString()));
return;
}
if (apiResponse.Content == null) {
dispatch.Dispatch(new ErrorFetchingCreateBookmarkAction(
"Error creating bookmark group"));
return;
}
dispatch.Dispatch(new AddBookmarkAction(apiResponse.Content));
dispatch.Dispatch(new ReceivedCreateBookmarkAction());
dispatch.Dispatch(new HideCreateBookmarkFormAction());
} catch (AccessTokenNotAvailableException e) {
e.Redirect();
}
}
}
}

View file

@ -0,0 +1,11 @@
using Fluxor;
namespace Start.Client.Store.Features.CreateBookmark {
public class CreateBookmarkFeature : Feature<CreateBookmarkState> {
public override string GetName() => "Create Bookmark";
protected override CreateBookmarkState GetInitialState() {
return new CreateBookmarkState();
}
}
}

View file

@ -0,0 +1,41 @@
using Fluxor;
namespace Start.Client.Store.Features.CreateBookmark {
public static class CreateBookmarkReducers {
[ReducerMethod]
public static CreateBookmarkState ShowCreateBookmarkForm(CreateBookmarkState state,
ShowCreateBookmarkFormAction action) {
return state with {
ShowCreateBookmarkForm = true,
GroupId = action.GroupId,
GroupTitle = action.GroupTitle,
IsLoadingCreateBookmark = false,
CreateBookmarkErrorMessage = null
};
}
[ReducerMethod(typeof(HideCreateBookmarkFormAction))]
public static CreateBookmarkState HideCreateBookmarkForm(CreateBookmarkState state) {
return state with {
ShowCreateBookmarkForm = false
};
}
[ReducerMethod(typeof(FetchCreateBookmarkAction))]
public static CreateBookmarkState FetchCreateBookmark(CreateBookmarkState state) {
return state with {
IsLoadingCreateBookmark = true,
CreateBookmarkErrorMessage = null
};
}
[ReducerMethod]
public static CreateBookmarkState ErrorFetchingCreateBookmark(CreateBookmarkState state,
ErrorFetchingCreateBookmarkAction action) {
return state with {
CreateBookmarkErrorMessage = action.ErrorMessage,
IsLoadingCreateBookmark = false
};
}
}
}

View file

@ -0,0 +1,15 @@
using Start.Client.Store.State;
namespace Start.Client.Store.Features.CreateBookmark {
public record CreateBookmarkState : RootState {
public bool ShowCreateBookmarkForm { get; init; }
public int GroupId { get; init; }
public string GroupTitle { get; init; }
public bool IsLoadingCreateBookmark { get; init; }
public string? CreateBookmarkErrorMessage { get; init; }
public CreateBookmarkState() {
this.GroupTitle = "";
}
}
}

View file

@ -33,7 +33,13 @@ namespace Start.Client.Store.Features.CreateGroup {
return;
}
dispatch.Dispatch(new AddBookmarkGroupAction(action.NewGroup));
if (apiResponse.Content == null) {
dispatch.Dispatch(new ErrorFetchingCreateGroupAction(
"Error creating bookmark group"));
return;
}
dispatch.Dispatch(new AddBookmarkGroupAction(apiResponse.Content));
dispatch.Dispatch(new RecievedCreateGroupAction());
dispatch.Dispatch(new HideCreateGroupFormAction());
} catch (AccessTokenNotAvailableException e) {

View file

@ -44,4 +44,20 @@ namespace Start.Client.Store.Features.CurrentContainer {
this.BookmarkGroupId = bookmarkGroupId;
}
}
public class AddBookmarkAction {
public BookmarkDto Bookmark { get; init; }
public AddBookmarkAction(BookmarkDto bookmark) {
this.Bookmark = bookmark;
}
}
public class RemoveBookmarkAction {
public int BookmarkId { get; init; }
public RemoveBookmarkAction(int bookmarkId) {
this.BookmarkId = bookmarkId;
}
}
}

View file

@ -78,5 +78,56 @@ namespace Start.Client.Store.Features.CurrentContainer {
}
};
}
[ReducerMethod]
public static RootState AddBookmark(RootState state, AddBookmarkAction action) {
BookmarkContainerDto? container = state.CurrentContainerState.Container;
if (container == null)
return state;
List<BookmarkGroupDto>? groups = container.BookmarkGroups
?.Select(bg => {
if (bg.BookmarkGroupId == action.Bookmark.BookmarkGroupId) {
return new BookmarkGroupDto(bg.BookmarkGroupId, bg.Title, bg.Color,
bg.BookmarkContainerId,
bg.Bookmarks?
.Concat(new List<BookmarkDto> { action.Bookmark })
.ToList());
}
return bg;
})
.ToList();
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = new BookmarkContainerDto(container.BookmarkContainerId,
container.Title, groups)
}
};
}
[ReducerMethod]
public static RootState RemoveBookmark(RootState state, RemoveBookmarkAction action) {
BookmarkContainerDto? container = state.CurrentContainerState.Container;
if (container == null)
return state;
List<BookmarkGroupDto>? groups = container.BookmarkGroups
?.Select(bg => new BookmarkGroupDto(bg.BookmarkGroupId, bg.Title, bg.Color,
bg.BookmarkContainerId, bg.Bookmarks
?.Where(b => b.BookmarkId != action.BookmarkId)
.ToList()))
.ToList();
return state with {
CurrentContainerState = state.CurrentContainerState with {
Container = new BookmarkContainerDto(container.BookmarkContainerId,
container.Title, groups)
}
};
}
}
}

View file

@ -1,6 +1,7 @@
using System;
using Start.Client.Store.State;
namespace Start.Client.Store.Features.DeleteGroup {
public record DeleteGroupState {
public record DeleteGroupState : RootState {
public bool ShowDeleteGroupForm { get; init; }
public int BookmarkGroupIdToDelete { get; init; }
public string BookmarkGroupTitleToDelete { get; init; }