< 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
100%
Covered lines: 3
Uncovered lines: 0
Coverable lines: 3
Total lines: 101
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Cancel()100%22100%

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"
 18                      @ref="_nameField"
 19                      Immediate="true"
 20                      OnKeyDown="@OnNameKeyDown" />
 21
 22        <MudTextField @bind-Value="_description"
 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)">
 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]
 44    private MudDialogInstance? MudDialog { get; set; }
 45
 46    [Parameter]
 47    public Guid WorldId { get; set; }
 48
 1349    private string _name = string.Empty;
 1350    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    {
 56        if (firstRender && _nameField != null)
 57        {
 58            await Task.Delay(100);
 59            await _nameField.FocusAsync();
 60        }
 61    }
 62
 263    private void Cancel() => MudDialog?.Cancel();
 64
 65    private async Task OnNameKeyDown(KeyboardEventArgs e)
 66    {
 67        if (e.Key == "Enter" && !string.IsNullOrWhiteSpace(_name) && !_isSubmitting)
 68        {
 69            await Submit();
 70        }
 71    }
 72
 73    private async Task Submit()
 74    {
 75        if (string.IsNullOrWhiteSpace(_name) || _isSubmitting) return;
 76
 77        _isSubmitting = true;
 78        StateHasChanged();
 79
 80        try
 81        {
 82            var createDto = new CampaignCreateDto
 83            {
 84                Name = _name.Trim(),
 85                Description = string.IsNullOrWhiteSpace(_description) ? null : _description.Trim(),
 86                WorldId = WorldId
 87            };
 88
 89            var campaign = await CampaignApi.CreateCampaignAsync(createDto);
 90            MudDialog?.Close(DialogResult.Ok(campaign));
 91        }
 92        catch
 93        {
 94            MudDialog?.Close(DialogResult.Cancel());
 95        }
 96        finally
 97        {
 98            _isSubmitting = false;
 99        }
 100    }
 101}

Methods/Properties

.ctor()
Cancel()