| | | 1 | | @inject ICharacterApiService CharacterApi |
| | | 2 | | @inject ISnackbar Snackbar |
| | | 3 | | |
| | | 4 | | @code { |
| | | 5 | | private readonly ILogger<CharacterClaimButton> _logger; |
| | | 6 | | |
| | | 7 | | // Direct constructor injection |
| | 0 | 8 | | public CharacterClaimButton(ILogger<CharacterClaimButton> logger) |
| | | 9 | | { |
| | 0 | 10 | | _logger = logger; |
| | 0 | 11 | | } |
| | | 12 | | } |
| | | 13 | | |
| | 0 | 14 | | @if (_isLoading) |
| | | 15 | | { |
| | | 16 | | <MudProgressCircular Size="Size.Small" Indeterminate="true" /> |
| | | 17 | | } |
| | 0 | 18 | | else if (_claimStatus != null) |
| | | 19 | | { |
| | 0 | 20 | | @if (_claimStatus.IsClaimedByMe) |
| | | 21 | | { |
| | | 22 | | <MudButton Variant="Variant.Outlined" |
| | | 23 | | Color="Color.Success" |
| | | 24 | | Size="Size.Small" |
| | | 25 | | StartIcon="@Icons.Material.Filled.CheckCircle" |
| | | 26 | | OnClick="UnclaimCharacter" |
| | | 27 | | Disabled="_isProcessing"> |
| | 0 | 28 | | @if (_isProcessing) |
| | | 29 | | { |
| | | 30 | | <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" /> |
| | | 31 | | } |
| | | 32 | | My Character |
| | | 33 | | </MudButton> |
| | | 34 | | } |
| | 0 | 35 | | else if (_claimStatus.IsClaimed) |
| | | 36 | | { |
| | | 37 | | <MudTooltip Text="@($"Claimed by {_claimStatus.ClaimedByName}")"> |
| | | 38 | | <MudChip T="string" |
| | | 39 | | Size="Size.Small" |
| | | 40 | | Variant="Variant.Outlined" |
| | | 41 | | Icon="@Icons.Material.Filled.Person"> |
| | 0 | 42 | | @_claimStatus.ClaimedByName's Character |
| | | 43 | | </MudChip> |
| | | 44 | | </MudTooltip> |
| | | 45 | | } |
| | | 46 | | else |
| | | 47 | | { |
| | | 48 | | <MudButton Variant="Variant.Outlined" |
| | | 49 | | Color="Color.Primary" |
| | | 50 | | Size="Size.Small" |
| | | 51 | | StartIcon="@Icons.Material.Filled.PersonAdd" |
| | | 52 | | OnClick="ClaimCharacter" |
| | | 53 | | Disabled="_isProcessing"> |
| | 0 | 54 | | @if (_isProcessing) |
| | | 55 | | { |
| | | 56 | | <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" /> |
| | | 57 | | } |
| | | 58 | | Claim as My Character |
| | | 59 | | </MudButton> |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | @code { |
| | | 64 | | [Parameter, EditorRequired] |
| | 0 | 65 | | public Guid CharacterId { get; set; } |
| | | 66 | | |
| | | 67 | | [Parameter] |
| | 0 | 68 | | public EventCallback OnClaimChanged { get; set; } |
| | | 69 | | |
| | | 70 | | private CharacterClaimStatusDto? _claimStatus; |
| | 0 | 71 | | private bool _isLoading = true; |
| | | 72 | | private bool _isProcessing = false; |
| | | 73 | | |
| | | 74 | | protected override async Task OnParametersSetAsync() |
| | | 75 | | { |
| | 0 | 76 | | await LoadClaimStatus(); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | private async Task LoadClaimStatus() |
| | | 80 | | { |
| | 0 | 81 | | _isLoading = true; |
| | 0 | 82 | | StateHasChanged(); |
| | | 83 | | |
| | | 84 | | try |
| | | 85 | | { |
| | 0 | 86 | | _claimStatus = await CharacterApi.GetClaimStatusAsync(CharacterId); |
| | 0 | 87 | | } |
| | 0 | 88 | | catch (Exception ex) |
| | | 89 | | { |
| | 0 | 90 | | _logger.LogErrorSanitized(ex, "Error loading claim status for character {CharacterId}", CharacterId); |
| | 0 | 91 | | } |
| | | 92 | | finally |
| | | 93 | | { |
| | 0 | 94 | | _isLoading = false; |
| | 0 | 95 | | StateHasChanged(); |
| | | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | private async Task ClaimCharacter() |
| | | 100 | | { |
| | 0 | 101 | | _isProcessing = true; |
| | 0 | 102 | | StateHasChanged(); |
| | | 103 | | |
| | | 104 | | try |
| | | 105 | | { |
| | 0 | 106 | | var success = await CharacterApi.ClaimCharacterAsync(CharacterId); |
| | 0 | 107 | | if (success) |
| | | 108 | | { |
| | 0 | 109 | | Snackbar.Add("Character claimed! This is now your character.", Severity.Success); |
| | 0 | 110 | | await LoadClaimStatus(); |
| | 0 | 111 | | await OnClaimChanged.InvokeAsync(); |
| | | 112 | | } |
| | | 113 | | else |
| | | 114 | | { |
| | 0 | 115 | | Snackbar.Add("Failed to claim character", Severity.Error); |
| | | 116 | | } |
| | 0 | 117 | | } |
| | 0 | 118 | | catch (Exception ex) |
| | | 119 | | { |
| | 0 | 120 | | _logger.LogErrorSanitized(ex, "Error claiming character {CharacterId}", CharacterId); |
| | 0 | 121 | | Snackbar.Add("An error occurred", Severity.Error); |
| | 0 | 122 | | } |
| | | 123 | | finally |
| | | 124 | | { |
| | 0 | 125 | | _isProcessing = false; |
| | 0 | 126 | | StateHasChanged(); |
| | | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | private async Task UnclaimCharacter() |
| | | 131 | | { |
| | 0 | 132 | | _isProcessing = true; |
| | 0 | 133 | | StateHasChanged(); |
| | | 134 | | |
| | | 135 | | try |
| | | 136 | | { |
| | 0 | 137 | | var success = await CharacterApi.UnclaimCharacterAsync(CharacterId); |
| | 0 | 138 | | if (success) |
| | | 139 | | { |
| | 0 | 140 | | Snackbar.Add("Character unclaimed", Severity.Info); |
| | 0 | 141 | | await LoadClaimStatus(); |
| | 0 | 142 | | await OnClaimChanged.InvokeAsync(); |
| | | 143 | | } |
| | | 144 | | else |
| | | 145 | | { |
| | 0 | 146 | | Snackbar.Add("Failed to unclaim character", Severity.Error); |
| | | 147 | | } |
| | 0 | 148 | | } |
| | 0 | 149 | | catch (Exception ex) |
| | | 150 | | { |
| | 0 | 151 | | _logger.LogErrorSanitized(ex, "Error unclaiming character {CharacterId}", CharacterId); |
| | 0 | 152 | | Snackbar.Add("An error occurred", Severity.Error); |
| | 0 | 153 | | } |
| | | 154 | | finally |
| | | 155 | | { |
| | 0 | 156 | | _isProcessing = false; |
| | 0 | 157 | | StateHasChanged(); |
| | | 158 | | } |
| | 0 | 159 | | } |
| | | 160 | | } |