BlazorStart/Start/Client/Pages/StartPage.razor
2021-11-22 12:52:41 -08:00

201 lines
6.7 KiB
Plaintext

@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)
{
<div class="empty">
<div class="empty-icon">
<div class="loading loading-lg"></div>
</div>
<p class="empty-title h5">Loading Containers</p>
</div>
}
else
{
<ul class="containerList tab">
@foreach (BookmarkContainerDto container in this.bookmarkContainers)
{
string itemClasses = "tab-item";
if (container.BookmarkContainerId == this.selectedBookmarkContainer?.BookmarkContainerId)
itemClasses += " active";
<li class="@itemClasses">
<a @onclick="() => OnContainerSelected(container.BookmarkContainerId)">
@container.Title
<button class="btn btn-clear"
@onclick="() => this.OnDeleteContainerClicked(container.BookmarkContainerId)">
</button>
</a>
</li>
}
<li class="tab-item tab-action">
<button @onclick="OnCreateContainerClicked" class="btn btn-link"
title="Create New Container" aria-label="Create New Container">
+
</button>
</li>
</ul>
<div class="activeBookmarkContainer">
@if (this.selectedBookmarkContainer == null)
{
<div class="empty">
<div class="empty-icon">
<div class="loading loading-icon"></div>
</div>
<p class="empty-title h5">Loading Bookmarks</p>
</div>
<p class="text-center">Loading Bookmarks</p>
}
else if (!this.selectedBookmarkContainer.BookmarkGroups?.Any() ?? true)
{
<div class="empty">
<div class="empty-icon">
<i class="icon icon-3x icon-bookmark"></i>
</div>
<p class="empty-title h5">No Bookmark Groups</p>
</div>
}
else
{
@foreach (BookmarkGroupDto group in this.selectedBookmarkContainer.BookmarkGroups!)
{
<BookmarkGroup Group="group" />
}
}
</div>
<CreateContainer IsOpen="showCreateContainerForm" OnClose="this.OnCloseCreateContainer"
OnCreated="this.OnContainerCreated" />
}
@code
{
private IList<BookmarkContainerDto>? bookmarkContainers;
private BookmarkContainerDto? selectedBookmarkContainer;
private bool showCreateContainerForm = false;
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<IList<BookmarkContainerDto>>(
"Bookmarks/GetAllBookmarkContainers");
if (this.bookmarkContainers == null || !this.bookmarkContainers.Any()) {
HttpResponseMessage response = await Http
.PostAsJsonAsync("Bookmarks/CreateBookmarkContainer", "Default");
BookmarkContainerDto? container = await response
.RequestMessage
!.Content
!.ReadFromJsonAsync<BookmarkContainerDto?>();
}
await this.OnContainerSelected(await this.GetSelectedContainerId());
}
catch (AccessTokenNotAvailableException e) {
e.Redirect();
}
}
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<BookmarkContainerDto?>(
$"Bookmarks/GetBookmarkContainer/{bookmarkContainerId}");
await this.SetSelectedContainer(bookmarkContainerId);
this.selectedBookmarkContainer = bookmarkContainer;
}
catch (AccessTokenNotAvailableException e)
{
e.Redirect();
}
}
protected async Task OnDeleteContainerClicked(int bookmarkContainerId)
{
try
{
HttpResponseMessage result = await Http
.DeleteAsync($"Bookmarks/DeleteBookmarkContainer/{bookmarkContainerId}");
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
if (await this.GetSelectedContainerId() == bookmarkContainerId)
await this.OnContainerSelected(
this.bookmarkContainers?.First().BookmarkContainerId
?? bookmarkContainerId);
this.bookmarkContainers = this.bookmarkContainers
?.Where(bc => bc.BookmarkContainerId != bookmarkContainerId)
.ToList();
}
}
catch (AccessTokenNotAvailableException e)
{
e.Redirect();
}
}
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);
}
// Save the currently selected container in LocalStorage so that the same container remains
// selected between new tabs
protected async Task<int> GetSelectedContainerId()
{
bool hasValue = await localStorage.ContainKeyAsync("SelectedContainer");
if (hasValue)
return await localStorage.GetItemAsync<int>("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<int>("SelectedContainer", selectedContainerId);
}
}