diff --git a/Start/Client/Store/Features/ContainersList/ContainerListEffects.cs b/Start/Client/Store/Features/ContainersList/ContainerListEffects.cs index b534a3e..e46898e 100644 --- a/Start/Client/Store/Features/ContainersList/ContainerListEffects.cs +++ b/Start/Client/Store/Features/ContainersList/ContainerListEffects.cs @@ -2,19 +2,25 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Blazored.LocalStorage; using Fluxor; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Refit; using Start.Client.Store.Features.CreateContainer; +using Start.Client.Store.Features.CurrentContainer; +using Start.Client.Store.State; using Start.Shared; using Start.Shared.Api; namespace Start.Client.Store.Features.ContainersList { public class ContainerListEffects { public IBookmarkContainersApi BookmarkContainersApi { get; init; } + public ILocalStorageService LocalStorage { get; set; } - public ContainerListEffects(IBookmarkContainersApi bookmarkContainersApi) { + public ContainerListEffects(IBookmarkContainersApi bookmarkContainersApi, + ILocalStorageService localStorage) { this.BookmarkContainersApi = bookmarkContainersApi; + this.LocalStorage = localStorage; } [EffectMethod(typeof(LoadContainerListAction))] @@ -35,21 +41,22 @@ namespace Start.Client.Store.Features.ContainersList { } if (!bookmarkContainers.Any()) { - dispatch.Dispatch(new SubmitCreateContainerAction( - new BookmarkContainerDto("Default", 0))); - - // And load again - response = await this + ApiResponse? createResponse = await this .BookmarkContainersApi - .GetAllBookmarkContainers(); + .CreateBookmarkContainer("Default", 0); - bookmarkContainers = response.Content?.ToList(); + BookmarkContainerDto? newContainer = createResponse.Content; - if (bookmarkContainers == null) { + if (newContainer == null) { dispatch.Dispatch(new ErrorFetchingContainerListAction( - "Failed to fetch containers list")); + "Failed to create default container")); return; } + + bookmarkContainers = new List { newContainer }; + + await this.SetSelectedContainer(newContainer.BookmarkContainerId); + dispatch.Dispatch(new LoadCurrentContainerAction(newContainer.BookmarkContainerId)); } dispatch.Dispatch(new RecievedContainerListAction(bookmarkContainers)); @@ -58,5 +65,9 @@ namespace Start.Client.Store.Features.ContainersList { e.Redirect(); } } + + private async Task SetSelectedContainer(int selectedContainerId) { + await this.LocalStorage.SetItemAsync("SelectedContainer", selectedContainerId); + } } } diff --git a/Start/Client/Store/State/StoreInitializedEffects.cs b/Start/Client/Store/State/StoreInitializedEffects.cs index f3a5755..4bc2263 100644 --- a/Start/Client/Store/State/StoreInitializedEffects.cs +++ b/Start/Client/Store/State/StoreInitializedEffects.cs @@ -21,20 +21,24 @@ namespace Start.Client.Store.State { dispatch.Dispatch(new LoadCurrentContainerAction(await GetSelectedContainerId())); } - private async Task GetSelectedContainerId() { + public async Task GetSelectedContainerId() { bool hasValue = await this.LocalStorage.ContainKeyAsync("SelectedContainer"); if (hasValue) return await this.LocalStorage.GetItemAsync("SelectedContainer"); // Default to the first container - int firstContainer = this.State.Value.ContainerListState.Containers - .First().BookmarkContainerId; - await this.SetSelectedContainer(firstContainer); - return firstContainer; + int? firstContainer = this.State.Value.ContainerListState.Containers + .FirstOrDefault()?.BookmarkContainerId; + + if (firstContainer == null) + return 0; + + await this.SetSelectedContainer(firstContainer ?? 0); + return firstContainer ?? 0; } - protected async Task SetSelectedContainer(int selectedContainerId) { + public async Task SetSelectedContainer(int selectedContainerId) { await this.LocalStorage.SetItemAsync("SelectedContainer", selectedContainerId); } }