SeemGen/Components/Partials/ContentItemList.razor

149 lines
4.7 KiB
Plaintext

@using BLAIzor.Models
@using BLAIzor.Services
@inject ContentEditorService contentEditorService
@inject DialogService dialogService
@inject NotificationService notificationService
<a style="width:100%" @onclick="ToggleVisibility">
<div class="mb-2 p-3 reference-button bg-panel">
<div class="text-content">
<strong>Content count: @contentItems.Count</strong><br />
<small class="text-muted">See all</small>
</div>
<div class="icon-buttons">
<div class="icon-circle"><i class="fa-solid fa-arrow-down text-white"></i></div>
</div>
</div>
</a>
@if (isVisible)
{
<ul class="list-group content-item-list mb-3">
<li class="list-group-item">
<button class="pointer bg-transparent border-0" style="width:100%; text-align: left; " @onclick="CreateNewItem">
<div class="px-3 py-2 reference-button bg-panel-gradient-highlight pointer">
<div class="text-content">
<strong>Add new content</strong>
<br />
<small class="text-muted">Add a new content to this group</small>
</div>
<div class="icon-buttons">
<div class="icon-circle"><i class="fa-solid fa-plus text-white"></i></div>
</div>
</div>
</button>
</li>
@* <li><button class="btn btn-sm btn-primary ms-2" @onclick="CreateNewItem">Add New Item</button></li> *@
@foreach (var item in contentItems)
{
<li class="list-group-item">
<div class="p-2 reference-button">
<div class="text-content text-start">
<strong>@item.Title</strong>
<br />
<small class="text-muted">@item.Language - @item.Tags</small>
</div>
<div class="icon-buttons text-end">
<button class="btn btn-sm btn-outline-primary" @onclick="() => EditItem(item.Id)">Edit</button>
<button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteItem(item.Id)">Delete</button>
@* <div class="icon-circle">V</div> *@
</div>
</div>
</li>
}
</ul>
}
@code {
[Parameter] public int ContentGroupId { get; set; }
[Parameter] public Func<string, int, Task> OnManageContentItemClicked { get; set; }
private List<ContentItem> contentItems = new();
private bool isVisible = false;
protected override async Task OnInitializedAsync()
{
contentItems = await contentEditorService.GetContentItemsByGroupIdAsync(ContentGroupId);
}
private void ToggleVisibility()
{
isVisible = !isVisible;
}
private async Task CreateNewItem()
{
var newItem = new ContentItem
{
ContentGroupId = ContentGroupId,
Title = "New Item",
Description = "",
Content = "",
Language = "en",
Tags = "",
IsPublished = false,
CreatedAt = DateTime.UtcNow,
LastUpdated = DateTime.UtcNow,
Version = 1
};
var result = await contentEditorService.CreateContentItemAsync(newItem);
if (result != null)
{
contentItems.Insert(0, result);
}
}
private async Task EditItem(int itemId)
{
if (OnManageContentItemClicked != null)
await OnManageContentItemClicked.Invoke("EditContentItem", itemId);
}
private async Task DeleteItem(int itemId)
{
var confirmationResult = await dialogService.Confirm("Are you sure?", "Delete content", new ConfirmOptions() { OkButtonText = "Delete", CancelButtonText = "Oops, no" });
if (confirmationResult == true)
{
await ReallyDeleteItem(itemId);
}
else
{
//do nothing?
}
}
private async Task ReallyDeleteItem(int itemId)
{
var result = await contentEditorService.DeleteContentItemByIdAsync(itemId);
if (result)
{
var toRemove = contentItems.FirstOrDefault(c => c.Id == itemId);
if (toRemove != null)
{
contentItems.Remove(toRemove);
}
var message = NotificationHelper.CreateNotificationMessage("Group deleted", 2, "Success", "ContentGroup deleted");
ShowNotification(message);
StateHasChanged();
}
}
public void ShowNotification(NotificationMessage message)
{
notificationService.Notify(message);
}
}