@page "/Start"
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Start.Client.Components
@using Start.Shared
@attribute [Authorize]
@inject HttpClient Http
@inject Blazored.LocalStorage.ILocalStorageService localStorage
@if (bookmarkContainers == null)
{
}
else
{
@if (this.selectedBookmarkContainer == null)
{
Loading Bookmarks
}
else if (!this.selectedBookmarkContainer.BookmarkGroups?.Any() ?? true)
{
}
else
{
@foreach (BookmarkGroupDto group in this.selectedBookmarkContainer.BookmarkGroups!)
{
}
}
}
@code
{
private IList? bookmarkContainers;
private BookmarkContainerDto? selectedBookmarkContainer;
private bool showCreateContainerForm = false;
private bool showDeleteContainerForm = false;
private BookmarkContainerDto? bookmarkContainerToDelete;
private bool showCreateGroupForm = false;
private bool showCreateBookmarkForm = false;
protected override async Task OnInitializedAsync()
{
await LoadContainers();
}
protected async Task LoadContainers()
{
try {
this.bookmarkContainers = await Http
.GetFromJsonAsync>(
"Bookmarks/GetAllBookmarkContainers");
if (this.bookmarkContainers == null || !this.bookmarkContainers.Any())
{
await this.CreateDefaultContainer();
}
await this.OnContainerSelected(await this.GetSelectedContainerId());
}
catch (AccessTokenNotAvailableException e) {
e.Redirect();
}
}
protected async Task CreateDefaultContainer()
{
HttpResponseMessage response = await Http
.PostAsJsonAsync("Bookmarks/CreateBookmarkContainer", "Default");
BookmarkContainerDto? container = await response
.RequestMessage
!.Content
!.ReadFromJsonAsync();
if (container != null)
await this.OnContainerSelected(container.BookmarkContainerId);
}
protected async Task OnContainerSelected(int bookmarkContainerId)
{
try
{
if (!this.bookmarkContainers?.Any(bc => bc.BookmarkContainerId == bookmarkContainerId) ?? false)
bookmarkContainerId = this.bookmarkContainers?.First().BookmarkContainerId ?? bookmarkContainerId;
BookmarkContainerDto? bookmarkContainer = await Http
.GetFromJsonAsync(
$"Bookmarks/GetBookmarkContainer/{bookmarkContainerId}");
await this.SetSelectedContainer(bookmarkContainerId);
this.selectedBookmarkContainer = bookmarkContainer;
}
catch (AccessTokenNotAvailableException e)
{
e.Redirect();
}
}
protected void OnDeleteContainerClicked(int bookmarkContainerId)
{
this.bookmarkContainerToDelete = this.bookmarkContainers
?.First(bc => bc.BookmarkContainerId == bookmarkContainerId);
this.showDeleteContainerForm = true;
}
protected void OnCreateContainerClicked()
{
this.showCreateContainerForm = true;
}
protected void OnCloseCreateContainer()
{
this.showCreateContainerForm = false;
}
protected async Task OnContainerCreated(BookmarkContainerDto newContainer)
{
if (this.bookmarkContainers == null)
return;
this.bookmarkContainers.Add(newContainer);
this.showCreateContainerForm = false;
await OnContainerSelected(newContainer.BookmarkContainerId);
}
protected void OnCloseDeleteContainer()
{
this.showDeleteContainerForm = false;
}
protected async Task OnContainerDeleted(int bookmarkContainerId)
{
if (!this.bookmarkContainers?.Any(bc => bc.BookmarkContainerId != bookmarkContainerId) ?? false)
await this.CreateDefaultContainer();
if (await this.GetSelectedContainerId() == bookmarkContainerId)
await this.OnContainerSelected(
this.bookmarkContainers?.First().BookmarkContainerId ?? bookmarkContainerId);
this.bookmarkContainers = this.bookmarkContainers
?.Where(bc => bc.BookmarkContainerId != bookmarkContainerId)
.ToList();
}
// Save the currently selected container in LocalStorage so that the same container remains
// selected between new tabs
protected async Task GetSelectedContainerId()
{
bool hasValue = await localStorage.ContainKeyAsync("SelectedContainer");
if (hasValue)
return await localStorage.GetItemAsync("SelectedContainer");
// Default to the first container
int firstContainer = this.bookmarkContainers!.First().BookmarkContainerId;
await this.SetSelectedContainer(firstContainer);
return firstContainer;
}
protected async Task SetSelectedContainer(int selectedContainerId)
{
await localStorage.SetItemAsync("SelectedContainer", selectedContainerId);
}
}