From b0aa777315ce72ed75ed2c61ab4ce3de8f8e771e Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 13 Jul 2022 14:03:41 +0200 Subject: [PATCH] refactor(ujs): france_connect controller to use turbo --- .../france_connect/particulier_controller.rb | 16 +++------------- app/javascript/entrypoints/application.js | 7 +------ app/javascript/new_design/fc-fusion.js | 6 ------ .../particulier/_password_confirmation.html.haml | 2 +- .../france_connect/particulier/merge.html.haml | 14 +++++++------- ...merge_with_existing_account.turbo_stream.haml | 0 .../particulier/merge_with_new_account.js.erb | 3 --- .../merge_with_new_account.turbo_stream.haml | 4 ++++ .../particulier_controller_spec.rb | 6 +++--- 9 files changed, 19 insertions(+), 39 deletions(-) create mode 100644 app/views/france_connect/particulier/merge_with_existing_account.turbo_stream.haml delete mode 100644 app/views/france_connect/particulier/merge_with_new_account.js.erb create mode 100644 app/views/france_connect/particulier/merge_with_new_account.turbo_stream.haml diff --git a/app/controllers/france_connect/particulier_controller.rb b/app/controllers/france_connect/particulier_controller.rb index 13f41b6f1..5817a3185 100644 --- a/app/controllers/france_connect/particulier_controller.rb +++ b/app/controllers/france_connect/particulier_controller.rb @@ -53,7 +53,7 @@ class FranceConnect::ParticulierController < ApplicationController if !user.can_france_connect? flash.alert = t('errors.messages.france_connect.forbidden_html', reset_link: new_user_password_path) - render js: ajax_redirect(root_path) + redirect_to root_path else @fci.update(user: user) @fci.delete_merge_token! @@ -63,8 +63,6 @@ class FranceConnect::ParticulierController < ApplicationController end else flash.alert = t('france_connect.particulier.flash.invalid_password') - - render js: helpers.render_flash end end @@ -112,10 +110,7 @@ class FranceConnect::ParticulierController < ApplicationController if @fci.nil? || !@fci.valid_for_merge? flash.alert = t('france_connect.particulier.flash.merger_token_expired', application_name: APPLICATION_NAME) - respond_to do |format| - format.html { redirect_to root_path } - format.js { render js: ajax_redirect(root_path) } - end + redirect_to root_path end end @@ -134,12 +129,7 @@ class FranceConnect::ParticulierController < ApplicationController user.update_attribute('loged_in_with_france_connect', User.loged_in_with_france_connects.fetch(:particulier)) - redirection_location = stored_location_for(current_user) || root_path(current_user) - - respond_to do |format| - format.html { redirect_to redirection_location } - format.js { render js: ajax_redirect(root_path) } - end + redirect_to stored_location_for(current_user) || root_path(current_user) end def redirect_france_connect_error_connection diff --git a/app/javascript/entrypoints/application.js b/app/javascript/entrypoints/application.js index fb0f65240..3143e2dd9 100644 --- a/app/javascript/entrypoints/application.js +++ b/app/javascript/entrypoints/application.js @@ -28,11 +28,7 @@ import { acceptEmailSuggestion, discardEmailSuggestionBox } from '../new_design/user-sign_up'; -import { - showFusion, - showNewAccount, - showNewAccountPasswordConfirmation -} from '../new_design/fc-fusion'; +import { showFusion, showNewAccount } from '../new_design/fc-fusion'; const application = Application.start(); registerControllers(application); @@ -46,7 +42,6 @@ const DS = { showImportJustificatif, showFusion, showNewAccount, - showNewAccountPasswordConfirmation, replaceSemicolonByComma, acceptEmailSuggestion, discardEmailSuggestionBox diff --git a/app/javascript/new_design/fc-fusion.js b/app/javascript/new_design/fc-fusion.js index e9d4b7628..5d912c5d1 100644 --- a/app/javascript/new_design/fc-fusion.js +++ b/app/javascript/new_design/fc-fusion.js @@ -11,9 +11,3 @@ export function showNewAccount() { show(document.querySelector('.new-account')); hide(document.querySelector('.new-account-password-confirmation')); } - -export function showNewAccountPasswordConfirmation() { - hide(document.querySelector('.fusion')); - hide(document.querySelector('.new-account')); - show(document.querySelector('.new-account-password-confirmation')); -} diff --git a/app/views/france_connect/particulier/_password_confirmation.html.haml b/app/views/france_connect/particulier/_password_confirmation.html.haml index eda3ea1bc..0fa778734 100644 --- a/app/views/france_connect/particulier/_password_confirmation.html.haml +++ b/app/views/france_connect/particulier/_password_confirmation.html.haml @@ -3,7 +3,7 @@ %br = t('.fill_in_password') -= form_tag france_connect_particulier_merge_with_existing_account_path, remote: true, class: 'mt-2 form fconnect-form' do += form_tag france_connect_particulier_merge_with_existing_account_path, data: { turbo: true }, class: 'mt-2 form fconnect-form' do = hidden_field_tag :merge_token, merge_token = hidden_field_tag :email, email = label_tag :password, t('views.registrations.new.password_label', min_length: 8) diff --git a/app/views/france_connect/particulier/merge.html.haml b/app/views/france_connect/particulier/merge.html.haml index ce8fedec8..2fa14bab6 100644 --- a/app/views/france_connect/particulier/merge.html.haml +++ b/app/views/france_connect/particulier/merge.html.haml @@ -19,9 +19,9 @@ .fusion.hidden %p= t('.title_fill_in_password') - = form_tag france_connect_particulier_merge_with_existing_account_path, remote: true, class: 'mt-2 form fconnect-form' do - = hidden_field_tag :merge_token, @fci.merge_token - = hidden_field_tag :email, @fci.email_france_connect + = form_tag france_connect_particulier_merge_with_existing_account_path, data: { turbo: true }, class: 'mt-2 form fconnect-form' do + = hidden_field_tag :merge_token, @fci.merge_token, id: dom_id(@fci, :fusion_merge_token) + = hidden_field_tag :email, @fci.email_france_connect, id: dom_id(@fci, :fusion_email) = label_tag :password, t('views.registrations.new.password_label', min_length: 8) = password_field_tag :password, nil, autocomplete: 'current-password', class: 'mb-1' @@ -36,10 +36,10 @@ .new-account.hidden %p= t('.title_fill_in_email', application_name: APPLICATION_NAME) - = form_tag france_connect_particulier_merge_with_new_account_path, remote: true, class: 'mt-2 form' do - = hidden_field_tag :merge_token, @fci.merge_token - = label_tag :email, t('views.registrations.new.email_label') - = email_field_tag :email, "", required: true + = form_tag france_connect_particulier_merge_with_new_account_path, data: { turbo: true }, class: 'mt-2 form' do + = hidden_field_tag :merge_token, @fci.merge_token, id: dom_id(@fci, :new_account_merge_token) + = label_tag :email, t('views.registrations.new.email_label'), for: dom_id(@fci, :new_account_email) + = email_field_tag :email, "", required: true, id: dom_id(@fci, :new_account_email) = submit_tag t('.button_use_this_email'), class: 'button primary' diff --git a/app/views/france_connect/particulier/merge_with_existing_account.turbo_stream.haml b/app/views/france_connect/particulier/merge_with_existing_account.turbo_stream.haml new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/france_connect/particulier/merge_with_new_account.js.erb b/app/views/france_connect/particulier/merge_with_new_account.js.erb deleted file mode 100644 index cea1ca67a..000000000 --- a/app/views/france_connect/particulier/merge_with_new_account.js.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= render_to_element('.new-account-password-confirmation', partial: 'password_confirmation', locals: { email: @email, merge_token: @merge_token }) %> - -DS.showNewAccountPasswordConfirmation(); diff --git a/app/views/france_connect/particulier/merge_with_new_account.turbo_stream.haml b/app/views/france_connect/particulier/merge_with_new_account.turbo_stream.haml new file mode 100644 index 000000000..9340f6927 --- /dev/null +++ b/app/views/france_connect/particulier/merge_with_new_account.turbo_stream.haml @@ -0,0 +1,4 @@ += turbo_stream.update '.new-account-password-confirmation', partial: 'password_confirmation', locals: { email: @email, merge_token: @merge_token } += turbo_stream.hide_all '.fusion' += turbo_stream.hide_all '.new-account' += turbo_stream.show_all '.new-account-password-confirmation' diff --git a/spec/controllers/france_connect/particulier_controller_spec.rb b/spec/controllers/france_connect/particulier_controller_spec.rb index fd6c2cd27..76bbae30d 100644 --- a/spec/controllers/france_connect/particulier_controller_spec.rb +++ b/spec/controllers/france_connect/particulier_controller_spec.rb @@ -198,7 +198,7 @@ describe FranceConnect::ParticulierController, type: :controller do let(:merge_token) { fci.create_merge_token! } let(:email) { 'EXISTING_account@a.com ' } let(:password) { 'my-s3cure-p4ssword' } - let(:format) { :js } + let(:format) { :turbo_stream } subject { post :merge_with_existing_account, params: { merge_token: merge_token, email: email, password: password }, format: format } @@ -309,7 +309,7 @@ describe FranceConnect::ParticulierController, type: :controller do let(:fci) { FranceConnectInformation.create!(user_info) } let(:merge_token) { fci.create_merge_token! } let(:email) { ' Account@a.com ' } - let(:format) { :js } + let(:format) { :turbo_stream } subject { post :merge_with_new_account, params: { merge_token: merge_token, email: email }, format: format } @@ -323,7 +323,7 @@ describe FranceConnect::ParticulierController, type: :controller do expect(fci.user.email).to eq(email.downcase.strip) expect(fci.merge_token).to be_nil expect(controller.current_user).to eq(fci.user) - expect(response.body).to include("window.location.href='/'") + expect(response).to redirect_to(root_path) end end