< Summary

Information
Class: Chronicis.Client.Components.Dialogs.CreateCampaignDialog
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Components/Dialogs/CreateCampaignDialog.razor
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 101
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_WorldId()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/CreateCampaignDialog.razor

#LineLine coverage
 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"
 018                      @ref="_nameField"
 019                      Immediate="true"
 020                      OnKeyDown="@OnNameKeyDown" />
 021
 022        <MudTextField @bind-Value="_description"
 023                      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)">
 033            @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]
 044    private MudDialogInstance? MudDialog { get; set; }
 45
 46    [Parameter]
 047    public Guid WorldId { get; set; }
 48
 049    private string _name = string.Empty;
 050    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    {
 056        if (firstRender && _nameField != null)
 57        {
 058            await Task.Delay(100);
 059            await _nameField.FocusAsync();
 60        }
 061    }
 62
 063    private void Cancel() => MudDialog?.Cancel();
 64
 65    private async Task OnNameKeyDown(KeyboardEventArgs e)
 66    {
 067        if (e.Key == "Enter" && !string.IsNullOrWhiteSpace(_name) && !_isSubmitting)
 68        {
 069            await Submit();
 70        }
 071    }
 72
 73    private async Task Submit()
 74    {
 075        if (string.IsNullOrWhiteSpace(_name) || _isSubmitting) return;
 76
 077        _isSubmitting = true;
 078        StateHasChanged();
 79
 80        try
 81        {
 082            var createDto = new CampaignCreateDto
 083            {
 084                Name = _name.Trim(),
 085                Description = string.IsNullOrWhiteSpace(_description) ? null : _description.Trim(),
 086                WorldId = WorldId
 087            };
 88
 089            var campaign = await CampaignApi.CreateCampaignAsync(createDto);
 090            MudDialog?.Close(DialogResult.Ok(campaign));
 091        }
 092        catch
 93        {
 094            MudDialog?.Close(DialogResult.Cancel());
 095        }
 96        finally
 97        {
 098            _isSubmitting = false;
 99        }
 0100    }
 101}