| | | 1 | | @using Chronicis.Shared.DTOs |
| | | 2 | | @inject ICampaignApiService CampaignApi |
| | | 3 | | |
| | | 4 | | <MudDialog> |
| | | 5 | | <TitleContent> |
| | | 6 | | <MudText Typo="Typo.h6"> |
| | | 7 | | <MudIcon Icon="@Icons.Material.Filled.AutoStories" Class="mr-2" /> |
| | | 8 | | New Campaign |
| | | 9 | | </MudText> |
| | | 10 | | </TitleContent> |
| | | 11 | | <DialogContent> |
| | | 12 | | <MudTextField @bind-Value="_name" |
| | | 13 | | Label="Campaign Name" |
| | | 14 | | Variant="Variant.Outlined" |
| | | 15 | | Required="true" |
| | | 16 | | RequiredError="Name is required" |
| | | 17 | | Class="mb-3" |
| | 0 | 18 | | @ref="_nameField" |
| | 0 | 19 | | Immediate="true" |
| | 0 | 20 | | OnKeyDown="@OnNameKeyDown" /> |
| | 0 | 21 | | |
| | 0 | 22 | | <MudTextField @bind-Value="_description" |
| | 0 | 23 | | Label="Description (optional)" |
| | | 24 | | Variant="Variant.Outlined" |
| | | 25 | | Lines="3" /> |
| | | 26 | | </DialogContent> |
| | | 27 | | <DialogActions> |
| | | 28 | | <MudButton OnClick="Cancel">Cancel</MudButton> |
| | | 29 | | <MudButton Color="Color.Primary" |
| | | 30 | | Variant="Variant.Filled" |
| | | 31 | | OnClick="Submit" |
| | | 32 | | Disabled="@(string.IsNullOrWhiteSpace(_name) || _isSubmitting)"> |
| | 0 | 33 | | @if (_isSubmitting) |
| | | 34 | | { |
| | | 35 | | <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" /> |
| | | 36 | | } |
| | | 37 | | Create |
| | | 38 | | </MudButton> |
| | | 39 | | </DialogActions> |
| | | 40 | | </MudDialog> |
| | | 41 | | |
| | | 42 | | @code { |
| | | 43 | | [CascadingParameter] |
| | 0 | 44 | | private MudDialogInstance? MudDialog { get; set; } |
| | | 45 | | |
| | | 46 | | [Parameter] |
| | 0 | 47 | | public Guid WorldId { get; set; } |
| | | 48 | | |
| | 0 | 49 | | private string _name = string.Empty; |
| | 0 | 50 | | private string _description = string.Empty; |
| | | 51 | | private bool _isSubmitting = false; |
| | | 52 | | private MudTextField<string>? _nameField; |
| | | 53 | | |
| | | 54 | | protected override async Task OnAfterRenderAsync(bool firstRender) |
| | | 55 | | { |
| | 0 | 56 | | if (firstRender && _nameField != null) |
| | | 57 | | { |
| | 0 | 58 | | await Task.Delay(100); |
| | 0 | 59 | | await _nameField.FocusAsync(); |
| | | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | private void Cancel() => MudDialog?.Cancel(); |
| | | 64 | | |
| | | 65 | | private async Task OnNameKeyDown(KeyboardEventArgs e) |
| | | 66 | | { |
| | 0 | 67 | | if (e.Key == "Enter" && !string.IsNullOrWhiteSpace(_name) && !_isSubmitting) |
| | | 68 | | { |
| | 0 | 69 | | await Submit(); |
| | | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private async Task Submit() |
| | | 74 | | { |
| | 0 | 75 | | if (string.IsNullOrWhiteSpace(_name) || _isSubmitting) return; |
| | | 76 | | |
| | 0 | 77 | | _isSubmitting = true; |
| | 0 | 78 | | StateHasChanged(); |
| | | 79 | | |
| | | 80 | | try |
| | | 81 | | { |
| | 0 | 82 | | var createDto = new CampaignCreateDto |
| | 0 | 83 | | { |
| | 0 | 84 | | Name = _name.Trim(), |
| | 0 | 85 | | Description = string.IsNullOrWhiteSpace(_description) ? null : _description.Trim(), |
| | 0 | 86 | | WorldId = WorldId |
| | 0 | 87 | | }; |
| | | 88 | | |
| | 0 | 89 | | var campaign = await CampaignApi.CreateCampaignAsync(createDto); |
| | 0 | 90 | | MudDialog?.Close(DialogResult.Ok(campaign)); |
| | 0 | 91 | | } |
| | 0 | 92 | | catch |
| | | 93 | | { |
| | 0 | 94 | | MudDialog?.Close(DialogResult.Cancel()); |
| | 0 | 95 | | } |
| | | 96 | | finally |
| | | 97 | | { |
| | 0 | 98 | | _isSubmitting = false; |
| | | 99 | | } |
| | 0 | 100 | | } |
| | | 101 | | } |