Skip to content

Commit

Permalink
Refactoring ConfirmDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Jan 15, 2025
1 parent aa88fba commit 04b4a86
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Stories TComponent="ConfirmDialog" Layout="typeof(StubLayout)">
<Story Name="Default">
<Template>
<ConfirmDialog OnConfirm="() => { }" @attributes="context.Args" @ref="_dialog">
<ConfirmDialog @attributes="context.Args" @ref="_dialog">
Are you sure?
</ConfirmDialog>
</Template>
Expand All @@ -12,10 +12,6 @@

@code {
private ConfirmDialog? _dialog;

protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
_dialog?.Open();
}

protected override Task OnAfterRenderAsync(bool firstRender) => _dialog?.IsConfirmed() ?? Task.CompletedTask;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
@code {
private AcceptCookieDialog? _dialog;

protected override Task OnAfterRenderAsync(bool firstRender) => _dialog?.Open() ?? Task.CompletedTask;
protected override Task OnAfterRenderAsync(bool firstRender) => _dialog?.IsAccepted() ?? Task.CompletedTask;
}
26 changes: 11 additions & 15 deletions src/Inc.TeamAssistant.WebUI/Components/ConfirmDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@ChildContent
</div>
<div class="confirm-dialog__actions">
<Button OnClick="() => Close(OnConfirm)">@Localizer["Yes"]</Button>
<Button OnClick="() => Close()" ComponentStyle="ComponentStyle.Secondary">@Localizer["No"]</Button>
<Button OnClick="() => Close(true)">@Localizer["Yes"]</Button>
<Button OnClick="() => Close(false)" ComponentStyle="ComponentStyle.Secondary">@Localizer["No"]</Button>
</div>
</div>
</div>
Expand All @@ -21,30 +21,26 @@
/// HTML content
/// </summary>
[Parameter, EditorRequired]
public RenderFragment ChildContent { get; set; } = default!;

/// <summary>
/// On confirm event
/// </summary>
[Parameter, EditorRequired]
public EventCallback OnConfirm { get; set; }
public RenderFragment ChildContent { get; set; } = null!;

private bool _isOpen;
private TaskCompletionSource<bool>? _state;

public void Open()
public Task<bool> IsConfirmed()
{
_isOpen = true;

StateHasChanged();

_state = new TaskCompletionSource<bool>();

return _state.Task;
}

private async Task Close(EventCallback? confirm = null)
private void Close(bool confirmed)
{
_isOpen = false;

if (confirm.HasValue)
await confirm.Value.InvokeAsync();
else
StateHasChanged();
_state?.SetResult(confirmed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
WebsiteSection="WebsiteSection.Constructor"
MetaOverrideTitle="@Localizer["Title"]" />

<ConfirmDialog OnConfirm="Remove" @ref="_confirmDialog">
<p>@string.Format(Localizer["RemoveConfirmationText"], _selectedBot?.Name)</p>
<ConfirmDialog @ref="_confirmDialog">
<p>@string.Format(Localizer["RemoveConfirmationText"], _botName)</p>
</ConfirmDialog>

<AuthorizeView>
Expand All @@ -36,7 +36,7 @@
private IReadOnlyCollection<BotDto> _bots = Array.Empty<BotDto>();

private ConfirmDialog? _confirmDialog;
private BotDto? _selectedBot;
private string? _botName;

protected override Task OnParametersSetAsync() => Load();

Expand Down Expand Up @@ -65,11 +65,12 @@
await NavRouter.MoveToRoute(routeSegment);
}

private void MoveToRemove(BotDto bot)
private async Task MoveToRemove(BotDto bot)
{
_selectedBot = bot;

_confirmDialog?.Open();
_botName = bot.Name;

if (_confirmDialog is not null && await _confirmDialog.IsConfirmed())
await Removed(bot.Id);
}

private string CreateMoveToDashboardLink(BotDto bot)
Expand All @@ -81,12 +82,9 @@
: NavRouter.CreateRoute($"dashboard/{bot.Id:N}");
}

private async Task Remove()
private async Task Removed(Guid botId)
{
if (_selectedBot is null)
return;

await BotService.Remove(_selectedBot.Id);
await BotService.Remove(botId);

await Load();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@inject IStringLocalizer<DashboardResources> Localizer

<ConfirmDialog OnConfirm="Disable" @ref="_confirmDialog">
<ConfirmDialog @ref="_confirmDialog">
<p>@Localizer["AppraiserIntegrationConfirmText"]</p>
</ConfirmDialog>

Expand Down Expand Up @@ -83,13 +83,13 @@
/// Form model.
/// </summary>
[Parameter, EditorRequired]
public AppraiserIntegrationFromModel FormModel { get; set; } = default!;
public AppraiserIntegrationFromModel FormModel { get; set; } = null!;

/// <summary>
/// Component state.
/// </summary>
[Parameter, EditorRequired]
public LoadingState State { get; set; } = default!;
public LoadingState State { get; set; } = null!;

/// <summary>
/// On disabled event.
Expand All @@ -107,7 +107,7 @@
/// Action for retry.
/// </summary>
[Parameter, EditorRequired]
public Func<Task> Retry { get; set; } = default!;
public Func<Task> Retry { get; set; } = null!;

private ConfirmDialog? _confirmDialog;
private FluentValidationValidator? _fluentValidationValidator;
Expand All @@ -118,18 +118,10 @@
_editContext = EditContextFactory.Create(FormModel);
}

private void BeginDisable()
private async Task BeginDisable()
{
if (FormModel.IsEnabled)
_confirmDialog?.Open();
}

private async Task Disable()
{
if (!TeamId.HasValue)
return;

await OnDisabled.InvokeAsync();
if (FormModel.IsEnabled && _confirmDialog is not null && await _confirmDialog.IsConfirmed())
await OnDisabled.InvokeAsync();
}

private async Task SubmitForm()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@inject IStringLocalizer<DashboardResources> Localizer

<ConfirmDialog OnConfirm="EmitTeammateRemoved" @ref="_confirmDialog">
<ConfirmDialog @ref="_confirmDialog">
@if (_isDelete || _deleteUntil.HasValue)
{
<p>@Localizer["ConfirmDeleteTeammate"] @_selectedTeammate?.Name</p>
Expand Down Expand Up @@ -82,19 +82,19 @@
/// Component state.
/// </summary>
[Parameter, EditorRequired]
public LoadingState State { get; set; } = default!;
public LoadingState State { get; set; } = null!;

/// <summary>
/// Teammates data.
/// </summary>
[Parameter, EditorRequired]
public GetTeammatesResult Item { get; set; } = default!;
public GetTeammatesResult Item { get; set; } = null!;

/// <summary>
/// Action for retry.
/// </summary>
[Parameter, EditorRequired]
public Func<Task> Retry { get; set; } = default!;
public Func<Task> Retry { get; set; } = null!;

/// <summary>
/// On confirm event
Expand All @@ -109,22 +109,24 @@

private IQueryable<TeammateDto> Teammates => Item.Teammates.AsQueryable();

private void DeleteTeammate(TeammateDto teammate, int? leaveUntil)
private async Task DeleteTeammate(TeammateDto teammate, int? leaveUntil)
{
_selectedTeammate = teammate;
_deleteUntil = leaveUntil;
_isDelete = !leaveUntil.HasValue;

_confirmDialog?.Open();
if (_confirmDialog is not null && await _confirmDialog.IsConfirmed())
await EmitTeammateRemoved();
}

private void RecoveryTeammate(TeammateDto teammate)
private async Task RecoveryTeammate(TeammateDto teammate)
{
_selectedTeammate = teammate;
_deleteUntil = null;
_isDelete = false;

_confirmDialog?.Open();
if (_confirmDialog is not null && await _confirmDialog.IsConfirmed())
await EmitTeammateRemoved();
}

private async Task EmitTeammateRemoved()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

@code {
private bool _isOpen = false;
private readonly TaskCompletionSource<object> _state = new();
private readonly TaskCompletionSource<bool> _state = new();

public Task Open()
public Task<bool> IsAccepted()
{
_isOpen = true;

Expand All @@ -29,6 +29,6 @@
{
_isOpen = false;

_state.SetResult(null!);
_state.SetResult(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@code {
private IJSObjectReference? _cookieModule;

private AcceptCookieDialog _dialog = null!;
private AcceptCookieDialog? _dialog;
private readonly string _rightsCookieName = "rights";
private readonly string _rightsCookieValue = "accept";
private readonly int _rightsCookieLifetime = 365;
Expand All @@ -22,12 +22,9 @@
"./Features/Layouts/AcceptCookieDialogContainer.razor.js");
var rightsValue = await _cookieModule.InvokeAsync<string>("readCookie", _rightsCookieName);

if (!_rightsCookieValue.Equals(rightsValue, StringComparison.InvariantCultureIgnoreCase))
{
await _dialog.Open();

if (!_rightsCookieValue.Equals(rightsValue, StringComparison.InvariantCultureIgnoreCase)
&& _dialog is not null && await _dialog.IsAccepted())
await Accepted();
}
}
}

Expand Down

0 comments on commit 04b4a86

Please sign in to comment.