From 11ad9acd26b8daa18e573089ed6fd58ce2abda1a Mon Sep 17 00:00:00 2001 From: noracato Date: Tue, 29 Oct 2024 16:34:42 +0100 Subject: [PATCH] Permanently delete scenarios --- .../nav_item/component.html.erb | 2 +- .../saved_scenarios/nav_item/component.rb | 1 + app/controllers/saved_scenarios_controller.rb | 23 +++++++----------- app/views/discarded/index.html.haml | 3 +++ .../confirm_destroy.html.erb | 3 --- app/views/featured_scenarios/edit.html.haml | 1 - .../_block_right_menu.html.erb | 14 +++++++++++ .../saved_scenarios/confirm_destroy.html.erb | 24 +++++++++++++++++++ config/locales/en.yml | 11 +++++++++ config/routes.rb | 1 + spec/requests/saved_scenarios_spec.rb | 4 ++-- 11 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 app/views/saved_scenarios/confirm_destroy.html.erb diff --git a/app/components/saved_scenarios/nav_item/component.html.erb b/app/components/saved_scenarios/nav_item/component.html.erb index 4972f28..39b1ddb 100644 --- a/app/components/saved_scenarios/nav_item/component.html.erb +++ b/app/components/saved_scenarios/nav_item/component.html.erb @@ -1,4 +1,4 @@ -<%= link_to @path, class: "flex p-2 pl-6 w-full transition text-sm #{css_classes}" do %> +<%= link_to @path, class: "flex p-2 pl-6 w-full transition text-sm #{css_classes}", data: @data do %> <%= heroicon @icon, options: { class: 'w-5 h-5' } %> <%= @title %> <% end %> diff --git a/app/components/saved_scenarios/nav_item/component.rb b/app/components/saved_scenarios/nav_item/component.rb index f48b576..fe8bb8c 100644 --- a/app/components/saved_scenarios/nav_item/component.rb +++ b/app/components/saved_scenarios/nav_item/component.rb @@ -5,6 +5,7 @@ class Component < ApplicationComponent option :icon option :active, default: proc { false } option :static, default: proc { false } + option :data, default: proc { {} } def css_classes if @active diff --git a/app/controllers/saved_scenarios_controller.rb b/app/controllers/saved_scenarios_controller.rb index e52e6e9..9e5333b 100644 --- a/app/controllers/saved_scenarios_controller.rb +++ b/app/controllers/saved_scenarios_controller.rb @@ -1,5 +1,5 @@ class SavedScenariosController < ApplicationController - load_resource only: %i[discard undiscard publish unpublish] + load_resource only: %i[discard undiscard publish unpublish confirm_destroy] load_and_authorize_resource only: %i[show new create edit update destroy] before_action :require_user, only: %i[index] @@ -8,7 +8,7 @@ class SavedScenariosController < ApplicationController authorize!(:update, @saved_scenario) end - before_action only: %i[discard undiscard] do + before_action only: %i[discard undiscard confirm_destroy] do authorize!(:destroy, @saved_scenario) end @@ -64,20 +64,15 @@ def update end end + def confirm_destroy + render :confirm_destroy, layout: 'application' + end + # DELETE /saved_scenarios/1 or /saved_scenarios/1.json def destroy - @saved_scenario.destroy! - - respond_to do |format| - format.html do - redirect_to( - saved_scenarios_path, - status: :see_other, - notice: "Scenario was successfully destroyed." - ) - end - format.json { head :no_content } - end + @saved_scenario.destroy + flash.notice = t('scenario.trash.deleted_flash') + redirect_to discarded_path end # Makes a scenario public. diff --git a/app/views/discarded/index.html.haml b/app/views/discarded/index.html.haml index 9dcf101..0709606 100644 --- a/app/views/discarded/index.html.haml +++ b/app/views/discarded/index.html.haml @@ -2,6 +2,9 @@ - content_for :menu_title, t('trash.title') - content_for :block_right, "FILTERS" +- if notice_message + = render(NoticeBanner::Component.new(text: notice_message)) + - if @resources.present? - @resources.each do |resource| - if resource.is_a?(SavedScenario) diff --git a/app/views/featured_scenarios/confirm_destroy.html.erb b/app/views/featured_scenarios/confirm_destroy.html.erb index 37195c6..a89fc65 100644 --- a/app/views/featured_scenarios/confirm_destroy.html.erb +++ b/app/views/featured_scenarios/confirm_destroy.html.erb @@ -1,6 +1,3 @@ -<%# We have to do the hovercard tutorial! With using turbo to insert the confirm into a modal /popup box -also check other tutorials for confirm destroy! %> - <%= render(ModalComponent.new(title: t('.title'))) do |modal| %> <%= turbo_frame_tag :modal do %> <%= form_for(@featured_scenario, url: saved_scenario_feature_path(@featured_scenario.saved_scenario), html: { method: :delete, data: { turbo: false } }) do |f| %> diff --git a/app/views/featured_scenarios/edit.html.haml b/app/views/featured_scenarios/edit.html.haml index bc02654..b8ff739 100644 --- a/app/views/featured_scenarios/edit.html.haml +++ b/app/views/featured_scenarios/edit.html.haml @@ -24,5 +24,4 @@ = submit_tag t('scenario.save'), class: 'button w-1/4 bg-midnight-900 text-midnight-200' = link_to t('scenario.discard_changes'), saved_scenario_path(@featured_scenario.saved_scenario), class: 'button w-1/4 ml-auto mr-0' - if @featured_scenario.persisted? - -# = button_to 'Destroy', saved_scenario_feature_path(@featured_scenario.saved_scenario), method: :delete, form: { data: { turbo_confirm: 'Are you sure?' } } = link_to t('featured_scenario.unfeature'), confirm_destroy_saved_scenario_feature_path(@featured_scenario.saved_scenario), data: { turbo_frame: 'modal' } , class: 'button w-1/4 ml-auto mr-0' diff --git a/app/views/saved_scenarios/_block_right_menu.html.erb b/app/views/saved_scenarios/_block_right_menu.html.erb index ff0db56..5d75db1 100644 --- a/app/views/saved_scenarios/_block_right_menu.html.erb +++ b/app/views/saved_scenarios/_block_right_menu.html.erb @@ -80,4 +80,18 @@ ))%> <% end %> <% end %> + <% if saved_scenario.collaborator?(current_user) && saved_scenario.discarded? %> + <%= render(Hovercard::Component.new( + path: '', + text: t("scenario_bar.destroy.description") + )) do %> + <%= render(SavedScenarios::NavItem::Component.new( + path: confirm_destroy_saved_scenario_path(saved_scenario), + title: t("scenario_bar.destroy.title"), + icon: 'x-mark', + static: true, + data: { turbo_frame: 'modal' } + ))%> + <% end %> + <% end %> <% end %> diff --git a/app/views/saved_scenarios/confirm_destroy.html.erb b/app/views/saved_scenarios/confirm_destroy.html.erb new file mode 100644 index 0000000..8c2d083 --- /dev/null +++ b/app/views/saved_scenarios/confirm_destroy.html.erb @@ -0,0 +1,24 @@ +<%= render(ModalComponent.new(title: t('.title'))) do |modal| %> + <%= turbo_frame_tag :modal do %> + <%= form_for(@saved_scenario, url: saved_scenario_path(@saved_scenario), html: { method: :delete, data: { turbo: false } }) do |f| %> +
+
+ <%= heroicon 'exclamation-triangle', options: { class: 'w-6 h-6' } %> + + <%= t('.warning_header') %> + +
+

+ <%= t('.warning') %> +

+

+ <%= t('.irreversible') %> +

+
+
+ <%= button_tag t('.submit'), class: button_classes("text-base", size: :lg, color: :warning) %> + <%= modal.close_link(t('identity.cancel'), saved_scenario_path(@saved_scenario), class: button_classes("text-base", size: :lg)) %> +
+ <% end %> + <% end %> +<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 5870b54..4993df7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -95,6 +95,9 @@ en: description: true: This scenario has been featured on a home page. Edit settings here. false: Feature this scenario on the home page + destroy: + title: Delete permanently + description: Permanently delete this scenario. scenario: succesful_update: Your scenario was succesfully updated title: Title @@ -105,6 +108,14 @@ en: saved_scenarios: empty: You don't have any scenarios. + confirm_destroy: + title: Permanently deleting scenario + warning_header: You are permanently deleting your scenario + warning: | + Deleting this scenario will remove all data including history, + any grants for access, title and description. + irreversible: This action is irreversible + submit: Delete this scenario featured_scenarios: confirm_destroy: title: Unfeaturing scenario diff --git a/config/routes.rb b/config/routes.rb index e5fca13..e383685 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,7 @@ put :unpublish put :discard put :undiscard + get :confirm_destroy end end diff --git a/spec/requests/saved_scenarios_spec.rb b/spec/requests/saved_scenarios_spec.rb index ae1c409..223a38a 100644 --- a/spec/requests/saved_scenarios_spec.rb +++ b/spec/requests/saved_scenarios_spec.rb @@ -151,9 +151,9 @@ }.to change(SavedScenario, :count).by(-1) end - it "redirects to the saved_scenarios list" do + it "redirects to the trash can" do delete saved_scenario_url(user_scenario) - expect(response).to redirect_to(saved_scenarios_url) + expect(response).to redirect_to(discarded_url) end end