SeemGen/Components/Pages/ManageContentGroups.razor

128 lines
3.8 KiB
Plaintext

@page "/site/{SiteInfoId:int}/content-groups"
@using BLAIzor.Components.Layout
@using BLAIzor.Models
@using BLAIzor.Services
@using Microsoft.AspNetCore.Components.Authorization
@layout AdminLayout
@inject ContentEditorService contentEditorService
@inject NavigationManager Navigation
@attribute [Authorize]
@if (isLoading)
{
<p>Loading content groups...</p>
}
else
{
@foreach (var group in groups)
{
if (selectedGroupId == group.Id)
{
<EditForm Model="@group" OnValidSubmit="() => SaveGroup(group)">
<div class="border rounded p-3 mb-3">
<label>
Name
<InputText class="form-control my-1" @bind-Value="group.Name" />
</label>
<label>
Type
<InputText class="form-control my-1" @bind-Value="group.Type" />
</label>
<div class="d-flex justify-content-between mt-2">
<button class="btn btn-success" type="submit">Save</button>
<button class="btn btn-danger" type="button" @onclick="() => DeleteGroup(group)">Delete</button>
<button class="btn btn-secondary" type="button" @onclick="() => DeselectGroup()">Cancel</button>
</div>
</div>
</EditForm>
}
else
{
<div class="border rounded p-3 mb-3 d-flex justify-content-between align-items-center">
<div>
<strong>@group.Name</strong><br />
<small class="text-muted">@group.Type</small>
</div>
<button class="btn btn-outline-primary" @onclick="() => SelectGroup(group.Id)">Edit</button>
</div>
}
}
<button class="btn btn-primary mt-3" @onclick="AddNewGroup">Add New Group</button>
}
@code {
[Parameter]
public int SiteInfoId { get; set; }
private List<ContentGroup> groups = new();
private bool isLoading = true;
private int? selectedGroupId = null;
private void SelectGroup(int groupId)
{
selectedGroupId = groupId;
}
private void DeselectGroup()
{
selectedGroupId = null;
}
protected override async Task OnInitializedAsync()
{
isLoading = true;
groups = (await contentEditorService.GetContentGroupsBySiteInfoIdAsync(SiteInfoId))
.Where(g => g.SiteInfoId == SiteInfoId)
.OrderByDescending(g => g.LastUpdated)
.ToList();
isLoading = false;
}
private void EditGroup(ContentGroup group)
{
Navigation.NavigateTo($"/content-group/{group.Id}/items");
}
private async Task SaveGroup(ContentGroup group)
{
group.LastUpdated = DateTime.UtcNow;
group.Version = group.Version + 1;
var result = await contentEditorService.UpdateContentGroupByIdAsync(group);
if(result != null) ;
}
private async Task DeleteGroup(ContentGroup group)
{
var result = await contentEditorService.DeleteContentGroupByIdAsync(group.Id); ;
groups.Remove(group);
}
private async Task AddNewGroup()
{
var newGroup = new ContentGroup
{
SiteInfoId = SiteInfoId,
Name = "New Group",
Slug = $"group-{DateTime.UtcNow.Ticks}",
Type = "manual",
VectorSize = 384,
EmbeddingModel = "default",
CreatedAt = DateTime.UtcNow,
LastUpdated = DateTime.UtcNow,
Version = 1
};
var result = await contentEditorService.CreateContentGroupAsync(newGroup);
if(result!=null)
{
groups.Insert(0, newGroup);
}
}
}