LANCommander/LANCommander.Server/UI/Pages/Redistributables/Edit.razor

180 lines
No EOL
6.3 KiB
Text

@page "/Redistributables/{id:guid}"
@page "/Redistributables/{id:guid}/{panel}"
@page "/Redistributables/Add"
@using LANCommander.SDK.Enums
@using LANCommander.Server.Models
@using Microsoft.EntityFrameworkCore
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@inject DatabaseServiceFactory DatabaseServiceFactory
@inject IMessageService MessageService
@inject NavigationManager NavigationManager
@inject ILogger<Edit> Logger
<Layout Class="panel-layout" Style="padding: 24px 0;">
<Sider Width="200">
<Menu Mode="@MenuMode.Inline" Style="height: 100%;">
<MenuItem RouterLink="@($"/Redistributables/{Redistributable.Id}/General")">General</MenuItem>
@if (Redistributable?.Id != Guid.Empty)
{
<MenuItem RouterLink="@($"/Redistributables/{Redistributable.Id}/Scripts")">Scripts</MenuItem>
<MenuItem RouterLink="@($"/Redistributables/{Redistributable.Id}/Archives")">Archives</MenuItem>
}
</Menu>
</Sider>
<Content>
<PageHeader>
<PageHeaderTitle>
@if (Id == Guid.Empty)
{
<Text>Add New Redistributable</Text>
}
else
{
@Panel
}
</PageHeaderTitle>
<PageHeaderExtra>
<Space Direction="DirectionVHType.Horizontal">
@if (Redistributable != null && Redistributable.Id != Guid.Empty)
{
<SpaceItem>
<a href="/Redistributables/@(Id)/Export" target="_blank" class="ant-btn ant-btn-default">Export</a>
</SpaceItem>
}
<SpaceItem>
<Button Type="@ButtonType.Primary" OnClick="Save">Save</Button>
</SpaceItem>
</Space>
</PageHeaderExtra>
</PageHeader>
<div class="panel-layout-content">
<div data-panel="General">
<Form Model="@Redistributable" Layout="@FormLayout.Vertical">
<FormItem Label="Name">
<Input @bind-Value="@context.Name" />
</FormItem>
<FormItem Label="Notes">
<TextArea @bind-Value="@context.Notes" MaxLength=2000 ShowCount />
</FormItem>
<FormItem Label="Description">
<TextArea @bind-Value="@context.Description" MaxLength=500 ShowCount />
</FormItem>
<FormItem Label="Games">
<TransferInput LeftTitle="Available" RightTitle="Selected" DataSource="Games" TitleSelector="r => r.Title" @bind-Values="Redistributable.Games" />
</FormItem>
</Form>
</div>
@if (Redistributable != null && Redistributable.Id != Guid.Empty)
{
<div data-panel="Scripts">
<ScriptEditor RedistributableId="Redistributable.Id" ArchiveId="@LatestArchiveId" AllowedTypes="new ScriptType[] { ScriptType.Install, ScriptType.DetectInstall }" />
</div>
<div data-panel="Archives">
<ArchiveEditor RedistributableId="Redistributable.Id" />
</div>
}
</div>
</Content>
</Layout>
@if (!String.IsNullOrWhiteSpace(Panel))
{
<style>
.panel-layout [data-panel="@Panel"] {
display: block;
}
</style>
}
else
{
<style>
.panel-layout [data-panel="General"] {
display: block;
}
</style>
}
@code {
[Parameter] public Guid Id { get; set; }
[Parameter] public string Panel { get; set; }
Redistributable Redistributable = new Redistributable();
ICollection<Game> Games;
Settings Settings = SettingService.GetSettings();
Guid LatestArchiveId
{
get
{
if (Redistributable != null && Redistributable.Archives != null && Redistributable.Archives.Count > 0)
return Redistributable.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault().Id;
else
return Guid.Empty;
}
}
protected override async Task OnInitializedAsync()
{
using (var redistributableService = DatabaseServiceFactory.Create<RedistributableService>())
{
if (Id != Guid.Empty && Panel == null)
NavigationManager.NavigateTo($"/Redistributables/{Id}/General", true);
else if (Id != Guid.Empty)
Redistributable = await redistributableService.GetAsync(Id);
}
using (var gameService = DatabaseServiceFactory.Create<GameService>())
{
Games = await gameService
.SortBy(g => String.IsNullOrWhiteSpace(g.SortTitle) ? g.Title : g.SortTitle)
.GetAsync(g => g.Type == GameType.MainGame || g.Type == GameType.StandaloneExpansion || g.Type == GameType.StandaloneMod || g.BaseGame == null);
}
}
async Task Save()
{
try
{
using (var redistributableService = DatabaseServiceFactory.Create<RedistributableService>())
{
if (Redistributable.Id != Guid.Empty)
{
Redistributable = await redistributableService.UpdateAsync(Redistributable);
await MessageService.Success("Redistributable updated!");
}
else
{
Redistributable = await redistributableService.AddAsync(Redistributable);
NavigationManager.LocationChanged += NotifyRedistributableAdded;
NavigationManager.NavigateTo($"/Redistributables/{Redistributable.Id}");
}
}
}
catch (Exception ex)
{
MessageService.Error("Could not save!");
Logger.LogError(ex, "Could not save!");
}
}
void NotifyRedistributableAdded(object? sender, LocationChangedEventArgs e)
{
NavigationManager.LocationChanged -= NotifyRedistributableAdded;
MessageService.Success("Redistributable added!");
}
}