Finish delete container process
This commit is contained in:
		
							parent
							
								
									c53d7b8ce3
								
							
						
					
					
						commit
						b8b23abffc
					
				
							
								
								
									
										63
									
								
								Start/Client/Components/DeleteContainer.razor
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								Start/Client/Components/DeleteContainer.razor
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,63 @@
 | 
				
			||||||
 | 
					@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
 | 
				
			||||||
 | 
					@inject HttpClient Http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@{ string title = $"Delete Container \"{this.ContainerTitle}\""; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<Dialog Title="@title" Active="this.Active">
 | 
				
			||||||
 | 
					    @if (this.ShowAlert)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        <div class="toast toast-error">
 | 
				
			||||||
 | 
					            There was an error deleting the bookmark container
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    <p>Are you sure you want to delete the bookmark container "@this.ContainerTitle"?</p>
 | 
				
			||||||
 | 
					    <div class="text-right">
 | 
				
			||||||
 | 
					        <button class="btn" @onclick="this.OnDialogClose">Cancel</button>
 | 
				
			||||||
 | 
					        <button class="btn btn-error" @onclick="this.OnConfirmDelete">Delete</button>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					</Dialog>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@code {
 | 
				
			||||||
 | 
					    [Parameter]
 | 
				
			||||||
 | 
					    public int BookmarkContainerId { get; set; }
 | 
				
			||||||
 | 
					    [Parameter]
 | 
				
			||||||
 | 
					    public string ContainerTitle { get; set; } = null!;
 | 
				
			||||||
 | 
					    [Parameter]
 | 
				
			||||||
 | 
					    public bool Active { get; set; }
 | 
				
			||||||
 | 
					    [Parameter]
 | 
				
			||||||
 | 
					    public EventCallback<int> OnDeleted { get; set; }
 | 
				
			||||||
 | 
					    [Parameter]
 | 
				
			||||||
 | 
					    public EventCallback OnClose { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public bool ShowAlert { get; set; } = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public async Task OnDialogClose()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        this.Active = false;
 | 
				
			||||||
 | 
					        await this.OnClose.InvokeAsync();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public async Task OnConfirmDelete()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        try
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            HttpResponseMessage result = await Http
 | 
				
			||||||
 | 
					                .DeleteAsync($"Bookmarks/DeleteBookmarkContainer/{this.BookmarkContainerId}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (result.StatusCode == System.Net.HttpStatusCode.OK)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                await this.OnDeleted.InvokeAsync(BookmarkContainerId);
 | 
				
			||||||
 | 
					                this.ShowAlert = false;
 | 
				
			||||||
 | 
					                this.Active = false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                this.ShowAlert = true;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        catch (AccessTokenNotAvailableException e)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            e.Redirect();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -73,13 +73,22 @@ else
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <CreateContainer IsOpen="showCreateContainerForm" OnClose="this.OnCloseCreateContainer"
 | 
					    <CreateContainer IsOpen="showCreateContainerForm" OnClose="this.OnCloseCreateContainer"
 | 
				
			||||||
                     OnCreated="this.OnContainerCreated" />
 | 
					                     OnCreated="this.OnContainerCreated" />
 | 
				
			||||||
 | 
					    <DeleteContainer Active="this.showDeleteContainerForm" OnClose="this.OnCloseDeleteContainer"
 | 
				
			||||||
 | 
					                     BookmarkContainerId="this.bookmarkContainerToDelete?.BookmarkContainerId ?? 0"
 | 
				
			||||||
 | 
					                     ContainerTitle="@(this.bookmarkContainerToDelete?.Title ?? "")"
 | 
				
			||||||
 | 
					                     OnDeleted="this.OnContainerDeleted" />
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@code
 | 
					@code
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    private IList<BookmarkContainerDto>? bookmarkContainers;
 | 
					    private IList<BookmarkContainerDto>? bookmarkContainers;
 | 
				
			||||||
    private BookmarkContainerDto? selectedBookmarkContainer;
 | 
					    private BookmarkContainerDto? selectedBookmarkContainer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private bool showCreateContainerForm = false;
 | 
					    private bool showCreateContainerForm = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private bool showDeleteContainerForm = false;
 | 
				
			||||||
 | 
					    private BookmarkContainerDto? bookmarkContainerToDelete;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private bool showCreateGroupForm = false;
 | 
					    private bool showCreateGroupForm = false;
 | 
				
			||||||
    private bool showCreateBookmarkForm = false;
 | 
					    private bool showCreateBookmarkForm = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -95,14 +104,9 @@ else
 | 
				
			||||||
                .GetFromJsonAsync<IList<BookmarkContainerDto>>(
 | 
					                .GetFromJsonAsync<IList<BookmarkContainerDto>>(
 | 
				
			||||||
                    "Bookmarks/GetAllBookmarkContainers");
 | 
					                    "Bookmarks/GetAllBookmarkContainers");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (this.bookmarkContainers == null || !this.bookmarkContainers.Any()) {
 | 
					            if (this.bookmarkContainers == null || !this.bookmarkContainers.Any())
 | 
				
			||||||
                HttpResponseMessage response = await Http
 | 
					            {
 | 
				
			||||||
                    .PostAsJsonAsync("Bookmarks/CreateBookmarkContainer", "Default");
 | 
					                await this.CreateDefaultContainer();
 | 
				
			||||||
 | 
					 | 
				
			||||||
                BookmarkContainerDto? container = await response
 | 
					 | 
				
			||||||
                    .RequestMessage
 | 
					 | 
				
			||||||
                    !.Content
 | 
					 | 
				
			||||||
                    !.ReadFromJsonAsync<BookmarkContainerDto?>();
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            await this.OnContainerSelected(await this.GetSelectedContainerId());
 | 
					            await this.OnContainerSelected(await this.GetSelectedContainerId());
 | 
				
			||||||
| 
						 | 
					@ -112,6 +116,20 @@ else
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected async Task CreateDefaultContainer()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        HttpResponseMessage response = await Http
 | 
				
			||||||
 | 
					            .PostAsJsonAsync("Bookmarks/CreateBookmarkContainer", "Default");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        BookmarkContainerDto? container = await response
 | 
				
			||||||
 | 
					            .RequestMessage
 | 
				
			||||||
 | 
					            !.Content
 | 
				
			||||||
 | 
					            !.ReadFromJsonAsync<BookmarkContainerDto?>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (container != null)
 | 
				
			||||||
 | 
					            await this.OnContainerSelected(container.BookmarkContainerId);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected async Task OnContainerSelected(int bookmarkContainerId)
 | 
					    protected async Task OnContainerSelected(int bookmarkContainerId)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        try
 | 
					        try
 | 
				
			||||||
| 
						 | 
					@ -132,29 +150,11 @@ else
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected async Task OnDeleteContainerClicked(int bookmarkContainerId)
 | 
					    protected void OnDeleteContainerClicked(int bookmarkContainerId)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        try
 | 
					        this.bookmarkContainerToDelete = this.bookmarkContainers
 | 
				
			||||||
        {
 | 
					            ?.First(bc => bc.BookmarkContainerId == bookmarkContainerId);
 | 
				
			||||||
            HttpResponseMessage result = await Http
 | 
					        this.showDeleteContainerForm = true;
 | 
				
			||||||
                .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()
 | 
					    protected void OnCreateContainerClicked()
 | 
				
			||||||
| 
						 | 
					@ -177,6 +177,25 @@ else
 | 
				
			||||||
        await OnContainerSelected(newContainer.BookmarkContainerId);
 | 
					        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
 | 
					    // Save the currently selected container in LocalStorage so that the same container remains
 | 
				
			||||||
    // selected between new tabs
 | 
					    // selected between new tabs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue