LANCommander/LANCommander.Server/UI/Pages/Metadata/Platforms/Index.razor
2025-12-05 16:28:39 -06:00

131 lines
4.3 KiB
Text

@page "/Metadata/Platforms"
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject PlatformService PlatformService
@inject IMessageService MessageService
@inject ILogger<Index> Logger
<PageHeader Title="Platforms" />
<DataTable
TItem="Platform"
@ref="Table"
Size="@TableSize.Small"
Searchable
SearchProperty="p => p.Name">
<RightToolbar>
<Button OnClick="() => OpenEdit(null)" Type="@ButtonType.Primary">Add Platform</Button>
</RightToolbar>
<Columns>
<BoundDataColumn Property="p => p.Name" Sortable DefaultSortOrder="SortDirection.Ascending" />
<BoundDataColumn Property="p => p.Games.Count" Sortable Title="Games" Include="Games" />
<BoundDataColumn Property="p => p.CreatedOn" Title="Created On" Sortable>
<LocalTime Value="context.UpdatedOn" />
</BoundDataColumn>
<BoundDataColumn Property="p => p.CreatedBy != null ? p.CreatedBy.UserName : String.Empty" Title="Created By" Sortable Include="CreatedBy" />
<BoundDataColumn Property="p => p.UpdatedOn" Title="Updated On" Sortable>
<LocalTime Value="context.UpdatedOn" />
</BoundDataColumn>
<BoundDataColumn Property="p => p.UpdatedBy != null ? p.UpdatedBy.UserName : String.Empty" Title="Updated By" Sortable Include="UpdatedBy" />
<DataActions TData="string">
<Button OnClick="() => OpenEdit(context)" Type="@ButtonType.Primary">Edit</Button>
@* TODO: Add seperate Edit page and navigate to it *@
@* <a href="@($"/Metadata/Platforms/{context.Id}")" class="ant-btn ant-btn-primary">Edit</a> *@
<Popconfirm OnConfirm="() => Delete(context)" Title="Are you sure you want to delete this platform?">
<Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
</Popconfirm>
</DataActions>
</Columns>
</DataTable>
<Modal Title="@(PlatformContext.Id == Guid.Empty ? "New Platform" : "Edit Platform")"
@bind-Visible="@EditPlatformVisible"
OnOk="UpdateOrAdd"
OnCancel="CloseEdit">
<Form Model="@PlatformContext">
<FormItem Label="Name">
<Input @bind-Value="@context.Name" AutoFocus />
</FormItem>
</Form>
</Modal>
@code {
bool Loading = true;
bool EditPlatformVisible = false;
Platform PlatformContext = new();
DataTable<Platform> Table;
protected override async Task OnInitializedAsync()
{
Loading = false;
}
async Task UpdateOrAdd()
{
try
{
if (PlatformContext.Id == Guid.Empty)
{
await PlatformService.AddMissingAsync(x => x.Name == PlatformContext.Name, PlatformContext);
MessageService.Success($"{PlatformContext.Name} was added!");
}
else
{
await PlatformService.UpdateAsync(PlatformContext);
MessageService.Success($"{PlatformContext.Name} was updated!");
}
}
catch (Exception ex)
{
if (PlatformContext.Id == Guid.Empty)
MessageService.Error($"Could not add {PlatformContext.Name}!");
else
MessageService.Error($"Could not update {PlatformContext.Name}!");
Logger.LogError(ex, $"Could not add/update {PlatformContext.Name}!");
}
await Table.ReloadAsync();
await CloseEdit();
}
async Task OpenEdit(Platform? platform)
{
// query new instance, or create a new Edit context instance
PlatformContext = platform != null ? await PlatformService.GetAsync(platform.Id) : default!;
PlatformContext ??= new();
EditPlatformVisible = true;
await InvokeAsync(StateHasChanged);
}
async Task CloseEdit()
{
EditPlatformVisible = false;
await InvokeAsync(StateHasChanged);
}
async Task Delete(Platform platform)
{
try
{
await PlatformService.DeleteAsync(platform);
MessageService.Success("Platform deleted!");
await Table.ReloadAsync();
}
catch (Exception ex)
{
MessageService.Error("Could not delete platform!");
Logger?.LogError(ex, "Could not delete platform");
}
}
}