LANCommander/LANCommander.Server/UI/Pages/Metadata/Platforms/Index.razor

132 lines
4.2 KiB
Text
Raw Permalink Normal View History

2024-06-30 23:10:55 -05:00
@page "/Metadata/Platforms"
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject PlatformService PlatformService
2024-06-30 23:10:55 -05:00
@inject IMessageService MessageService
2024-07-07 16:33:46 -05:00
@inject ILogger<Index> Logger
2024-06-30 23:10:55 -05:00
2025-01-19 14:50:12 -06:00
<PageHeader Title="Platforms" />
<DataTable
TItem="Platform"
@ref="Table"
2025-01-21 18:48:01 -06:00
Size="@TableSize.Small"
Searchable
SearchProperty="p => p.Name">
2025-01-19 14:50:12 -06:00
<RightToolbar>
<Button OnClick="() => OpenEdit(null)" Type="@ButtonType.Primary">Add Platform</Button>
</RightToolbar>
<Columns>
2025-01-20 01:22:56 -06:00
<BoundDataColumn Property="p => p.Name" Sortable DefaultSortOrder="SortDirection.Ascending" />
<BoundDataColumn Property="p => p.Games.Count" Sortable Title="Games" Include="Games" />
2025-03-17 02:02:17 -05:00
<BoundDataColumn Property="p => p.CreatedOn" Title="Created On" Sortable>
<LocalTime Value="context.UpdatedOn" />
</BoundDataColumn>
2025-01-20 01:22:56 -06:00
<BoundDataColumn Property="p => p.CreatedBy != null ? p.CreatedBy.UserName : String.Empty" Title="Created By" Sortable Include="CreatedBy" />
2025-03-17 02:02:17 -05:00
<BoundDataColumn Property="p => p.UpdatedOn" Title="Updated On" Sortable>
<LocalTime Value="context.UpdatedOn" />
</BoundDataColumn>
2025-01-20 01:22:56 -06:00
<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> *@
2025-01-19 14:50:12 -06:00
<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>
2024-06-30 23:10:55 -05:00
<Modal Title="@(PlatformContext.Id == Guid.Empty ? "New Platform" : "Edit Platform")"
@bind-Visible="@EditPlatformVisible"
OnOk="UpdateOrAdd"
OnCancel="CloseEdit">
<Form Model="@PlatformContext">
<FormItem Label="Name">
2025-02-23 09:27:34 -06:00
<Input @bind-Value="@context.Name" AutoFocus />
2024-06-30 23:10:55 -05:00
</FormItem>
</Form>
</Modal>
@code {
bool Loading = true;
bool EditPlatformVisible = false;
2025-01-19 14:50:12 -06:00
Platform PlatformContext = new();
2024-06-30 23:10:55 -05:00
DataTable<Platform> Table;
protected override async Task OnInitializedAsync()
2024-06-30 23:10:55 -05:00
{
Loading = false;
2024-06-30 23:10:55 -05:00
}
2024-11-22 02:05:44 -06:00
async Task UpdateOrAdd()
2024-06-30 23:10:55 -05:00
{
try
{
if (PlatformContext.Id == Guid.Empty)
{
await PlatformService.AddMissingAsync(x => x.Name == PlatformContext.Name, PlatformContext);
MessageService.Success($"{PlatformContext.Name} was added!");
}
else
2024-06-30 23:10:55 -05:00
{
await PlatformService.UpdateAsync(PlatformContext);
MessageService.Success($"{PlatformContext.Name} was updated!");
2024-06-30 23:10:55 -05:00
}
}
catch (Exception ex)
{
if (PlatformContext.Id == Guid.Empty)
MessageService.Error($"Could not add {PlatformContext.Name}!");
else
2024-07-07 16:33:46 -05:00
MessageService.Error($"Could not update {PlatformContext.Name}!");
Logger.LogError(ex, $"Could not add/update {PlatformContext.Name}!");
2024-06-30 23:10:55 -05:00
}
Table.Reload();
2024-06-30 23:10:55 -05:00
await CloseEdit();
}
async Task OpenEdit(Platform? platform)
2024-06-30 23:10:55 -05:00
{
// query new instance, or create a new Edit context instance
PlatformContext = platform != null ? await PlatformService.GetAsync(platform.Id) : default!;
PlatformContext ??= new();
2024-06-30 23:10:55 -05:00
EditPlatformVisible = true;
await InvokeAsync(StateHasChanged);
}
2024-11-22 02:05:44 -06:00
async Task CloseEdit()
2024-06-30 23:10:55 -05:00
{
EditPlatformVisible = false;
await InvokeAsync(StateHasChanged);
}
async Task Delete(Platform platform)
2024-06-30 23:10:55 -05:00
{
try
{
await PlatformService.DeleteAsync(platform);
2024-06-30 23:10:55 -05:00
MessageService.Success("Platform deleted!");
2024-06-30 23:10:55 -05:00
Table.Reload();
}
catch (Exception ex)
{
MessageService.Error("Could not delete platform!");
Logger?.LogError(ex, "Could not delete platform");
}
2024-06-30 23:10:55 -05:00
}
}