diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index cce7d7dd9..c2c3c8cfa 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -113,6 +113,7 @@ //= require globalize //= require settings //= require cookies +//= require cookies_consent //= require columns_selector //= require budget_edit_associations //= require budget_hide_money @@ -179,6 +180,7 @@ var initialize_modules = function() { App.SDGRelatedListSelector.initialize(); App.SDGManagementRelationSearch.initialize(); App.AuthenticityTokenRefresh.initialize(); + App.CookiesConsent.initialize(); }; var destroy_non_idempotent_modules = function() { diff --git a/app/assets/javascripts/cookies_consent.js b/app/assets/javascripts/cookies_consent.js new file mode 100644 index 000000000..29085daf8 --- /dev/null +++ b/app/assets/javascripts/cookies_consent.js @@ -0,0 +1,14 @@ +(function() { + "use strict"; + App.CookiesConsent = { + hide: function() { + $("#cookies_consent_banner").hide(); + }, + initialize: function() { + $(".accept-essential-cookies").on("click", function() { + App.Cookies.saveCookie("cookies_consent", "essential", 365); + App.CookiesConsent.hide(); + }); + } + }; +}).call(this); diff --git a/app/assets/stylesheets/layout/cookies_consent_banner.scss b/app/assets/stylesheets/layout/cookies_consent_banner.scss index ed8018bb1..56fca5fc7 100644 --- a/app/assets/stylesheets/layout/cookies_consent_banner.scss +++ b/app/assets/stylesheets/layout/cookies_consent_banner.scss @@ -7,4 +7,16 @@ position: fixed; right: $line-height / 3; z-index: 20; + + button { + @include breakpoint(small only) { + display: block; + width: 100%; + } + + &.accept-essential-cookies { + @include regular-button; + margin-bottom: 0; + } + } } diff --git a/app/components/layout/cookies_consent/banner_component.html.erb b/app/components/layout/cookies_consent/banner_component.html.erb index cc85fd86e..cee08ff60 100644 --- a/app/components/layout/cookies_consent/banner_component.html.erb +++ b/app/components/layout/cookies_consent/banner_component.html.erb @@ -1,4 +1,5 @@ diff --git a/app/components/layout/cookies_consent/banner_component.rb b/app/components/layout/cookies_consent/banner_component.rb index 108e1c66b..da08b3e05 100644 --- a/app/components/layout/cookies_consent/banner_component.rb +++ b/app/components/layout/cookies_consent/banner_component.rb @@ -1,5 +1,13 @@ class Layout::CookiesConsent::BannerComponent < ApplicationComponent + delegate :cookies, to: :helpers + def render? - feature?(:cookies_consent) + feature?(:cookies_consent) && cookies_consent_unset? end + + private + + def cookies_consent_unset? + cookies["cookies_consent"].blank? + end end diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index a74ebb33f..25cf43bc4 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -934,5 +934,6 @@ en: comments_summary: "Comments summary" info_text: "Content generated by AI / Machine Learning" cookies_consent: + accept_essential_cookies: "Accept essential cookies" message: "Cookies help us deliver our services. By using our services, you agree to our use of cookies." title: "Cookies policy" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 8a574e0f4..89d6e7491 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -934,5 +934,6 @@ es: comments_summary: "Resumen de comentarios" info_text: "Contenido generado mediante IA / Machine Learning" cookies_consent: + accept_essential_cookies: "Aceptar cookies esenciales" message: "Las cookies nos ayudan a ofrecer nuestros servicios. Al utilizar nuestros servicios, aceptas el uso de cookies." title: "PolĂ­tica de cookies" diff --git a/spec/components/layout/cookies_consent/banner_component_spec.rb b/spec/components/layout/cookies_consent/banner_component_spec.rb index fb95ed35b..235856dfa 100644 --- a/spec/components/layout/cookies_consent/banner_component_spec.rb +++ b/spec/components/layout/cookies_consent/banner_component_spec.rb @@ -3,6 +3,14 @@ require "rails_helper" describe Layout::CookiesConsent::BannerComponent do before { Setting["feature.cookies_consent"] = true } + it "does not render the banner when cookies were accepted" do + vc_test_request.cookies[:cookies_consent] = "essential" + + render_inline Layout::CookiesConsent::BannerComponent.new + + expect(page).not_to be_rendered + end + it "does not render the banner when feature `cookies_consent` is disabled" do Setting["feature.cookies_consent"] = nil @@ -11,10 +19,11 @@ describe Layout::CookiesConsent::BannerComponent do expect(page).not_to be_rendered end - it "renders the banner content when feature `cookies_consent` is enabled" do + it "renders the banner content when feature `cookies_consent` is enabled and cookies were not accepted" do render_inline Layout::CookiesConsent::BannerComponent.new expect(page).to be_rendered expect(page).to have_css "h2", text: "Cookies policy" + expect(page).to have_button "Accept essential cookies" end end diff --git a/spec/system/cookies_consent_spec.rb b/spec/system/cookies_consent_spec.rb index 83bc63e00..8a1af460b 100644 --- a/spec/system/cookies_consent_spec.rb +++ b/spec/system/cookies_consent_spec.rb @@ -3,13 +3,22 @@ require "rails_helper" describe "Cookies consent" do before { Setting["feature.cookies_consent"] = true } - scenario "Shows the cookies consent banner and for consecutive visits" do - visit root_path + context "Banner consent" do + scenario "Hides the banner when accepting essential cookies and for consecutive visits" do + visit root_path - expect(page).to have_css "h2", text: "Cookies policy" + expect(cookie_by_name("cookies_consent")).to be nil - refresh + within ".cookies-consent-banner" do + click_button "Accept essential cookies" + end - expect(page).to have_css "h2", text: "Cookies policy" + expect(cookie_by_name("cookies_consent")[:value]).to eq "essential" + expect(page).not_to have_content "Cookies policy" + + refresh + + expect(page).not_to have_content "Cookies policy" + end end end