Quick fixes for initial setup

This commit is contained in:
Neil Brommer 2022-04-21 09:36:42 -07:00
parent 45c8899f06
commit 2356fbab4f
2 changed files with 31 additions and 16 deletions

View file

@ -2,19 +2,25 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Blazored.LocalStorage;
using Fluxor; using Fluxor;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Refit; using Refit;
using Start.Client.Store.Features.CreateContainer; using Start.Client.Store.Features.CreateContainer;
using Start.Client.Store.Features.CurrentContainer;
using Start.Client.Store.State;
using Start.Shared; using Start.Shared;
using Start.Shared.Api; using Start.Shared.Api;
namespace Start.Client.Store.Features.ContainersList { namespace Start.Client.Store.Features.ContainersList {
public class ContainerListEffects { public class ContainerListEffects {
public IBookmarkContainersApi BookmarkContainersApi { get; init; } public IBookmarkContainersApi BookmarkContainersApi { get; init; }
public ILocalStorageService LocalStorage { get; set; }
public ContainerListEffects(IBookmarkContainersApi bookmarkContainersApi) { public ContainerListEffects(IBookmarkContainersApi bookmarkContainersApi,
ILocalStorageService localStorage) {
this.BookmarkContainersApi = bookmarkContainersApi; this.BookmarkContainersApi = bookmarkContainersApi;
this.LocalStorage = localStorage;
} }
[EffectMethod(typeof(LoadContainerListAction))] [EffectMethod(typeof(LoadContainerListAction))]
@ -35,21 +41,22 @@ namespace Start.Client.Store.Features.ContainersList {
} }
if (!bookmarkContainers.Any()) { if (!bookmarkContainers.Any()) {
dispatch.Dispatch(new SubmitCreateContainerAction( ApiResponse<BookmarkContainerDto?>? createResponse = await this
new BookmarkContainerDto("Default", 0)));
// And load again
response = await this
.BookmarkContainersApi .BookmarkContainersApi
.GetAllBookmarkContainers(); .CreateBookmarkContainer("Default", 0);
bookmarkContainers = response.Content?.ToList(); BookmarkContainerDto? newContainer = createResponse.Content;
if (bookmarkContainers == null) { if (newContainer == null) {
dispatch.Dispatch(new ErrorFetchingContainerListAction( dispatch.Dispatch(new ErrorFetchingContainerListAction(
"Failed to fetch containers list")); "Failed to create default container"));
return; return;
} }
bookmarkContainers = new List<BookmarkContainerDto> { newContainer };
await this.SetSelectedContainer(newContainer.BookmarkContainerId);
dispatch.Dispatch(new LoadCurrentContainerAction(newContainer.BookmarkContainerId));
} }
dispatch.Dispatch(new RecievedContainerListAction(bookmarkContainers)); dispatch.Dispatch(new RecievedContainerListAction(bookmarkContainers));
@ -58,5 +65,9 @@ namespace Start.Client.Store.Features.ContainersList {
e.Redirect(); e.Redirect();
} }
} }
private async Task SetSelectedContainer(int selectedContainerId) {
await this.LocalStorage.SetItemAsync("SelectedContainer", selectedContainerId);
}
} }
} }

View file

@ -21,20 +21,24 @@ namespace Start.Client.Store.State {
dispatch.Dispatch(new LoadCurrentContainerAction(await GetSelectedContainerId())); dispatch.Dispatch(new LoadCurrentContainerAction(await GetSelectedContainerId()));
} }
private async Task<int> GetSelectedContainerId() { public async Task<int> GetSelectedContainerId() {
bool hasValue = await this.LocalStorage.ContainKeyAsync("SelectedContainer"); bool hasValue = await this.LocalStorage.ContainKeyAsync("SelectedContainer");
if (hasValue) if (hasValue)
return await this.LocalStorage.GetItemAsync<int>("SelectedContainer"); return await this.LocalStorage.GetItemAsync<int>("SelectedContainer");
// Default to the first container // Default to the first container
int firstContainer = this.State.Value.ContainerListState.Containers int? firstContainer = this.State.Value.ContainerListState.Containers
.First().BookmarkContainerId; .FirstOrDefault()?.BookmarkContainerId;
await this.SetSelectedContainer(firstContainer);
return firstContainer; 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); await this.LocalStorage.SetItemAsync("SelectedContainer", selectedContainerId);
} }
} }