diff --git a/app/components/release_note/note_form_component.rb b/app/components/release_note/note_form_component.rb index 86a19767449..a5d35af19f8 100644 --- a/app/components/release_note/note_form_component.rb +++ b/app/components/release_note/note_form_component.rb @@ -34,4 +34,14 @@ def categories_full_messages_errors def generate_incremental_index Time.current.to_f.to_i end + + def fieldset_element_id + dom_id(note, :fieldset_element) + end + + def delete_button + link_to t('.delete'), super_admins_release_note_path(group: note.group, id: note.id), + class: "fr-btn fr-btn--secondary fr-btn--sm fr-btn--icon-left fr-icon-delete-line", + data: { turbo: true, turbo_method: :delete, turbo_confirm: "Supprimer cette note ?" } + end end diff --git a/app/components/release_note/note_form_component/note_form_component.en.yml b/app/components/release_note/note_form_component/note_form_component.en.yml new file mode 100644 index 00000000000..fa956c3b45e --- /dev/null +++ b/app/components/release_note/note_form_component/note_form_component.en.yml @@ -0,0 +1,3 @@ +--- +en: + delete: Delete this note diff --git a/app/components/release_note/note_form_component/note_form_component.fr.yml b/app/components/release_note/note_form_component/note_form_component.fr.yml new file mode 100644 index 00000000000..7b5a48ce1f3 --- /dev/null +++ b/app/components/release_note/note_form_component/note_form_component.fr.yml @@ -0,0 +1,3 @@ +--- +fr: + delete: Supprimer cette note diff --git a/app/components/release_note/note_form_component/note_form_component.html.haml b/app/components/release_note/note_form_component/note_form_component.html.haml index e23649c0742..9c502d73b46 100644 --- a/app/components/release_note/note_form_component/note_form_component.html.haml +++ b/app/components/release_note/note_form_component/note_form_component.html.haml @@ -1,4 +1,4 @@ -.fr-fieldset__element +.fr-fieldset__element{ id: fieldset_element_id } .fr-background-alt--grey.fr-my-2w.fr-p-2w %fieldset.fr-fieldset.fr-mb-0 = fields_for "release_notes_attributes[#{index}]", note do |note_form| @@ -29,3 +29,7 @@ .fr-fieldset__element = render Dsfr::InputComponent.new(form: note_form, attribute: :body, input_type: :rich_text_area) + + .fr-fieldset__element.text-right + = delete_button + diff --git a/app/controllers/super_admins/release_notes_controller.rb b/app/controllers/super_admins/release_notes_controller.rb index df049c64ff9..ad2a435ea52 100644 --- a/app/controllers/super_admins/release_notes_controller.rb +++ b/app/controllers/super_admins/release_notes_controller.rb @@ -1,6 +1,6 @@ class SuperAdmins::ReleaseNotesController < ApplicationController before_action :authenticate_super_admin! - before_action :set_notes, only: [:edit, :update] + before_action :set_notes, only: [:edit, :update, :destroy] def index @release_notes_groups = ReleaseNote @@ -39,6 +39,25 @@ def update end end + def destroy + @note = ReleaseNote.find(params[:id]) + + # At least one note per announce group, otherwise we can't destroy by turbo and stays on the same page + if @notes.many? + @note.destroy! + else + flash[:alert] = t('.error') + end + + respond_to do |format| + format.html do + redirect_to edit_super_admins_release_note_path(@note.group), notice: t('.success') + end + + format.turbo_stream + end + end + def add_note @release_note = ReleaseNote.new(group: params[:group], published: false) @release_note.save(validate: false) diff --git a/app/views/super_admins/release_notes/destroy.turbo_stream.haml b/app/views/super_admins/release_notes/destroy.turbo_stream.haml new file mode 100644 index 00000000000..eff7119e92b --- /dev/null +++ b/app/views/super_admins/release_notes/destroy.turbo_stream.haml @@ -0,0 +1 @@ += turbo_stream.remove dom_id(@note, :fieldset_element) if @note.destroyed? diff --git a/config/locales/release_notes.en.yml b/config/locales/release_notes.en.yml index 5f6ef562823..f1046759240 100644 --- a/config/locales/release_notes.en.yml +++ b/config/locales/release_notes.en.yml @@ -8,6 +8,9 @@ en: update: success: Release notes were successfully saved. error: Release notes were not saved. + destroy: + success: Release note was successfully deleted. + error: "Can't destroy the last note of announce." activerecord: attributes: release_note: diff --git a/config/locales/release_notes.fr.yml b/config/locales/release_notes.fr.yml index 58f3cf94114..33e895d00cb 100644 --- a/config/locales/release_notes.fr.yml +++ b/config/locales/release_notes.fr.yml @@ -8,6 +8,9 @@ fr: update: error: Les annonces n’ont pas pu être sauvegardées. success: Les annonces ont été sauvegardées. + destroy: + success: La note a été supprimée. + error: "Vous ne pouvez pas supprimer la dernière note d’une annonce." activerecord: attributes: release_note: diff --git a/config/routes.rb b/config/routes.rb index d55f4a612c9..567b5627123 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -124,7 +124,7 @@ } namespace :super_admins do - resources :release_notes, only: [:index, :create, :edit, :update, :show], param: :group do + resources :release_notes, only: [:index, :create, :edit, :update, :destroy, :show], param: :group do member do post :add_note end diff --git a/spec/system/super_admins/release_notes_spec.rb b/spec/system/super_admins/release_notes_spec.rb index 778de373c53..72a44eba5ad 100644 --- a/spec/system/super_admins/release_notes_spec.rb +++ b/spec/system/super_admins/release_notes_spec.rb @@ -80,6 +80,32 @@ expect(note.categories).to eq(['expert']) end + scenario 'Delete a note', js: true, retry: 3 do + note1 = create(:release_note, body: "first note", group: "abc123") + note2 = create(:release_note, body: "second note", group: "abc123") + + visit edit_super_admins_release_note_path("abc123") + + expect(page).to have_content("first note") + + within(all('.fr-background-alt--grey')[0]) do + accept_confirm { click_link 'Supprimer cette note' } + end + + expect(page).not_to have_content("first note") + wait_until { + expect(ReleaseNote.exists?(note1.id)).to be_falsy + } + + within(all('.fr-background-alt--grey')[0]) do + accept_confirm { click_link 'Supprimer cette note' } + end + + expect(ReleaseNote.exists?(note2.id)).to be_truthy + expect(page).to have_content("Vous ne pouvez pas") + expect(page).to have_content("second note") + end + def fill_in_trix_editor(label, with:) within(".fr-input-group", text: label) do find('trix-editor').click.set(with)