2021-12-05 23:50:48 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Fluxor;
|
|
|
|
|
using Blazored.LocalStorage;
|
|
|
|
|
using Start.Client.Store.Features.ContainersList;
|
|
|
|
|
using Start.Client.Store.Features.CurrentContainer;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Start.Client.Store.State {
|
|
|
|
|
public class StoreInitializedEffects {
|
|
|
|
|
public IState<RootState> State { get; init; }
|
|
|
|
|
public ILocalStorageService LocalStorage { get; init; }
|
|
|
|
|
|
|
|
|
|
public StoreInitializedEffects(IState<RootState> state, ILocalStorageService localStorage) {
|
|
|
|
|
this.State = state;
|
|
|
|
|
this.LocalStorage = localStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[EffectMethod(typeof(StoreInitializedAction))]
|
|
|
|
|
public async Task InitialLoad(IDispatcher dispatch) {
|
|
|
|
|
dispatch.Dispatch(new LoadContainerListAction());
|
|
|
|
|
dispatch.Dispatch(new LoadCurrentContainerAction(await GetSelectedContainerId()));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 16:36:42 +00:00
|
|
|
|
public async Task<int> GetSelectedContainerId() {
|
2021-12-05 23:50:48 +00:00
|
|
|
|
bool hasValue = await this.LocalStorage.ContainKeyAsync("SelectedContainer");
|
|
|
|
|
|
|
|
|
|
if (hasValue)
|
|
|
|
|
return await this.LocalStorage.GetItemAsync<int>("SelectedContainer");
|
|
|
|
|
|
|
|
|
|
// Default to the first container
|
2022-04-21 16:36:42 +00:00
|
|
|
|
int? firstContainer = this.State.Value.ContainerListState.Containers
|
|
|
|
|
.FirstOrDefault()?.BookmarkContainerId;
|
|
|
|
|
|
|
|
|
|
if (firstContainer == null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
await this.SetSelectedContainer(firstContainer ?? 0);
|
|
|
|
|
return firstContainer ?? 0;
|
2021-12-05 23:50:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 16:36:42 +00:00
|
|
|
|
public async Task SetSelectedContainer(int selectedContainerId) {
|
2021-12-05 23:50:48 +00:00
|
|
|
|
await this.LocalStorage.SetItemAsync("SelectedContainer", selectedContainerId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|