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 = "";
}
}
}