@inject KeyService KeyService @inject IMessageService MessageService @inject ILogger Logger @if (_keys != null) { @switch (context.AllocationMethod) { case KeyAllocationMethod.MacAddress: @context.ClaimedByMacAddress break; case KeyAllocationMethod.UserAccount: @context.ClaimedByUser?.UserName break; } @if (context.IsAllocated()) { } } @code { [CascadingParameter(Name = "GameId")] public Guid GameId { get; set; } ICollection _keys = new List(); int _allocatedKeys; bool _editModalVisible = false; 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(); _keys = await KeyService.GetAsync(k => k.GameId == GameId); _allocatedKeys = _keys.Count(k => k.IsAllocated()); } void Edit() { _editModalVisible = true; } async Task Release(Key key) { await KeyService.ReleaseAsync(key); 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; MessageService.Success("Keys updated!"); } catch (Exception ex) { MessageService.ErrorAsync("Could not update keys!"); Logger.LogError(ex, "Could not update keys!"); } } }