| | | 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" /> |
| | 0 | 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" |
| | 0 | 19 | | @ref="_titleField" |
| | 0 | 20 | | Immediate="true" /> |
| | 0 | 21 | | </DialogContent> |
| | 0 | 22 | | <DialogActions> |
| | 0 | 23 | | <MudButton OnClick="Cancel">Cancel</MudButton> |
| | 0 | 24 | | <MudButton Color="Color.Primary" |
| | | 25 | | Variant="Variant.Filled" |
| | | 26 | | OnClick="Submit" |
| | | 27 | | Disabled="@(string.IsNullOrWhiteSpace(_title) || _isSubmitting)"> |
| | 0 | 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] |
| | 0 | 39 | | private MudDialogInstance? MudDialog { get; set; } |
| | | 40 | | |
| | | 41 | | [Parameter] |
| | 0 | 42 | | public Guid WorldId { get; set; } |
| | | 43 | | |
| | | 44 | | [Parameter] |
| | 0 | 45 | | public ArticleType ArticleType { get; set; } = ArticleType.WikiArticle; |
| | | 46 | | |
| | 0 | 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 | | { |
| | 0 | 53 | | if (firstRender && _titleField != null) |
| | | 54 | | { |
| | 0 | 55 | | await Task.Delay(100); |
| | 0 | 56 | | await _titleField.FocusAsync(); |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | private string GetIcon() => ArticleType switch |
| | 0 | 61 | | { |
| | 0 | 62 | | ArticleType.Character => Icons.Material.Filled.Person, |
| | 0 | 63 | | ArticleType.WikiArticle => Icons.Material.Filled.MenuBook, |
| | 0 | 64 | | ArticleType.Session => Icons.Material.Filled.EventNote, |
| | 0 | 65 | | _ => Icons.Material.Filled.Article |
| | 0 | 66 | | }; |
| | | 67 | | |
| | 0 | 68 | | private string GetTitle() => ArticleType switch |
| | 0 | 69 | | { |
| | 0 | 70 | | ArticleType.Character => "New Player Character", |
| | 0 | 71 | | ArticleType.WikiArticle => "New Wiki Article", |
| | 0 | 72 | | ArticleType.Session => "New Session", |
| | 0 | 73 | | _ => "New Article" |
| | 0 | 74 | | }; |
| | | 75 | | |
| | 0 | 76 | | private string GetTitleLabel() => ArticleType switch |
| | 0 | 77 | | { |
| | 0 | 78 | | ArticleType.Character => "Character Name", |
| | 0 | 79 | | _ => "Title" |
| | 0 | 80 | | }; |
| | | 81 | | |
| | 0 | 82 | | private void Cancel() => MudDialog?.Cancel(); |
| | | 83 | | |
| | | 84 | | private async Task Submit() |
| | | 85 | | { |
| | 0 | 86 | | if (string.IsNullOrWhiteSpace(_title) || _isSubmitting) return; |
| | | 87 | | |
| | 0 | 88 | | _isSubmitting = true; |
| | 0 | 89 | | StateHasChanged(); |
| | | 90 | | |
| | 0 | 91 | | var createDto = new ArticleCreateDto |
| | 0 | 92 | | { |
| | 0 | 93 | | Title = _title.Trim(), |
| | 0 | 94 | | Body = string.Empty, |
| | 0 | 95 | | WorldId = WorldId, |
| | 0 | 96 | | Type = ArticleType, |
| | 0 | 97 | | EffectiveDate = DateTime.Now |
| | 0 | 98 | | }; |
| | | 99 | | |
| | 0 | 100 | | var article = await ArticleApi.CreateArticleAsync(createDto); |
| | 0 | 101 | | if (article != null) |
| | | 102 | | { |
| | 0 | 103 | | MudDialog?.Close(DialogResult.Ok(article)); |
| | | 104 | | } |
| | | 105 | | else |
| | | 106 | | { |
| | 0 | 107 | | MudDialog?.Close(DialogResult.Cancel()); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | _isSubmitting = false; |
| | 0 | 111 | | } |
| | | 112 | | } |