@page "/edit-template/{TemplateId:int}"
@using BLAIzor.Components.Layout
@using BLAIzor.Models
@using BLAIzor.Services
@using Newtonsoft.Json
@using BLAIzor.Components.Partials
@attribute [Authorize]
@layout AdminLayout
@inject DesignTemplateService DesignTemplateService
@inject CssTemplateService CssTemplateService
@inject NavigationManager NavigationManager
@inject QDrantService QDrantService
@inject HtmlSnippetProcessor HtmlSnippetProcessor
@inject IJSRuntime JSRuntime
Edit Design Template
@if (isLoading)
{
Loading...
}
else
{
@{
foreach (var snippet in SnippetList)
{
}
}
}
@code {
[Parameter]
public int TemplateId { get; set; }
private bool isLoading = true;
private DesignTemplate currentTemplate = new();
private CssTemplate currentCssTemplate = new();
private bool hasCss = false;
private bool hasCollection = false;
private List SnippetList = [];
private string SessionId;
private static readonly Dictionary _instances = new();
protected override async Task OnInitializedAsync()
{
SessionId = Guid.NewGuid().ToString();
_instances[SessionId] = this;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("setSessionId", SessionId);
await JSRuntime.InvokeVoidAsync("setHtmlEditorSourceMode");
await LoadTemplate();
//var cssPath = await CssTemplateService.SaveTempCssFileAsync(currentCssTemplate.CssContent, SessionId);
//await JSRuntime.InvokeVoidAsync("seemgen.injectCssFile", cssPath);
}
}
private async Task LoadTemplate()
{
isLoading = true;
// Load the design template and its associated CSS template from the database
currentTemplate = await DesignTemplateService.GetByIdAsync(TemplateId);
currentCssTemplate = await CssTemplateService.GetByDesignTemplateIdAsync(TemplateId);
var currentCollection = await QDrantService.GetCollectionByNameAsync(currentTemplate.QDrandCollectionName);
Console.Write(currentCollection);
if (currentCssTemplate == null)
{
hasCss = false;
currentCssTemplate = new();
currentCssTemplate.DesignTemplateId = TemplateId;
}
else
{
hasCss = true;
}
if (!string.IsNullOrEmpty(currentCollection))
{
hasCollection = true;
var collectionCount = await QDrantService.GetCollectionCount(currentTemplate.QDrandCollectionName);
for (int i = 1; i <= collectionCount; i++)
{
var snippet = await QDrantService.GetSnippetAsync(i, currentTemplate.QDrandCollectionName);
var selectedPoint = JsonConvert.DeserializeObject(snippet)!;
Console.Write($"Id: {selectedPoint.result.id}, html: {selectedPoint.result.payload.Html}");
SnippetList.Add(selectedPoint.result.payload);
}
}
else
{
//let's create a collection
var result = QDrantService.CreateQdrantCollectionAsync(currentTemplate.QDrandCollectionName);
}
isLoading = false;
await InvokeAsync(StateHasChanged);
}
private async Task SaveTemplate()
{
isLoading = true;
currentCssTemplate.CssContent = currentCssTemplate.CssContent.Trim();
if (hasCss)
{
// Update the CSS template content
currentCssTemplate.LastUpdated = DateTime.UtcNow;
await CssTemplateService.UpdateAsync(currentCssTemplate);
}
else
{
// Create the CSS template content
currentCssTemplate.LastUpdated = DateTime.UtcNow;
await CssTemplateService.CreateAsync(currentCssTemplate);
}
// Save the CSS template
// Save the design template
currentTemplate.UpdatedAt = DateTime.Now;
await DesignTemplateService.UpdateAsync(currentTemplate);
isLoading = false;
// Redirect to the template manager page after saving
NavigationManager.NavigateTo("/design-templates");
}
private async Task DeleteTemplate()
{
isLoading = true;
// Delete the CSS template first
await CssTemplateService.DeleteAsync(currentCssTemplate.Id);
// Delete the design template
await DesignTemplateService.DeleteAsync(currentTemplate.Id);
isLoading = false;
// Redirect to the template manager page after deletion
NavigationManager.NavigateTo("/design-templates");
}
private void UpdateSnippet(HtmlSnippet updatedItem)
{
var item = SnippetList.FirstOrDefault(i => i.Name == updatedItem.Name);
if (item != null)
{
item.Html = updatedItem.Html;
}
}
private async Task SaveSnippets()
{
//I want to update all items without gaps in the ids always 1 to x
for (int i = 1; i <= SnippetList.Count(); i++)
{
SnippetList[i - 1].Id = i;
}
await HtmlSnippetProcessor.ProcessAndStoreTemplateSnippetAsync(currentTemplate.QDrandCollectionName, SnippetList);
}
async Task OnPaste(HtmlEditorPasteEventArgs args)
{
// await OnContentUpdated.InvokeAsync(MenuItem);
}
async Task OnChange(string html)
{
}
async Task OnInput(string html)
{
}
void OnExecute(HtmlEditorExecuteEventArgs args)
{
}
void OnUploadComplete(UploadCompleteEventArgs args)
{
}
public async Task AddSnippet()
{
SnippetList.Add(new HtmlSnippet());
}
}