Fix #4936: add the cookie consent theme control.

This commit is contained in:
Ben
2025-02-22 09:49:33 +08:00
parent 7a4ea8cf1b
commit 982f3b1943
14 changed files with 322 additions and 0 deletions

View File

@ -26,9 +26,11 @@
<div class="container">
<div class="row px-4">
<Pane Name="@PaneNames.Admin" />
<CookieConsent />
</div>
</div>
</div>
</div>
@code {

View File

@ -0,0 +1,33 @@
@namespace Oqtane.Themes.Controls
@inherits ThemeControlBase
@inject ICookieConsentService CookieConsentService
@inject IJSRuntime JSRuntime
@inject IStringLocalizer<CookieConsent> Localizer
@if (showBanner)
{
<form method="post" @formname="CookieConsentForm" @onsubmit="async () => await AcceptPolicy()" data-enhance>
<input type="hidden" name="@Constants.RequestVerificationToken" value="@SiteState.AntiForgeryToken" />
@((MarkupString)Convert.ToString(Localizer["ConsentBody"]))
</form>
}
@code {
private bool showBanner;
protected override async Task OnInitializedAsync()
{
showBanner = !(await CookieConsentService.CanTrackAsync());
}
private async Task AcceptPolicy()
{
var cookieString = await CookieConsentService.CreateConsentCookieAsync();
if (!string.IsNullOrEmpty(cookieString))
{
var interop = new Interop(JSRuntime);
await interop.SetCookieString(cookieString);
showBanner = false;
}
}
}

View File

@ -107,6 +107,7 @@
{
<Pane Name="Footer" />
}
<CookieConsent />
</div>
</main>