< Summary

Information
Class: Chronicis.Client.Components.Dialogs.CreateArcDialog
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Components/Dialogs/CreateArcDialog.razor
Line coverage
0%
Covered lines: 0
Uncovered lines: 37
Coverable lines: 37
Total lines: 112
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_MudDialog()100%210%
get_CampaignId()100%210%
.ctor()100%210%
OnAfterRenderAsync()0%2040%
Cancel()0%620%
OnNameKeyDown()0%4260%
Submit()0%110100%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Components/Dialogs/CreateArcDialog.razor

#LineLine coverage
 1@using Chronicis.Shared.DTOs
 2@using MudBlazor
 3@inject IArcApiService ArcApi
 4
 5<MudDialog>
 6    <TitleContent>
 7        <MudText Typo="Typo.h6">
 8            <MudIcon Icon="@Icons.Material.Filled.Bookmark" Class="mr-2" />
 9            New Arc
 10        </MudText>
 11    </TitleContent>
 12    <DialogContent>
 13        <MudTextField @bind-Value="_name"
 14                      Label="Arc Name"
 15                      Variant="Variant.Outlined"
 16                      Required="true"
 17                      RequiredError="Name is required"
 18                      Placeholder="e.g., Act 1, Chapter 1, The Beginning"
 19                      Class="mb-3"
 020                      @ref="_nameField"
 021                      Immediate="true"
 022                      OnKeyDown="@OnNameKeyDown" />
 023
 024        <MudTextField @bind-Value="_description"
 025                      Label="Description (optional)"
 26                      Variant="Variant.Outlined"
 27                      Lines="3"
 28                      Class="mb-3" />
 29
 30        <MudNumericField @bind-Value="_sortOrder"
 31                         Label="Sort Order"
 32                         Variant="Variant.Outlined"
 33                         Min="0"
 34                         HelperText="Lower numbers appear first" />
 35    </DialogContent>
 36    <DialogActions>
 37        <MudButton OnClick="Cancel">Cancel</MudButton>
 38        <MudButton Color="Color.Primary"
 39                   Variant="Variant.Filled"
 40                   OnClick="Submit"
 41                   Disabled="@(string.IsNullOrWhiteSpace(_name) || _isSubmitting)">
 042            @if (_isSubmitting)
 43            {
 44                <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" />
 45            }
 46            Create
 47        </MudButton>
 48    </DialogActions>
 49</MudDialog>
 50
 51@code {
 52    [CascadingParameter]
 053    private MudDialogInstance? MudDialog { get; set; }
 54
 55    [Parameter]
 056    public Guid CampaignId { get; set; }
 57
 058    private string _name = string.Empty;
 059    private string _description = string.Empty;
 60    private int _sortOrder = 0;
 61    private bool _isSubmitting = false;
 62    private MudTextField<string>? _nameField;
 63
 64    protected override async Task OnAfterRenderAsync(bool firstRender)
 65    {
 066        if (firstRender && _nameField != null)
 67        {
 068            await Task.Delay(100);
 069            await _nameField.FocusAsync();
 70        }
 071    }
 72
 073    private void Cancel() => MudDialog?.Cancel();
 74
 75    private async Task OnNameKeyDown(KeyboardEventArgs e)
 76    {
 077        if (e.Key == "Enter" && !string.IsNullOrWhiteSpace(_name) && !_isSubmitting)
 78        {
 079            await Submit();
 80        }
 081    }
 82
 83    private async Task Submit()
 84    {
 085        if (string.IsNullOrWhiteSpace(_name) || _isSubmitting) return;
 86
 087        _isSubmitting = true;
 088        StateHasChanged();
 89
 90        try
 91        {
 092            var createDto = new ArcCreateDto
 093            {
 094                Name = _name.Trim(),
 095                Description = string.IsNullOrWhiteSpace(_description) ? null : _description.Trim(),
 096                CampaignId = CampaignId,
 097                SortOrder = _sortOrder
 098            };
 99
 0100            var arc = await ArcApi.CreateArcAsync(createDto);
 0101            MudDialog?.Close(DialogResult.Ok(arc));
 0102        }
 0103        catch
 104        {
 0105            MudDialog?.Close(DialogResult.Cancel());
 0106        }
 107        finally
 108        {
 0109            _isSubmitting = false;
 110        }
 0111    }
 112}