diff --git a/app/components/dsfr/notice_component.rb b/app/components/dsfr/notice_component.rb index 557f5a20e..44cae0b70 100644 --- a/app/components/dsfr/notice_component.rb +++ b/app/components/dsfr/notice_component.rb @@ -2,11 +2,18 @@ class Dsfr::NoticeComponent < ApplicationComponent renders_one :title - def initialize(closable: false) + attr_reader :data_attributes + + def initialize(closable: false, data_attributes: {}) @closable = closable + @data_attributes = data_attributes end def closable? !!@closable end + + def notice_data_attributes + { "data-dsfr-header-target": "notice" }.merge(data_attributes) + end end diff --git a/app/components/dsfr/notice_component/notice_component.html.haml b/app/components/dsfr/notice_component/notice_component.html.haml index 3ff0e4642..a99303599 100644 --- a/app/components/dsfr/notice_component/notice_component.html.haml +++ b/app/components/dsfr/notice_component/notice_component.html.haml @@ -1,4 +1,4 @@ -.fr-notice.fr-notice--info{ "data-dsfr-header-target": "notice" } +.fr-notice.fr-notice--info{ **notice_data_attributes } .fr-container .fr-notice__body %p.fr-notice__title diff --git a/app/components/switch_domain_banner_component.rb b/app/components/switch_domain_banner_component.rb index 49104e4f6..b9bf0a2ef 100644 --- a/app/components/switch_domain_banner_component.rb +++ b/app/components/switch_domain_banner_component.rb @@ -9,8 +9,7 @@ class SwitchDomainBannerComponent < ApplicationComponent def render? return false unless helpers.switch_domain_enabled?(request) - - # TODO if preferred hosts + return false if user&.preferred_domain_demarches_gouv_fr? && requested_from_new_domain? true end @@ -27,6 +26,8 @@ class SwitchDomainBannerComponent < ApplicationComponent helpers.url_for(url_options) end + def requested_from_new_domain? + Current.host == ApplicationHelper::APP_HOST end private diff --git a/app/components/switch_domain_banner_component/switch_domain_banner_component.html.haml b/app/components/switch_domain_banner_component/switch_domain_banner_component.html.haml index 8a8f07136..efd5db5cb 100644 --- a/app/components/switch_domain_banner_component/switch_domain_banner_component.html.haml +++ b/app/components/switch_domain_banner_component/switch_domain_banner_component.html.haml @@ -8,7 +8,8 @@ // TODO: notify us in order to monitor connectivity issues volume }) -= render Dsfr::NoticeComponent.new(closable: true) do |c| + += render Dsfr::NoticeComponent.new(closable: true, data_attributes: { "data-switch-domain-notice" => true }) do |c| - c.with_title do = t(".message_new_domain") = "#{helpers.link_to APPLICATION_NAME, new_host_url}." @@ -17,3 +18,10 @@ = t(".follow_link") - elsif auto_switch? || true = t(".detected_error") + +- if user && !user.preferred_domain_demarches_gouv_fr? && requested_from_new_domain? + = form_tag(helpers.preferred_domain_path, method: :patch, remote: true, name: "update-preferred-domain") + :javascript + document.addEventListener('noticeClosed', function(e) { + document.forms['update-preferred-domain'].submit(); + }); diff --git a/app/controllers/users/profil_controller.rb b/app/controllers/users/profil_controller.rb index 2e039d025..ee2e90e27 100644 --- a/app/controllers/users/profil_controller.rb +++ b/app/controllers/users/profil_controller.rb @@ -63,6 +63,12 @@ module Users redirect_to profil_path end + def preferred_domain + current_user.update_preferred_domain(request.host_with_port) + + head :no_content + end + private def find_transfers diff --git a/app/javascript/controllers/dsfr_header_controller.ts b/app/javascript/controllers/dsfr_header_controller.ts index 7015ef29a..884b01f68 100644 --- a/app/javascript/controllers/dsfr_header_controller.ts +++ b/app/javascript/controllers/dsfr_header_controller.ts @@ -9,5 +9,8 @@ export class DSFRHeaderController extends ApplicationController { this.noticeTarget.parentNode?.removeChild(this.noticeTarget); this.element.classList.remove('fr-header__with-notice-info'); + + const event = new CustomEvent('noticeClosed', { bubbles: true }); + this.element.dispatchEvent(event); } } diff --git a/app/models/concerns/domain_migratable_concern.rb b/app/models/concerns/domain_migratable_concern.rb index 181ca93c6..65be5f723 100644 --- a/app/models/concerns/domain_migratable_concern.rb +++ b/app/models/concerns/domain_migratable_concern.rb @@ -5,5 +5,14 @@ module DomainMigratableConcern enum preferred_domain: { demarches_gouv_fr: 0, demarches_simplifiees_fr: 1 }, _prefix: true validates :preferred_domain, inclusion: { in: User.preferred_domains.keys, allow_nil: true } + + def update_preferred_domain(host) + case host + when ApplicationHelper::APP_HOST + preferred_domain_demarches_gouv_fr! + when ApplicationHelper::APP_HOST_LEGACY + preferred_domain_demarches_simplifiees_fr! + end + end end end diff --git a/app/models/user.rb b/app/models/user.rb index 19d829aec..955f8cd33 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ApplicationRecord + include DomainMigratableConcern include EmailSanitizableConcern include PasswordComplexityConcern diff --git a/config/routes.rb b/config/routes.rb index 1ea777bb2..171fbc67c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -386,6 +386,7 @@ Rails.application.routes.draw do post 'accept_merge' => 'profil#accept_merge' post 'refuse_merge' => 'profil#refuse_merge' delete 'france_connect_information' => 'profil#destroy_fci' + patch 'preferred_domain', to: 'profil#preferred_domain' get 'fermeture/:path', to: 'commencer#closing_details', as: :closing_details end