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

125 lines
4.2 KiB
Text

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