Use Fluxor for state management
This commit is contained in:
parent
437e90039f
commit
560c25b4e8
27 changed files with 823 additions and 210 deletions
|
|
@ -0,0 +1,42 @@
|
|||
using System.Collections.Generic;
|
||||
using Start.Shared;
|
||||
|
||||
namespace Start.Client.Store.Features.ContainersList {
|
||||
/// <summary>Dispatch before sending an API request</summary>
|
||||
public class FetchContainerListAction { }
|
||||
|
||||
/// <summary>Dispatch after recieving the container list from an API request</summary>
|
||||
public class RecievedContainerListAction {
|
||||
public IList<BookmarkContainerDto> Containers { get; set; }
|
||||
|
||||
public RecievedContainerListAction(IList<BookmarkContainerDto> containers) {
|
||||
this.Containers = containers;
|
||||
}
|
||||
}
|
||||
|
||||
public class ErrorFetchingContainerListAction {
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
public ErrorFetchingContainerListAction(string errorMessage) {
|
||||
this.ErrorMessage = errorMessage;
|
||||
}
|
||||
}
|
||||
|
||||
public class LoadContainerListAction { }
|
||||
|
||||
public class RemoveContainerFromListAction {
|
||||
public int ContainerIdToRemove { get; set; }
|
||||
|
||||
public RemoveContainerFromListAction(int containerIdToRemove) {
|
||||
this.ContainerIdToRemove = containerIdToRemove;
|
||||
}
|
||||
}
|
||||
|
||||
public class AddContainerToListAction {
|
||||
public BookmarkContainerDto NewContainer { get; set; }
|
||||
|
||||
public AddContainerToListAction(BookmarkContainerDto newContainer) {
|
||||
this.NewContainer = newContainer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Fluxor;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
|
||||
using Refit;
|
||||
using Start.Shared;
|
||||
using Start.Shared.Api;
|
||||
|
||||
namespace Start.Client.Store.Features.ContainersList {
|
||||
public class ContainerListEffects {
|
||||
public IBookmarkContainersApi BookmarkContainersApi { get; init; }
|
||||
|
||||
public ContainerListEffects(IBookmarkContainersApi bookmarkContainersApi) {
|
||||
this.BookmarkContainersApi = bookmarkContainersApi;
|
||||
}
|
||||
|
||||
[EffectMethod(typeof(LoadContainerListAction))]
|
||||
public async Task LoadContainerList(IDispatcher dispatch) {
|
||||
dispatch.Dispatch(new FetchContainerListAction());
|
||||
|
||||
try {
|
||||
ApiResponse<IEnumerable<BookmarkContainerDto>> response = await this
|
||||
.BookmarkContainersApi
|
||||
.GetAllBookmarkContainers();
|
||||
|
||||
List<BookmarkContainerDto>? bookmarkContainers = response.Content?.ToList();
|
||||
|
||||
if (bookmarkContainers == null) {
|
||||
dispatch.Dispatch(new ErrorFetchingContainerListAction(
|
||||
"Failed to fetch containers list"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!bookmarkContainers.Any())
|
||||
throw new NotImplementedException("Create bookmark effect has not been created");
|
||||
|
||||
dispatch.Dispatch(new RecievedContainerListAction(bookmarkContainers));
|
||||
}
|
||||
catch (AccessTokenNotAvailableException e) {
|
||||
e.Redirect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Fluxor;
|
||||
using Start.Client.Store.State;
|
||||
using Start.Shared;
|
||||
|
||||
namespace Start.Client.Store.Features.ContainersList {
|
||||
public static class ContainerListReducers {
|
||||
[ReducerMethod(typeof(FetchContainerListAction))]
|
||||
public static RootState OnFetchContainerList(RootState state) {
|
||||
return state with {
|
||||
ContainerListState = state.ContainerListState with {
|
||||
Containers = ImmutableList<BookmarkContainerDto>.Empty,
|
||||
IsLoadingContainersList = true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[ReducerMethod]
|
||||
public static RootState OnRecievedContainerList(RootState state,
|
||||
RecievedContainerListAction action) {
|
||||
return state with {
|
||||
ContainerListState = state.ContainerListState with {
|
||||
Containers = action.Containers.ToImmutableList(),
|
||||
IsLoadingContainersList = false,
|
||||
ErrorMessage = null
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[ReducerMethod]
|
||||
public static RootState OnErrorFetchingContainerList(RootState state,
|
||||
ErrorFetchingContainerListAction action) {
|
||||
return state with {
|
||||
ContainerListState = state.ContainerListState with {
|
||||
ErrorMessage = action.ErrorMessage
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[ReducerMethod]
|
||||
public static RootState AddContainerToList(RootState state,
|
||||
AddContainerToListAction action) {
|
||||
return state with {
|
||||
ContainerListState = state.ContainerListState with {
|
||||
Containers = state.ContainerListState.Containers.Add(action.NewContainer)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[ReducerMethod]
|
||||
public static RootState RemoveContainerFromList(RootState state,
|
||||
RemoveContainerFromListAction action) {
|
||||
return state with {
|
||||
ContainerListState = state.ContainerListState with {
|
||||
Containers = state.ContainerListState.Containers
|
||||
.Where(c => c.BookmarkContainerId != action.ContainerIdToRemove)
|
||||
.ToImmutableList()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue