LANCommander/LANCommander.Server/UI/Components/ActionEditor.razor

173 lines
5.1 KiB
Text

@using System.Collections.ObjectModel
@inject GameService GameService
@inject ServerService ServerService
@inject ModalService ModalService
@inject MessageService MessageService
@inject ILogger<ActionEditor> Logger
<Flex Vertical Gap="FlexGap.Large">
<Table TItem="Data.Models.Action" DataSource="@Actions.OrderBy(a => a.SortOrder)" HidePagination Style="border: 1px solid #f0f0f0" Responsive>
<PropertyColumn Property="a => a.Name">
<Input Type="InputType.Text" @bind-Value="context.Name"/>
</PropertyColumn>
<PropertyColumn Property="a => a.Path">
<FilePicker @bind-Value="context.Path" ArchiveId="@ArchiveId" AllowDirectories Title="Select Action Executable" OnSelected="(path) => OnPathSelected(path, context)"/>
</PropertyColumn>
<PropertyColumn Property="a => a.Arguments">
<Input Type="InputType.Text" @bind-Value="context.Arguments"/>
</PropertyColumn>
<PropertyColumn Property="a => a.WorkingDirectory" Title="Working Dir">
<Input Type="InputType.Text" @bind-Value="context.WorkingDirectory"/>
</PropertyColumn>
<PropertyColumn Property="a => a.PrimaryAction" Title="Primary" Style="text-align: center">
<Checkbox @bind-Checked="context.PrimaryAction"/>
</PropertyColumn>
<ActionColumn>
<Flex Gap="FlexGap.Small" Justify="FlexJustify.End">
<Button OnClick="() => MoveUp(context)" Icon="@IconType.Outline.Up" Type="ButtonType.Text"/>
<Button OnClick="() => MoveDown(context)" Icon="@IconType.Outline.Down" Type="ButtonType.Text"/>
<Popconfirm OnConfirm="() => RemoveAction(context)" Title="Are you sure you want to remove this action?">
<Button Icon="@IconType.Outline.Close" Type="ButtonType.Text" Danger/>
</Popconfirm>
</Flex>
</ActionColumn>
</Table>
<GridRow Justify="RowJustify.End">
<GridCol>
<Button OnClick="AddAction" Type="ButtonType.Primary">Add Action</Button>
</GridCol>
</GridRow>
</Flex>
@code {
[Parameter] public Guid GameId { get; set; }
[Parameter] public Guid ServerId { get; set; }
[Parameter] public Guid ArchiveId { get; set; }
public ICollection<Data.Models.Action> Actions { get; set; }
Game Game;
Server Server;
ObservableCollection<Data.Models.Action> SortedActions = new();
protected override async Task OnInitializedAsync()
{
if (GameId != Guid.Empty)
{
Game = await GameService
.Include(g => g.Actions)
.GetAsync(GameId);
Actions = Game.Actions;
}
if (ServerId != Guid.Empty)
{
Server = await ServerService
.Include(s => s.Actions)
.GetAsync(ServerId);
Actions = Server.Actions;
}
if (SortedActions.Count != Actions.Count)
{
SortedActions.Clear();
foreach (var action in Actions.OrderBy(a => a.SortOrder))
SortedActions.Add(action);
}
}
async Task AddAction()
{
var newAction = new Data.Models.Action()
{
PrimaryAction = Actions.Count == 0,
SortOrder = Actions.Count
};
if (GameId != Guid.Empty)
newAction.GameId = GameId;
if (ServerId != Guid.Empty)
newAction.ServerId = ServerId;
SortedActions.Add(newAction);
await UpdateOrder();
}
async Task RemoveAction(Data.Models.Action action)
{
SortedActions.Remove(action);
await UpdateOrder();
}
async Task MoveUp(Data.Models.Action action)
{
var index = SortedActions.IndexOf(action);
if (index > 0)
SortedActions.Move(index, index - 1);
await UpdateOrder();
}
async Task MoveDown(Data.Models.Action action)
{
var index = SortedActions.IndexOf(action);
if (index < SortedActions.Count - 1)
SortedActions.Move(index, index + 1);
await UpdateOrder();
}
async Task UpdateOrder()
{
foreach (var action in SortedActions)
{
action.SortOrder = SortedActions.IndexOf(action);
}
Actions = SortedActions;
if (Game != null)
Game.Actions = Actions;
if (Server != null)
Server.Actions = Actions;
}
public async Task Save()
{
try
{
if (GameId != Guid.Empty)
{
await GameService.UpdateAsync(Game);
}
if (ServerId != Guid.Empty)
{
await ServerService.UpdateAsync(Server);
}
MessageService.Success("Actions updated!");
}
catch (Exception ex)
{
MessageService.Error("Could not update actions!");
Logger.LogError(ex, "Could not update actions!");
}
}
void OnPathSelected(string path, Data.Models.Action action)
{
action.WorkingDirectory = "{InstallDir}";
}
}