diff --git a/app/controllers/champs/repetition_controller.rb b/app/controllers/champs/repetition_controller.rb index 8ed626fb2..b96f5e8ea 100644 --- a/app/controllers/champs/repetition_controller.rb +++ b/app/controllers/champs/repetition_controller.rb @@ -1,8 +1,14 @@ class Champs::RepetitionController < ApplicationController before_action :authenticate_logged_user! - def show + def add @champ = policy_scope(Champ).includes(:champs).find(params[:champ_id]) @champs = @champ.add_row end + + def remove + champ = policy_scope(Champ).includes(:champs).find(params[:champ_id]) + champ.champs.where(id: params[:champ_ids]).destroy_all + @row_id = params[:row_id] + end end diff --git a/app/javascript/new_design/champs/repetition.js b/app/javascript/new_design/champs/repetition.js deleted file mode 100644 index 3fb740666..000000000 --- a/app/javascript/new_design/champs/repetition.js +++ /dev/null @@ -1,33 +0,0 @@ -import { delegate, fire } from '@utils'; - -const CHAMP_SELECTOR = '.editable-champ'; -const BUTTON_SELECTOR = '.button.remove-row'; -const DESTROY_INPUT_SELECTOR = 'input[type=hidden][name*=_destroy]'; -const DOM_ID_INPUT_SELECTOR = 'input[type=hidden][name*=deleted_row_dom_ids]'; - -delegate('click', BUTTON_SELECTOR, (evt) => { - evt.preventDefault(); - - const row = evt.target.closest('.row'); - - for (let input of row.querySelectorAll(DESTROY_INPUT_SELECTOR)) { - input.disabled = false; - input.value = true; - } - row.querySelector(DOM_ID_INPUT_SELECTOR).disabled = false; - - for (let champ of row.querySelectorAll(CHAMP_SELECTOR)) { - champ.remove(); - } - - evt.target.remove(); - row.classList.remove('row'); - - // We could debounce the autosave request, so that row removal would be batched - // with the next changes. - // However *adding* a new repetition row isn't debounced (changes are immediately - // effective server-side). - // So, to avoid ordering issues, enqueue an autosave request as soon as the row - // is removed. - fire(row, 'autosave:trigger'); -}); diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 945d83857..9e78eb30a 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -31,7 +31,6 @@ import '../new_design/dossiers/auto-save'; import '../new_design/dossiers/auto-upload'; import '../new_design/champs/linked-drop-down-list'; -import '../new_design/champs/repetition'; import '../new_design/champs/drop-down-list'; import { diff --git a/app/views/champs/repetition/add.turbo_stream.haml b/app/views/champs/repetition/add.turbo_stream.haml new file mode 100644 index 000000000..53b6c3d9d --- /dev/null +++ b/app/views/champs/repetition/add.turbo_stream.haml @@ -0,0 +1,2 @@ += fields_for @champ.input_name, @champ do |form| + = turbo_stream.append dom_id(@champ, :rows), partial: 'shared/dossiers/editable_champs/repetition_row', locals: { form: form, champ: @champ, row: @champs } diff --git a/app/views/champs/repetition/remove.turbo_stream.haml b/app/views/champs/repetition/remove.turbo_stream.haml new file mode 100644 index 000000000..82ed9d99a --- /dev/null +++ b/app/views/champs/repetition/remove.turbo_stream.haml @@ -0,0 +1 @@ += turbo_stream.remove @row_id diff --git a/app/views/champs/repetition/show.js.erb b/app/views/champs/repetition/show.js.erb deleted file mode 100644 index 1ac03b11c..000000000 --- a/app/views/champs/repetition/show.js.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= fields_for @champ.input_name, @champ do |form| %> - <%= append_to_element("##{@champ.input_group_id} .repetition", partial: 'shared/dossiers/editable_champs/repetition_row', locals: { form: form, champ: @champ, row: @champs }) %> -<% end %> diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index ed9d71b76..8a7048d6b 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -42,4 +42,8 @@ = yield :charts_js + // Container for custom turbo-stream actions %turbo-events + // Container for turbo form that we can submit from inside other forms + %div{ 'data-turbo': 'true' } + = form_tag('', id: :turbo_form) diff --git a/app/views/shared/dossiers/editable_champs/_repetition.html.haml b/app/views/shared/dossiers/editable_champs/_repetition.html.haml index 010289cb0..54e58d46f 100644 --- a/app/views/shared/dossiers/editable_champs/_repetition.html.haml +++ b/app/views/shared/dossiers/editable_champs/_repetition.html.haml @@ -1,12 +1,8 @@ -.repetition +.repetition{ id: dom_id(champ, :rows) } - champ.rows.each do |champs| = render partial: 'shared/dossiers/editable_champs/repetition_row', locals: { form: form, champ: champ, row: champs } -- if champ.persisted? - = link_to champs_repetition_path(champ.id), class: 'button add-row', data: { remote: true, disable: true, method: 'POST' } do - %span.icon.add - Ajouter un élément pour « #{champ.libelle} » -- else - %a.button.add-row +.actions{ 'data-turbo': 'true' } + = button_tag type: :submit, form: :turbo_form, formaction: champs_repetition_path(champ.id), formmethod: :post, class: 'button add-row' do %span.icon.add Ajouter un élément pour « #{champ.libelle} » diff --git a/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml b/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml index d416a3665..62a825530 100644 --- a/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml +++ b/app/views/shared/dossiers/editable_champs/_repetition_row.html.haml @@ -1,13 +1,9 @@ - row_dom_id = "row-#{SecureRandom.hex(4)}" .row{ id: row_dom_id } - -# Tell the controller which DOM element should be removed once the row deletion is successful - = hidden_field_tag 'deleted_row_dom_ids[]', row_dom_id, disabled: true - - row.each do |champ| = fields_for champ.input_name, champ do |form| = render partial: 'shared/dossiers/editable_champs/editable_champ', locals: { form: form, champ: champ } - = form.hidden_field :_destroy, disabled: true - .flex.row-reverse - %button.button.danger.remove-row{ type: :button } + .flex.row-reverse{ 'data-turbo': 'true' } + = button_tag type: :submit, form: :turbo_form, formaction: champs_repetition_path(champ.id, champ_ids: row.map(&:id), row_id: row_dom_id), formmethod: :delete, class: 'button danger remove-row' do Supprimer l’élément diff --git a/app/views/users/dossiers/brouillon.js.erb b/app/views/users/dossiers/brouillon.js.erb deleted file mode 100644 index bb354a490..000000000 --- a/app/views/users/dossiers/brouillon.js.erb +++ /dev/null @@ -1,3 +0,0 @@ -<% (params['deleted_row_dom_ids'] || []).each do |deleted_row_dom_id| %> - <%= remove_element('#' + deleted_row_dom_id) %> -<% end %> diff --git a/config/routes.rb b/config/routes.rb index 24d75f346..631eb778f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -144,7 +144,8 @@ Rails.application.routes.draw do get ':champ_id/siret', to: 'siret#show', as: :siret get ':champ_id/dossier_link', to: 'dossier_link#show', as: :dossier_link post ':champ_id/carte', to: 'carte#show', as: :carte - post ':champ_id/repetition', to: 'repetition#show', as: :repetition + post ':champ_id/repetition', to: 'repetition#add', as: :repetition + delete ':champ_id/repetition', to: 'repetition#remove' get ':champ_id/carte/features', to: 'carte#index', as: :carte_features post ':champ_id/carte/features', to: 'carte#create'