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

143 lines
4.7 KiB
Text
Raw Permalink Normal View History

@inject KeyService KeyService
@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>
2025-01-21 21:54:00 -06:00
<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>
<BoundDataColumn Property="k => k.AllocationMethod" Title="Allocation Method"/>
<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>
2025-03-17 02:02:17 -05:00
<BoundDataColumn Property="s => s.ClaimedOn" Title="Claimed" Sortable>
<LocalTime Value="context.CreatedOn" Relative />
</BoundDataColumn>
<DataActions TData="string">
2025-01-21 21:54:00 -06:00
@if (context.IsAllocated())
{
<Button OnClick="() => Release(context)">Release</Button>
}
</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;
StandaloneCodeEditor? _editor;
2025-01-21 21:54:00 -06:00
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);
2025-01-21 21:54:00 -06:00
_allocatedKeys = _keys.Count(k => k.IsAllocated());
}
2025-01-21 21:54:00 -06:00
void Edit()
{
_editModalVisible = true;
}
2025-01-21 21:54:00 -06:00
async Task Release(Key key)
{
await KeyService.ReleaseAsync(key);
2025-05-23 02:17:45 -05:00
await MessageService.SuccessAsync("Key was unallocated!");
}
2025-01-21 21:54:00 -06:00
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;
MessageService.Success("Keys updated!");
}
catch (Exception ex)
{
MessageService.ErrorAsync("Could not update keys!");
Logger.LogError(ex, "Could not update keys!");
}
}
}