Skip to content

Commit

Permalink
Refactoring AcceptCookieDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Jan 14, 2025
1 parent 3a43155 commit aa88fba
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<Stories TComponent="AcceptCookieDialog">
<Story Name="Default">
<Template>
<AcceptCookieDialog IsShow="@_isShow" Accepted="HideDialog" />
<AcceptCookieDialog @ref="_dialog" />
</Template>
</Story>
</Stories>

@code {
private bool _isShow = true;
private void HideDialog() => _isShow = false;
private AcceptCookieDialog? _dialog;

protected override Task OnAfterRenderAsync(bool firstRender) => _dialog?.Open() ?? Task.CompletedTask;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5101",
"applicationUrl": "http://localhost:8001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@inject IStringLocalizer<LayoutResources> Localizer

@if (IsShow)
@if (_isOpen)
{
<div class="inline-dialog">
<div class="inline-dialog__content">
Expand All @@ -13,15 +13,22 @@
}

@code {
/// <summary>
/// Visualizes the dialog.
/// </summary>
[Parameter, EditorRequired]
public bool IsShow { get; set; }

/// <summary>
/// Accepted event.
/// </summary>
[Parameter, EditorRequired]
public EventCallback Accepted { get; set; }
private bool _isOpen = false;
private readonly TaskCompletionSource<object> _state = new();

public Task Open()
{
_isOpen = true;

StateHasChanged();

return _state.Task;
}

private void Accepted()
{
_isOpen = false;

_state.SetResult(null!);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
@inject IRenderContext RenderContext
@inject IJSRuntime JsRuntime

<AcceptCookieDialog IsShow="_isShow" Accepted="Accept" />
<AcceptCookieDialog @ref="_dialog" />

@code {
private IJSObjectReference? _cookieModule;
private bool _isShow;

private AcceptCookieDialog _dialog = null!;
private readonly string _rightsCookieName = "rights";
private readonly string _rightsCookieValue = "accept";
private readonly int _rightsCookieLifetime = 365;
Expand All @@ -20,16 +20,18 @@
_cookieModule = await JsRuntime.InvokeAsync<IJSObjectReference>(
"import",
"./Features/Layouts/AcceptCookieDialogContainer.razor.js");

var rightsValue = await _cookieModule.InvokeAsync<string>("readCookie", _rightsCookieName);

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

StateHasChanged();
await Accepted();
}
}
}

private async Task Accept()
private async Task Accepted()
{
if (_cookieModule is null)
return;
Expand All @@ -39,8 +41,6 @@
_rightsCookieName,
_rightsCookieValue,
_rightsCookieLifetime);

_isShow = false;
}

public async ValueTask DisposeAsync()
Expand Down

0 comments on commit aa88fba

Please sign in to comment.