From a9d4ff85e6b7a95e46e228b0183e562a165f4993 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 12 Jan 2023 17:55:01 +0100 Subject: [PATCH 1/2] refactor(js): more autosubmit controllers --- .../experts_procedures/index.html.haml | 12 ++++++------ .../_instructeurs_self_management.html.haml | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/views/administrateurs/experts_procedures/index.html.haml b/app/views/administrateurs/experts_procedures/index.html.haml index 68ebef395..f3df79249 100644 --- a/app/views/administrateurs/experts_procedures/index.html.haml +++ b/app/views/administrateurs/experts_procedures/index.html.haml @@ -15,8 +15,8 @@ method: :put, url: allow_expert_review_admin_procedure_path(@procedure), html: { class: 'form procedure-form__column--form no-background' } do |f| - %label.toggle-switch - = f.check_box :allow_expert_review, class: 'toggle-switch-checkbox', onchange: 'this.form.requestSubmit()' + %label.toggle-switch{ data: { controller: 'autosubmit' } } + = f.check_box :allow_expert_review, class: 'toggle-switch-checkbox' %span.toggle-switch-control.round %span.toggle-switch-label.on %span.toggle-switch-label.off @@ -29,8 +29,8 @@ method: :put, url: experts_require_administrateur_invitation_admin_procedure_path(@procedure), html: { class: 'form procedure-form__column--form no-background' } do |f| - %label.toggle-switch - = f.check_box :experts_require_administrateur_invitation, class: 'toggle-switch-checkbox', onchange: 'this.form.requestSubmit()' + %label.toggle-switch{ data: { controller: 'autosubmit' } } + = f.check_box :experts_require_administrateur_invitation, class: 'toggle-switch-checkbox' %span.toggle-switch-control.round %span.toggle-switch-label.on %span.toggle-switch-label.off @@ -79,8 +79,8 @@ method: :put, data: { turbo: true }, html: { class: 'form procedure-form__column--form no-background' } do |f| - %label.toggle-switch - = f.check_box :allow_decision_access, class: 'toggle-switch-checkbox', onchange: 'this.form.requestSubmit()' + %label.toggle-switch{ data: { controller: 'autosubmit' } } + = f.check_box :allow_decision_access, class: 'toggle-switch-checkbox' %span.toggle-switch-control.round %span.toggle-switch-label.on %span.toggle-switch-label.off diff --git a/app/views/administrateurs/groupe_instructeurs/_instructeurs_self_management.html.haml b/app/views/administrateurs/groupe_instructeurs/_instructeurs_self_management.html.haml index ee20f9d5a..5b85b3504 100644 --- a/app/views/administrateurs/groupe_instructeurs/_instructeurs_self_management.html.haml +++ b/app/views/administrateurs/groupe_instructeurs/_instructeurs_self_management.html.haml @@ -5,9 +5,10 @@ = form_for procedure, method: :patch, url: update_instructeurs_self_management_enabled_admin_procedure_groupe_instructeurs_path(procedure), + data: { controller: 'autosubmit', turbo: 'true' }, html: { class: 'form procedure-form__column--form no-background' } do |f| %label.toggle-switch - = f.check_box :instructeurs_self_management_enabled, class: 'toggle-switch-checkbox', onchange: 'this.form.submit()' + = f.check_box :instructeurs_self_management_enabled, class: 'toggle-switch-checkbox' %span.toggle-switch-control.round %span.toggle-switch-label.on %span.toggle-switch-label.off From a0878ffde5dfa49f6bb179a40d645a28644bbea5 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 12 Jan 2023 17:57:41 +0100 Subject: [PATCH 2/2] feat(turbo): prevent scroll on form submits --- .../controllers/turbo_controller.ts | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/app/javascript/controllers/turbo_controller.ts b/app/javascript/controllers/turbo_controller.ts index 2c4e424ff..730fcd4ff 100644 --- a/app/javascript/controllers/turbo_controller.ts +++ b/app/javascript/controllers/turbo_controller.ts @@ -1,4 +1,5 @@ import { show, hide } from '@utils'; +import { session as TurboSession } from '@hotwired/turbo'; import { ApplicationController } from './application_controller'; @@ -8,16 +9,34 @@ export class TurboController extends ApplicationController { declare readonly spinnerTarget: HTMLElement; declare readonly hasSpinnerTarget: boolean; + #submitting = true; + connect() { - this.onGlobal('turbo:submit-start', () => { - if (this.hasSpinnerTarget) { - show(this.spinnerTarget); - } - }); - this.onGlobal('turbo:submit-end', () => { - if (this.hasSpinnerTarget) { - hide(this.spinnerTarget); - } - }); + this.onGlobal('turbo:submit-start', () => this.startSpinner()); + this.onGlobal('turbo:submit-end', () => this.stopSpinner()); + this.onGlobal('turbo:fetch-request-error', () => this.stopSpinner()); + + // prevent scroll on turbo form submits + this.onGlobal('turbo:render', () => this.preventScrollIfNeeded()); + } + + startSpinner() { + this.#submitting = true; + if (this.hasSpinnerTarget) { + show(this.spinnerTarget); + } + } + + stopSpinner() { + this.#submitting = false; + if (this.hasSpinnerTarget) { + hide(this.spinnerTarget); + } + } + + preventScrollIfNeeded() { + if (this.#submitting && TurboSession.navigator.currentVisit) { + TurboSession.navigator.currentVisit.scrolled = true; + } } }