Render confirm remove image modal outside of form so a simple :button_to can be used instead of submitting via Stimulus controller.

This commit is contained in:
Cillian O'Ruanaidh
2025-05-02 12:59:30 +01:00
parent ace5d5eb08
commit 65efd3b66c
5 changed files with 28 additions and 37 deletions

View File

@@ -70,7 +70,6 @@ module Admin
if @object.update(enterprise_params)
flash[:success] = flash_success_message
set_panel_if_attachment_removal
respond_with(@object) do |format|
format.html { redirect_to location_after_save }
@@ -175,16 +174,6 @@ module Admin
end
end
def set_panel_if_attachment_removal
return if !attachment_removal?
@panel = if attachment_removal_parameter == "remove_white_label_logo"
"white_label"
elsif ["remove_logo", "remove_promo_image"].include?(attachment_removal_parameter)
"images"
end
end
def attachment_removal?
attachment_removal_parameter.present?
end
@@ -194,6 +183,7 @@ module Admin
enterprise_params.keys.first
end
end
helper_method :attachment_removal_parameter
def load_enterprise_set_on_index
return unless spree_current_user.admin?

View File

@@ -44,6 +44,22 @@ module Admin
ConnectedApp::TYPES & connected_apps_enabled
end
def enterprise_attachment_removal_modal_id
attachment_removal_parameter # remove_logo|remove_promo_image|remove_white_label_logo
end
def enterprise_attachment_removal_panel
if attachment_removal_parameter == "remove_white_label_logo"
"white_label"
elsif ["remove_logo", "remove_promo_image"].include?(attachment_removal_parameter)
"images"
end
end
def enterprise_attachment_removal_panel_id
"#{enterprise_attachment_removal_panel}_panel"
end
private
def build_enterprise_side_menu_items(

View File

@@ -1,4 +1,5 @@
= turbo_stream.remove enterprise_attachment_removal_modal_id
= turbo_stream.update "flashes" do
= render partial: "admin/shared/flashes", locals: { flashes: flash } if defined? flash
= turbo_stream.update "#{@panel}_panel" do
= render partial: "admin/enterprises/form/#{@panel}", locals: { f: ActionView::Helpers::FormBuilder.new(:enterprise, @enterprise, self, {}), enterprise: @enterprise }
= turbo_stream.update enterprise_attachment_removal_panel_id do
= render partial: "admin/enterprises/form/#{enterprise_attachment_removal_panel}", locals: { f: ActionView::Helpers::FormBuilder.new(:enterprise, @enterprise, self, {}), enterprise: @enterprise }

View File

@@ -15,6 +15,11 @@
= t(".#{attachment_name}_remove")
- if object.send(attachment_name).present?
= render ConfirmModalComponent.new(id: "remove_#{attachment_name}", confirm_actions: "click->attachment-field#remove", controller: "attachment-field") do
.margin-bottom-30
= t(".#{attachment_name}_remove_confirm")
- # add to admin footer to avoid nesting forms
- content_for :admin_footer do
= render ModalComponent.new(id: "remove_#{attachment_name}", close_button: false, modal_class: "tiny") do
.margin-bottom-30
= t(".#{attachment_name}_remove_confirm")
%div{ class: "modal-actions justify-space-around" }
%input{ class: "button icon-plus secondary", type: 'button', value: I18n.t('js.admin.modals.cancel'), "data-action": "click->modal#close" }
= button_to I18n.t('js.admin.modals.confirm'), admin_enterprise_path(object), method: :patch, params: { enterprise: { "remove_#{attachment_name}": 1 } }, form: { "data-turbo": true }

View File

@@ -1,21 +0,0 @@
import { Controller } from "stimulus";
export default class extends Controller {
async remove() {
const attachmentRemovalParameterKey = this.element.id; // e.g. 'remove_logo'
const action = this.element.closest("form").action;
const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');
const formData = new FormData();
formData.append(`enterprise[${attachmentRemovalParameterKey}]`, "1")
const response = await fetch(action, {
method: 'PATCH',
headers: {
Accept: 'text/vnd.turbo-stream.html',
'X-CSRF-Token': csrfToken,
},
body: formData
});
const responseTurboStream = await response.text();
Turbo.renderStreamMessage(responseTurboStream);
}
}