TourIAm/TIAMSharedUI/Pages/User/SysAdmins/Blog.razor

184 lines
5.0 KiB
Plaintext

@page "/sysadmin/blogs"
@using TIAMWebApp.Shared.Application.Models
@using TIAMWebApp.Shared.Application.Services
@using TIAMSharedUI.Shared
@layout AdminLayout
@inject BlogService BlogService
<div clss="container">
<h3 class="mb-4">Manage Blog Posts</h3>
<EditForm Model="@Post" OnValidSubmit="@SavePost">
<div class="mb-3">
<label class="form-label">Title</label>
<InputText class="form-control" @bind-Value="Post.Title" />
</div>
<div class="mb-3">
<label class="form-label">Lead</label>
<InputText class="form-control" @bind-Value="Post.Lead" />
</div>
<div class="mb-3">
<label class="form-label">Google Drive Link</label>
<InputText class="form-control" @bind-Value="Post.DriveLink" />
</div>
<div class="mb-3">
<label class="form-label">Tags (comma-separated)</label>
<InputText class="form-control" @bind-Value="TagsCsv" />
</div>
<div class="mb-3">
<label class="form-label">Cover Image (optional)</label>
<InputFile OnChange="@HandleFileSelected" />
</div>
<button class="btn btn-primary" type="submit" disabled="@IsSaving">
@if (IsSaving)
{
<span class="spinner-border spinner-border-sm"></span> @(IsEditMode ? "Updating..." : "Saving...")
}
else
{
<span>@(IsEditMode ? "Update Post" : "Save Post")</span>
}
</button>
@if (IsEditMode)
{
<button type="button" class="btn btn-secondary ms-2" @onclick="CancelEdit">Cancel</button>
}
</EditForm>
@if (SaveSuccess)
{
<div class="alert alert-success mt-3">Post saved successfully!</div>
}
<h4 class="mt-5">Existing Posts</h4>
@if (Posts.Count == 0)
{
<p>No blog posts found.</p>
}
else
{
<table class="table table-bordered mt-3">
<thead>
<tr>
<th>Title</th>
<th>Tags</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var item in Posts)
{
<tr>
<td><img src="@item.CoverImage" style="max-height:150px" /></td>
<td>@item.Title</td>
<td>@string.Join(", ", item.Tags)</td>
<td>
<button class="btn btn-sm btn-primary me-2" @onclick="() => EditPost(item)">Edit</button>
<button class="btn btn-sm btn-danger" @onclick="() => DeletePost(item.Id)">Delete</button>
</td>
</tr>
}
</tbody>
</table>
}
</div>
@code {
private BlogPostMetadata Post = new();
private List<BlogPostMetadata> Posts = new();
private IBrowserFile? CoverImage;
private string TagsCsv = "";
private bool IsSaving = false;
private bool SaveSuccess = false;
private bool IsEditMode = false;
protected override async Task OnInitializedAsync()
{
await LoadPosts();
}
private async Task LoadPosts()
{
Posts = await BlogService.GetAllPostsAsync();
}
private async Task HandleFileSelected(InputFileChangeEventArgs e)
{
CoverImage = e.File;
}
private async Task SavePost()
{
IsSaving = true;
SaveSuccess = false;
Post.Tags = TagsCsv.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList();
if (IsEditMode)
{
await BlogService.UpdatePostAsync(Post, CoverImage);
}
else
{
await BlogService.CreatePostAsync(Post, CoverImage);
}
IsSaving = false;
SaveSuccess = true;
await LoadPosts();
ResetForm();
}
private void EditPost(BlogPostMetadata post)
{
Post = new BlogPostMetadata
{
Id = post.Id,
Title = post.Title,
Lead = post.Lead,
DriveLink = post.DriveLink,
Tags = new List<string>(post.Tags),
CoverImage = post.CoverImage
};
TagsCsv = string.Join(", ", post.Tags);
IsEditMode = true;
SaveSuccess = false;
}
private async Task DeletePost(string id)
{
if (await ConfirmDeleteAsync())
{
await BlogService.DeletePostAsync(id);
await LoadPosts();
}
}
private async Task<bool> ConfirmDeleteAsync()
{
return await Task.FromResult(true); // Replace with real confirmation popup later
}
private void CancelEdit()
{
ResetForm();
}
private void ResetForm()
{
Post = new BlogPostMetadata();
TagsCsv = "";
CoverImage = null;
IsEditMode = false;
}
}