| | | 1 | | @using Chronicis.Shared.DTOs |
| | | 2 | | @using Chronicis.Shared.Enums |
| | | 3 | | @inject IArticleApiService ArticleApi |
| | | 4 | | |
| | | 5 | | <MudDialog> |
| | | 6 | | <TitleContent> |
| | | 7 | | <MudText Typo="Typo.h6"> |
| | | 8 | | <MudIcon Icon="@GetIcon()" Class="mr-2" /> |
| | | 9 | | @GetTitle() |
| | | 10 | | </MudText> |
| | | 11 | | </TitleContent> |
| | | 12 | | <DialogContent> |
| | | 13 | | <MudTextField @bind-Value="_title" |
| | | 14 | | Label="@GetTitleLabel()" |
| | | 15 | | Variant="Variant.Outlined" |
| | | 16 | | Required="true" |
| | | 17 | | RequiredError="Title is required" |
| | | 18 | | Class="mb-3" |
| | | 19 | | @ref="_titleField" |
| | | 20 | | Immediate="true" /> |
| | | 21 | | </DialogContent> |
| | | 22 | | <DialogActions> |
| | | 23 | | <MudButton OnClick="Cancel">Cancel</MudButton> |
| | | 24 | | <MudButton Color="Color.Primary" |
| | | 25 | | Variant="Variant.Filled" |
| | | 26 | | OnClick="Submit" |
| | | 27 | | Disabled="@(string.IsNullOrWhiteSpace(_title) || _isSubmitting)"> |
| | | 28 | | @if (_isSubmitting) |
| | | 29 | | { |
| | | 30 | | <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" /> |
| | | 31 | | } |
| | | 32 | | Create |
| | | 33 | | </MudButton> |
| | | 34 | | </DialogActions> |
| | | 35 | | </MudDialog> |
| | | 36 | | |
| | | 37 | | @code { |
| | | 38 | | [CascadingParameter] |
| | | 39 | | private MudDialogInstance? MudDialog { get; set; } |
| | | 40 | | |
| | | 41 | | [Parameter] |
| | | 42 | | public Guid WorldId { get; set; } |
| | | 43 | | |
| | | 44 | | [Parameter] |
| | | 45 | | public ArticleType ArticleType { get; set; } = ArticleType.WikiArticle; |
| | | 46 | | |
| | 14 | 47 | | private string _title = string.Empty; |
| | | 48 | | private bool _isSubmitting = false; |
| | | 49 | | private MudTextField<string>? _titleField; |
| | | 50 | | |
| | | 51 | | protected override async Task OnAfterRenderAsync(bool firstRender) |
| | | 52 | | { |
| | | 53 | | if (firstRender && _titleField != null) |
| | | 54 | | { |
| | | 55 | | await Task.Delay(100); |
| | | 56 | | await _titleField.FocusAsync(); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | 8 | 60 | | private string GetIcon() => ArticleType switch |
| | 8 | 61 | | { |
| | 2 | 62 | | ArticleType.Character => Icons.Material.Filled.Person, |
| | 5 | 63 | | ArticleType.WikiArticle => Icons.Material.Filled.MenuBook, |
| | 1 | 64 | | _ => Icons.Material.Filled.Article |
| | 8 | 65 | | }; |
| | | 66 | | |
| | 8 | 67 | | private string GetTitle() => ArticleType switch |
| | 8 | 68 | | { |
| | 2 | 69 | | ArticleType.Character => "New Player Character", |
| | 5 | 70 | | ArticleType.WikiArticle => "New Wiki Article", |
| | 1 | 71 | | _ => "New Article" |
| | 8 | 72 | | }; |
| | | 73 | | |
| | 11 | 74 | | private string GetTitleLabel() => ArticleType switch |
| | 11 | 75 | | { |
| | 3 | 76 | | ArticleType.Character => "Character Name", |
| | 8 | 77 | | _ => "Title" |
| | 11 | 78 | | }; |
| | | 79 | | |
| | 2 | 80 | | private void Cancel() => MudDialog?.Cancel(); |
| | | 81 | | |
| | | 82 | | private async Task Submit() |
| | | 83 | | { |
| | | 84 | | if (string.IsNullOrWhiteSpace(_title) || _isSubmitting) return; |
| | | 85 | | |
| | | 86 | | _isSubmitting = true; |
| | | 87 | | StateHasChanged(); |
| | | 88 | | |
| | | 89 | | var createDto = new ArticleCreateDto |
| | | 90 | | { |
| | | 91 | | Title = _title.Trim(), |
| | | 92 | | Body = string.Empty, |
| | | 93 | | WorldId = WorldId, |
| | | 94 | | Type = ArticleType, |
| | | 95 | | EffectiveDate = DateTime.Now |
| | | 96 | | }; |
| | | 97 | | |
| | | 98 | | var article = await ArticleApi.CreateArticleAsync(createDto); |
| | | 99 | | if (article != null) |
| | | 100 | | { |
| | | 101 | | MudDialog?.Close(DialogResult.Ok(article)); |
| | | 102 | | } |
| | | 103 | | else |
| | | 104 | | { |
| | | 105 | | MudDialog?.Close(DialogResult.Cancel()); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | _isSubmitting = false; |
| | | 109 | | } |
| | | 110 | | } |