Skip to content

Commit

Permalink
Tri adding folder edit button
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxocube committed Jan 7, 2025
1 parent 5d02d49 commit 77cbdbc
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MediaFeeder.Components.Account.Pages.Manage;
using MediaFeeder.Data.db;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Identity;
Expand Down
33 changes: 33 additions & 0 deletions MediaFeeder/MediaFeeder/Components/Dialogs/EditFolder.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@inherits FeedbackComponent<MediaFeeder.Data.db.Folder>

<Form Model="@Options"
LabelCol="new ColLayoutParam { Span = 8 }"
WrapperCol="new ColLayoutParam { Span = 16 }"
ValidateOnChange="true"
OnFinish="OnFinish">
<FormItem Label="Username" Required ShowFeedbackOnError="true">
<Input @bind-Value="@context.Name" required="required" />
</FormItem>
<FormItem Label="Parent" ShowFeedbackOnError="true">
<TreeSelect
DataSource="@ExistingFolders"
@bind-Value="@context.Parent"
Placeholder="Please select"
ItemValue="static item => item"
ItemLabel="static item => item.Name"
AllowClear
EnableSearch
MatchedStyle="font-weight: bold"
ChildrenExpression="static node => node.DataItem.Subfolders"
TitleExpression="static node => node.DataItem.Name"
TitleTemplate="static node => node.DataItem.Name.ToRenderFragment()"
KeyExpression="static node => node.DataItem.Id.ToString()"
IsLeafExpression="static node => node.DataItem.Subfolders.Count == 0"
/>
</FormItem>
<FormItem WrapperCol="new ColLayoutParam{ Offset = 8, Span = 16 }">
<Button Type="@ButtonType.Primary" HtmlType="submit">
Submit
</Button>
</FormItem>
</Form>
47 changes: 47 additions & 0 deletions MediaFeeder/MediaFeeder/Components/Dialogs/EditFolder.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using MediaFeeder.Data;
using MediaFeeder.Data.db;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace MediaFeeder.Components.Dialogs;

public partial class EditFolder
{
[Inject] public required MediaFeederDataContext Context { get; set; }
[Inject] public required AuthenticationStateProvider AuthenticationStateProvider { get; init; }
[Inject] public required UserManager<AuthUser> UserManager { get; set; }
private List<Folder> ExistingFolders { get; set; } = new();

protected override async Task OnInitializedAsync()
{
if (Options == null)
{
Options = Context.Folders.CreateProxy();

var auth = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = await UserManager.GetUserAsync(auth.User);

ArgumentNullException.ThrowIfNull(user);

Options.User = user;
}
else
{
Context.Attach(Options);
}

ExistingFolders = await Context.Folders.Where(f => f != Options).ToListAsync();

await base.OnInitializedAsync();
}

private void OnFinish(EditContext editContext)
{
Context.SaveChanges();

_ = FeedbackRef.CloseAsync();
}
}
7 changes: 4 additions & 3 deletions MediaFeeder/MediaFeeder/Components/Tree/TreeView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
</Tooltip>

<Tooltip Title="Add Folder">
<Button Icon="@(IconType.Outline.FolderAdd)"></Button>
<Button Icon="@(IconType.Outline.FolderAdd)" OnClick="() => EditFolder(null)"></Button>
</Tooltip>

<Tooltip Title="Import Subscriptions">
<Button Icon="@(IconType.Outline.Import)"></Button>
</Tooltip>

<Tooltip Title="Edit">
<Button Icon="@(IconType.Outline.Edit)"></Button>
<Button Icon="@(IconType.Outline.Edit)" OnClick="EditSelected"></Button>
</Tooltip>

<Tooltip Title="Delete">
Expand All @@ -33,7 +33,8 @@
ShowLeafIcon="true"
Selectable="true"
TItem="ITreeSelectable"
DefaultExpandAll="true">
DefaultExpandAll="true"
@ref="TreeRef">
@foreach (var folder in Folders)
{
<TreeFolder Folder="@folder"/>
Expand Down
69 changes: 67 additions & 2 deletions MediaFeeder/MediaFeeder/Components/Tree/TreeView.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MediaFeeder.Data;
using AntDesign;
using MediaFeeder.Components.Dialogs;
using MediaFeeder.Data;
using MediaFeeder.Data.db;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
Expand All @@ -17,9 +19,13 @@ public sealed partial class TreeView

[Inject] public required UserManager<AuthUser> UserManager { get; set; }

[Inject] public required ModalService ModalService { get; set; }


private List<Folder>? Folders { get; set; }

private SemaphoreSlim Updating { get; } = new(1);
public Tree<ITreeSelectable>? TreeRef { get; set; }

protected override async Task OnParametersSetAsync()
{
Expand Down Expand Up @@ -52,4 +58,63 @@ protected override async Task OnParametersSetAsync()

await base.OnParametersSetAsync();
}
}

private void EditFolder(Folder? folder)
{
var modalConfig = new ModalOptions();
ModalRef? modalRef = null;

modalConfig.Title = "Basic Form";
modalConfig.OnCancel = async _ =>
{
Console.WriteLine("OnCancel");
await modalRef?.CloseAsync();
};

modalConfig.AfterClose = () =>
{
Console.WriteLine("AfterClose");
InvokeAsync(StateHasChanged);
return Task.CompletedTask;
};

modalRef = ModalService.CreateModal<EditFolder, Folder>(modalConfig, folder);

modalRef.OnOpen = static () =>
{
Console.WriteLine("ModalRef OnOpen");
return Task.CompletedTask;
};

modalRef.OnOk = static () =>
{
Console.WriteLine("ModalRef OnOk");
return Task.CompletedTask;
};

modalRef.OnCancel = static () =>
{
Console.WriteLine("ModalRef OnCancel");
return Task.CompletedTask;
};

modalRef.OnClose = static () =>
{
Console.WriteLine("ModalRef OnClose");
return Task.CompletedTask;
};
}

private void EditSelected()
{
switch (TreeRef?.SelectedData)
{
case Folder folder:
EditFolder(folder);
break;
case Subscription subscription:
Console.WriteLine($"Would open subscription dialog for {subscription}");
break;
}
}
}

0 comments on commit 77cbdbc

Please sign in to comment.