Skip to content

Commit

Permalink
feat(announce): can destroy a note
Browse files Browse the repository at this point in the history
  • Loading branch information
colinux committed Oct 30, 2023
1 parent 5f62af7 commit 79da969
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 3 deletions.
10 changes: 10 additions & 0 deletions app/components/release_note/note_form_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
en:
delete: Delete this note
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
fr:
delete: Supprimer cette note
Original file line number Diff line number Diff line change
@@ -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|
Expand Down Expand Up @@ -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

21 changes: 20 additions & 1 deletion app/controllers/super_admins/release_notes_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= turbo_stream.remove dom_id(@note, :fieldset_element) if @note.destroyed?
3 changes: 3 additions & 0 deletions config/locales/release_notes.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions config/locales/release_notes.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/system/super_admins/release_notes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 79da969

Please sign in to comment.