| | | 1 | | @using Chronicis.Shared.DTOs |
| | | 2 | | @inject IWorldApiService WorldApi |
| | | 3 | | @inject ISnackbar Snackbar |
| | | 4 | | |
| | | 5 | | <MudDialog> |
| | | 6 | | <TitleContent> |
| | | 7 | | <MudText Typo="Typo.h6"> |
| | | 8 | | <MudIcon Icon="@Icons.Material.Filled.GroupAdd" Class="mr-2" /> |
| | | 9 | | Join a World |
| | | 10 | | </MudText> |
| | | 11 | | </TitleContent> |
| | | 12 | | <DialogContent> |
| | | 13 | | <MudText Typo="Typo.body2" Class="mb-4"> |
| | | 14 | | Enter the invitation code shared by the world's GM to join their world. |
| | | 15 | | </MudText> |
| | | 16 | | |
| | | 17 | | <MudTextField @bind-Value="_code" |
| | | 18 | | Label="Invitation Code" |
| | | 19 | | Variant="Variant.Outlined" |
| | | 20 | | Required="true" |
| | | 21 | | RequiredError="Code is required" |
| | | 22 | | Placeholder="e.g., FROG-AXLE" |
| | | 23 | | Class="mb-3" |
| | 0 | 24 | | @ref="_codeField" |
| | 0 | 25 | | Immediate="true" |
| | 0 | 26 | | Style="font-family: monospace; font-size: 1.2em; letter-spacing: 2px;" |
| | 0 | 27 | | Error="@(!string.IsNullOrEmpty(_error))" |
| | 0 | 28 | | ErrorText="@_error" /> |
| | 0 | 29 | | |
| | 0 | 30 | | @if (_result != null && _result.Success) |
| | | 31 | | { |
| | | 32 | | <MudAlert Severity="Severity.Success" Class="mt-3"> |
| | | 33 | | <MudText Typo="Typo.body1"> |
| | 0 | 34 | | Successfully joined <strong>@_result.WorldName</strong> as <strong>@_result.AssignedRole</strong>! |
| | | 35 | | </MudText> |
| | | 36 | | </MudAlert> |
| | | 37 | | } |
| | | 38 | | </DialogContent> |
| | | 39 | | <DialogActions> |
| | 0 | 40 | | @if (_result?.Success == true) |
| | | 41 | | { |
| | | 42 | | <MudButton Color="Color.Primary" |
| | | 43 | | Variant="Variant.Filled" |
| | | 44 | | OnClick="GoToWorld"> |
| | | 45 | | Go to World |
| | | 46 | | </MudButton> |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | | 50 | | <MudButton OnClick="Cancel">Cancel</MudButton> |
| | | 51 | | <MudButton Color="Color.Primary" |
| | | 52 | | Variant="Variant.Filled" |
| | | 53 | | OnClick="Submit" |
| | | 54 | | Disabled="@(string.IsNullOrWhiteSpace(_code) || _isSubmitting)"> |
| | 0 | 55 | | @if (_isSubmitting) |
| | | 56 | | { |
| | | 57 | | <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" /> |
| | | 58 | | } |
| | | 59 | | Join |
| | | 60 | | </MudButton> |
| | | 61 | | } |
| | | 62 | | </DialogActions> |
| | | 63 | | </MudDialog> |
| | | 64 | | |
| | | 65 | | @code { |
| | | 66 | | [CascadingParameter] |
| | 0 | 67 | | private MudDialogInstance? MudDialog { get; set; } |
| | | 68 | | |
| | 0 | 69 | | private string _code = string.Empty; |
| | | 70 | | private string? _error = null; |
| | | 71 | | private bool _isSubmitting = false; |
| | | 72 | | private WorldJoinResultDto? _result = null; |
| | | 73 | | private MudTextField<string>? _codeField; |
| | | 74 | | |
| | | 75 | | protected override async Task OnAfterRenderAsync(bool firstRender) |
| | | 76 | | { |
| | 0 | 77 | | if (firstRender && _codeField != null) |
| | | 78 | | { |
| | 0 | 79 | | await Task.Delay(100); |
| | 0 | 80 | | await _codeField.FocusAsync(); |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | private void Cancel() => MudDialog?.Cancel(); |
| | | 85 | | |
| | | 86 | | private void GoToWorld() |
| | | 87 | | { |
| | 0 | 88 | | MudDialog?.Close(DialogResult.Ok(_result)); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | private async Task Submit() |
| | | 92 | | { |
| | 0 | 93 | | if (string.IsNullOrWhiteSpace(_code) || _isSubmitting) return; |
| | | 94 | | |
| | 0 | 95 | | _isSubmitting = true; |
| | 0 | 96 | | _error = null; |
| | 0 | 97 | | StateHasChanged(); |
| | | 98 | | |
| | | 99 | | try |
| | | 100 | | { |
| | 0 | 101 | | _result = await WorldApi.JoinWorldAsync(_code.Trim()); |
| | | 102 | | |
| | 0 | 103 | | if (_result == null) |
| | | 104 | | { |
| | 0 | 105 | | _error = "Failed to join world. Please try again."; |
| | | 106 | | } |
| | 0 | 107 | | else if (!_result.Success) |
| | | 108 | | { |
| | 0 | 109 | | _error = _result.ErrorMessage ?? "Failed to join world."; |
| | | 110 | | } |
| | | 111 | | // Success case is handled by the UI showing the success message |
| | 0 | 112 | | } |
| | 0 | 113 | | catch (Exception ex) |
| | | 114 | | { |
| | 0 | 115 | | _error = $"Error: {ex.Message}"; |
| | 0 | 116 | | } |
| | | 117 | | finally |
| | | 118 | | { |
| | 0 | 119 | | _isSubmitting = false; |
| | 0 | 120 | | StateHasChanged(); |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | } |