Use Fluxor for state management

This commit is contained in:
Neil Brommer 2021-12-03 16:44:02 -08:00
parent 437e90039f
commit 560c25b4e8
27 changed files with 823 additions and 210 deletions

View file

@ -0,0 +1,27 @@
using Start.Shared;
namespace Start.Client.Store.Features.CreateContainer {
public class ShowCreateContainerFormAction { }
public class HideCreateContainerFormAction { }
public class FetchCreateContainerAction { }
public class ReceivedCreateContainerAction { }
public class ErrorFetchingCreateContainerAction {
public string ErrorMessage { get; set; }
public ErrorFetchingCreateContainerAction(string errorMessage) {
this.ErrorMessage = errorMessage;
}
}
public class SubmitCreateContainerAction {
public BookmarkContainerDto NewContainer { get; set; }
public SubmitCreateContainerAction(BookmarkContainerDto container) {
this.NewContainer = container;
}
}
}

View file

@ -0,0 +1,43 @@
using System.Threading.Tasks;
using Fluxor;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Refit;
using Start.Shared;
using Start.Shared.Api;
using Start.Client.Store.Features.CurrentContainer;
using Start.Client.Store.Features.ContainersList;
namespace Start.Client.Store.Features.CreateContainer {
public class CreateContainerEffects {
public IBookmarkContainersApi BookmarkContainersApi { get; set; }
public CreateContainerEffects(IBookmarkContainersApi bookmarkContainersApi) {
this.BookmarkContainersApi = bookmarkContainersApi;
}
[EffectMethod]
public async Task SubmitCreateContainer(SubmitCreateContainerAction action,
IDispatcher dispatch) {
dispatch.Dispatch(new FetchCreateContainerAction());
try {
ApiResponse<BookmarkContainerDto?> apiResponse = await this.BookmarkContainersApi
.CreateBookmarkContainer(action.NewContainer.Title);
BookmarkContainerDto? container = apiResponse.Content;
if (container == null)
dispatch.Dispatch(new ErrorFetchingCreateContainerAction(
"Failed to create container"));
else {
dispatch.Dispatch(new AddContainerToListAction(container));
dispatch.Dispatch(new ReceivedCreateContainerAction());
dispatch.Dispatch(new LoadCurrentContainerAction(
container.BookmarkContainerId));
}
} catch (AccessTokenNotAvailableException e) {
e.Redirect();
}
}
}
}

View file

@ -0,0 +1,12 @@
using Fluxor;
using Start.Client.Store.State;
namespace Start.Client.Store.Features.CreateContainer {
public class CreateContainerFeature : Feature<CreateContainerState> {
public override string GetName() => "Create Container";
protected override CreateContainerState GetInitialState() {
return new CreateContainerState();
}
}
}

View file

@ -0,0 +1,44 @@
using Fluxor;
namespace Start.Client.Store.Features.CreateContainer {
public static class CreateContainerReducers {
[ReducerMethod(typeof(ShowCreateContainerFormAction))]
public static CreateContainerState ShowCreateContainerForm(CreateContainerState state) {
return state with {
ShowCreateContainerForm = true
};
}
[ReducerMethod(typeof(HideCreateContainerFormAction))]
public static CreateContainerState HideCreateContainerForm(CreateContainerState state) {
return state with {
ShowCreateContainerForm = false
};
}
[ReducerMethod(typeof(FetchCreateContainerAction))]
public static CreateContainerState FetchCreateContainer(CreateContainerState state) {
return state with {
IsLoadingCreateContainer = true
};
}
[ReducerMethod(typeof(ReceivedCreateContainerAction))]
public static CreateContainerState ReceivedCreateContainer(CreateContainerState state) {
return state with {
IsLoadingCreateContainer = false,
CreateContainerErrorMessage = null,
ShowCreateContainerForm = false
};
}
[ReducerMethod]
public static CreateContainerState ErrorFetchingCreateContainer(CreateContainerState state,
ErrorFetchingCreateContainerAction action) {
return state with {
IsLoadingCreateContainer = false,
CreateContainerErrorMessage = action.ErrorMessage
};
}
}
}

View file

@ -0,0 +1,20 @@
using Start.Client.Store.State;
namespace Start.Client.Store.Features.CreateContainer {
public record CreateContainerState : RootState {
public bool ShowCreateContainerForm { get; init; }
public bool IsLoadingCreateContainer { get; init; }
public string? CreateContainerErrorMessage { get; init; }
public CreateContainerState() { }
public CreateContainerState(ContainerListState containerList,
CurrentContainerState currentContainer, bool showCreateContainer, bool isLoading,
string? errorMessage)
: base(containerList, currentContainer) {
this.ShowCreateContainerForm = showCreateContainer;
this.IsLoadingCreateContainer = isLoading;
this.CreateContainerErrorMessage = errorMessage;
}
}
}