LANCommander/LANCommander.Server/UI/Pages/Games/Components/KeysEditor.razor

220 lines
7.4 KiB
Text

@using LANCommander.SDK.Enums
@inject KeyService KeyService
@inject ModalService ModalService
@inject IMessageService MessageService
@inject ILogger<KeysEditor> Logger
@if (_keys != null)
{
<Flex Direction="FlexDirection.Vertical" Gap="FlexGap.Small">
<GridRow>
<GridCol Span="8">
<Statistic Title="Available" Value="_keys.Count - _allocatedKeys" Style="text-align: center;" />
</GridCol>
<GridCol Span="8">
<Statistic Title="Allocated" Value="_allocatedKeys" Style="text-align: center;" />
</GridCol>
<GridCol Span="8">
<Statistic Title="Total" Value="_keys.Count" Style="text-align: center;" />
</GridCol>
</GridRow>
<DataTable @ref="_dataTable"
TItem="Key"
Responsive
Query="k => k.GameId == GameId">
<RightToolbar>
<Button Type="@ButtonType.Default" OnClick="() => Edit()">Edit</Button>
<Button Type="@ButtonType.Primary" OnClick="() => Save()">Save</Button>
</RightToolbar>
<Columns>
<BoundDataColumn Property="k => k.Value">
<InputPassword @bind-Value="@context.Value"/>
</BoundDataColumn>
<DataColumn TData="string" Title="Allocation Method">
@if (context.AllocationMethod.HasValue)
{
@context.AllocationMethod.Value.GetDisplayName()
}
</DataColumn>
<DataColumn TData="string" Title="Claimed By" Include="ClaimedByUser">
@switch (context.AllocationMethod)
{
case KeyAllocationMethod.MacAddress:
<text>@context.ClaimedByMacAddress</text>
break;
case KeyAllocationMethod.UserAccount:
<text>@context.ClaimedByUser?.UserName</text>
break;
}
</DataColumn>
<BoundDataColumn Property="s => s.ClaimedOn" Title="Claimed" Sortable>
@if (context.ClaimedOn.HasValue)
{
<LocalTime Value="context.ClaimedOn" Relative />
}
</BoundDataColumn>
<DataActions TData="string">
<Dropdown>
<Overlay>
<Menu>
@if (!context.IsAllocated())
{
<MenuItem OnClick="() => Assign(context)">Assign</MenuItem>
}
@if (context.IsAllocated())
{
<MenuItem OnClick="() => Release(context)">Release</MenuItem>
}
<Popconfirm Title="Are you sure you want to remove this key?" OnConfirm="() => Remove(context)">
<MenuItem>Remove</MenuItem>
</Popconfirm>
</Menu>
</Overlay>
<ChildContent>
<Button Icon="@IconType.Outline.Ellipsis" />
</ChildContent>
</Dropdown>
</DataActions>
</Columns>
</DataTable>
</Flex>
<Modal Title="Edit Keys" Visible="_editModalVisible" Maximizable="false" DefaultMaximized="true" OnCancel="() => _editModalVisible = false" OnOk="Save">
<StandaloneCodeEditor @ref="_editor" Id="editor" ConstructionOptions="EditorConstructionOptions" />
</Modal>
}
<style>
.monaco-editor-container {
height: 600px;
}
</style>
@code {
[CascadingParameter(Name = "GameId")] public Guid GameId { get; set; }
ICollection<Key> _keys = new List<Key>();
int _allocatedKeys;
bool _editModalVisible = false;
DataTable<Key>? _dataTable;
StandaloneCodeEditor? _editor;
StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
{
return new StandaloneEditorConstructionOptions
{
AutomaticLayout = true,
Language = "text",
Value = String.Join('\n', _keys.Select(k => k.Value)),
Theme = "vs-dark",
};
}
protected override async Task OnInitializedAsync()
{
if (_keys == null)
_keys = new List<Key>();
_keys = await KeyService.GetAsync(k => k.GameId == GameId);
_allocatedKeys = _keys.Count(k => k.IsAllocated());
}
void Edit()
{
_editModalVisible = true;
}
async Task Assign(Key key)
{
var modalOptions = new ModalOptions()
{
Title = "Assign Key",
Maximizable = false,
DefaultMaximized = false,
Closable = true,
Draggable = true,
OkText = "Assign"
};
var modalRef = await ModalService.CreateModalAsync<AssignKeyDialog, Guid, bool>(modalOptions, key.Id);
modalRef.OnOk = async (result) =>
{
_keys = await KeyService.GetAsync(k => k.GameId == GameId);
_allocatedKeys = _keys.Count(k => k.IsAllocated());
if (_dataTable != null)
await _dataTable.ReloadAsync();
await InvokeAsync(StateHasChanged);
};
}
async Task Remove(Key key)
{
await KeyService.DeleteAsync(key);
_keys = await KeyService.GetAsync(k => k.GameId == GameId);
_allocatedKeys = _keys.Count(k => k.IsAllocated());
if (_dataTable != null)
await _dataTable.ReloadAsync();
await MessageService.SuccessAsync("Key was removed!");
}
async Task Release(Key key)
{
await KeyService.ReleaseAsync(key);
_keys = await KeyService.GetAsync(k => k.GameId == GameId);
_allocatedKeys = _keys.Count(k => k.IsAllocated());
if (_dataTable != null)
await _dataTable.ReloadAsync();
await MessageService.SuccessAsync("Key was unallocated!");
}
async Task Save()
{
try
{
var value = await _editor.GetValue();
var keys = value.Split("\n").Select(k => k.Trim()).Where(k => !String.IsNullOrWhiteSpace(k)).ToList();
var keysDeleted = _keys.Where(k => !keys.Contains(k.Value)).ToList();
var keysAdded = keys.Where(k => !_keys.Any(gk => gk.Value == k)).ToList();
foreach (var key in keysDeleted)
await KeyService.DeleteAsync(key);
foreach (var key in keysAdded)
await KeyService.AddAsync(new Key()
{
GameId = GameId,
Value = key
});
_editModalVisible = false;
_keys = await KeyService.GetAsync(k => k.GameId == GameId);
_allocatedKeys = _keys.Count(k => k.IsAllocated());
if (_dataTable != null)
await _dataTable.ReloadAsync();
MessageService.Success("Keys updated!");
}
catch (Exception ex)
{
MessageService.ErrorAsync("Could not update keys!");
Logger.LogError(ex, "Could not update keys!");
}
}
}