diff --git a/app/components/confirm_modal_component.rb b/app/components/confirm_modal_component.rb index 3a73af0e68..4fd17dc908 100644 --- a/app/components/confirm_modal_component.rb +++ b/app/components/confirm_modal_component.rb @@ -1,11 +1,12 @@ # frozen_string_literal: true class ConfirmModalComponent < ModalComponent - def initialize(id:, confirm_actions: nil, controllers: nil, message: nil, confirm_reflexes: nil) + def initialize(id:, confirm_actions: nil, reflex: nil, controller: nil, message: nil, confirm_reflexes: nil) super(id: id, close_button: true) @confirm_actions = confirm_actions + @reflex = reflex @confirm_reflexes = confirm_reflexes - @controllers = controllers + @controller = controller @message = message end diff --git a/app/components/confirm_modal_component/confirm_modal_component.html.haml b/app/components/confirm_modal_component/confirm_modal_component.html.haml index 0c507885ce..1b3e1ff217 100644 --- a/app/components/confirm_modal_component/confirm_modal_component.html.haml +++ b/app/components/confirm_modal_component/confirm_modal_component.html.haml @@ -1,4 +1,4 @@ -%div{ id: @id, "data-controller": "modal #{@controllers}", "data-action": "keyup@document->modal#closeIfEscapeKey" } +%div{ id: @id, "data-controller": "modal #{@controller}", "data-action": "keyup@document->modal#closeIfEscapeKey", "data-#{@controller}-reflex-value": @reflex } .reveal-modal-bg.fade{ "data-modal-target": "background", "data-action": "click->modal#close" } .reveal-modal.fade.tiny.help-modal{ "data-modal-target": "modal" } = content diff --git a/app/views/spree/admin/orders/_bulk_actions.html.haml b/app/views/spree/admin/orders/_bulk_actions.html.haml index cea049867c..ce3abc5ca7 100644 --- a/app/views/spree/admin/orders/_bulk_actions.html.haml +++ b/app/views/spree/admin/orders/_bulk_actions.html.haml @@ -13,7 +13,7 @@ %span.name{ "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "send_invoice" } = t('spree.admin.orders.index.send_invoice') %div.menu_item - %span.name{ "data-controller": "bulk-invoice", "data-action": "click->bulk-invoice#generate" } + %span.name{ "data-controller": "bulk-actions", "data-action": "click->bulk-actions#perform", "data-bulk-actions-reflex-value": "Admin::Orders#bulk_invoice" } = t('spree.admin.orders.index.print_invoices') %div.menu_item %span.name{ "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "cancel_orders" } diff --git a/app/views/spree/admin/orders/index.html.haml b/app/views/spree/admin/orders/index.html.haml index d46af52b9a..a7d64de200 100644 --- a/app/views/spree/admin/orders/index.html.haml +++ b/app/views/spree/admin/orders/index.html.haml @@ -20,14 +20,14 @@ = render 'spree/admin/shared/custom-confirm' -= render ConfirmModalComponent.new(id: "resend_confirmation", confirm_actions: "click->resend-confirmation-email#confirm", controllers: "resend-confirmation-email") do += render ConfirmModalComponent.new(id: "resend_confirmation", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "BulkActionsInOrdersList#resend_confirmation_email") do .margin-bottom-30 = t('.resend_confirmation_confirm_html') -= render ConfirmModalComponent.new(id: "send_invoice", confirm_actions: "click->send-invoice#confirm", controllers: "send-invoice") do += render ConfirmModalComponent.new(id: "send_invoice", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "BulkActionsInOrdersList#send_invoice") do .margin-bottom-30 = t('.send_invoice_confirm_html') -= render ConfirmModalComponent.new(id: "cancel_orders", confirm_actions: "click->cancel-orders#confirm", controllers: "cancel-orders", message: "spree/admin/orders/messages/cancel_orders") do += render ConfirmModalComponent.new(id: "cancel_orders", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "CancelOrders#confirm", message: "spree/admin/orders/messages/cancel_orders") do .margin-bottom-30 = t("js.admin.orders.cancel_the_order_html") diff --git a/app/views/spree/admin/orders/messages/_cancel_orders.html.haml b/app/views/spree/admin/orders/messages/_cancel_orders.html.haml index ca9093665d..9b689ff75e 100644 --- a/app/views/spree/admin/orders/messages/_cancel_orders.html.haml +++ b/app/views/spree/admin/orders/messages/_cancel_orders.html.haml @@ -1,5 +1,5 @@ .modal-message - %form{ "data-cancel-orders-target": "extraParams" } + %form{ "data-bulk-actions-target": "extraParams" } %input{ type: "checkbox", name: "send_cancellation_email", value: "1", id: "send_cancellation_email", checked: "true" } %label{ for: "send_cancellation_email" } = t("js.admin.orders.cancel_the_order_send_cancelation_email") diff --git a/app/webpacker/controllers/bulk_actions_controller.js b/app/webpacker/controllers/bulk_actions_controller.js index 791eddc820..9de40fe8cb 100644 --- a/app/webpacker/controllers/bulk_actions_controller.js +++ b/app/webpacker/controllers/bulk_actions_controller.js @@ -1,20 +1,35 @@ import ApplicationController from "./application_controller"; export default class extends ApplicationController { + static targets = ["extraParams"] + static values = { reflex: String } + connect() { super.connect(); } - // abstract - confirm(action) { - this.stimulate(action, this.getSelectedIds()); + perform() { + let params = { bulk_ids: this.getSelectedIds() }; + + if (this.hasExtraParamsTarget) { + Object.assign(params, this.extraFormData()) + } + + this.stimulate(this.reflexValue, params); } // private + getSelectedIds() { const checkboxes = document.querySelectorAll( "table input[name='bulk_ids[]']:checked" ); return Array.from(checkboxes).map((checkbox) => checkbox.value); } + + extraFormData() { + if (this.extraParamsTarget.constructor.name !== "HTMLFormElement") { return {} } + + return Object.fromEntries(new FormData(this.extraParamsTarget).entries()) + } } diff --git a/app/webpacker/controllers/bulk_invoice_controller.js b/app/webpacker/controllers/bulk_invoice_controller.js deleted file mode 100644 index 3b09724bcd..0000000000 --- a/app/webpacker/controllers/bulk_invoice_controller.js +++ /dev/null @@ -1,11 +0,0 @@ -import BulkActionsController from "./bulk_actions_controller"; - -export default class extends BulkActionsController { - connect() { - super.connect(); - } - - generate() { - this.stimulate("Admin::OrdersReflex#bulk_invoice", { bulk_ids: super.getSelectedIds() }); - } -} diff --git a/app/webpacker/controllers/cancel_orders_controller.js b/app/webpacker/controllers/cancel_orders_controller.js deleted file mode 100644 index b735cea31a..0000000000 --- a/app/webpacker/controllers/cancel_orders_controller.js +++ /dev/null @@ -1,27 +0,0 @@ -import BulkActionsController from "./bulk_actions_controller"; - -export default class extends BulkActionsController { - static targets = ["extraParams"] - - connect() { - super.connect(); - } - - confirm() { - let data = { bulk_ids: super.getSelectedIds() }; - - if (this.hasExtraParamsTarget) { - Object.assign(data, this.extraFormData()) - } - - this.stimulate("CancelOrdersReflex#confirm", data); - } - - // private - - extraFormData() { - if (this.extraParamsTarget.constructor.name !== "HTMLFormElement") { return {} } - - return Object.fromEntries(new FormData(this.extraParamsTarget).entries()) - } -} diff --git a/app/webpacker/controllers/resend_confirmation_email_controller.js b/app/webpacker/controllers/resend_confirmation_email_controller.js deleted file mode 100644 index ea48c61416..0000000000 --- a/app/webpacker/controllers/resend_confirmation_email_controller.js +++ /dev/null @@ -1,11 +0,0 @@ -import BulkActionsController from "./bulk_actions_controller"; - -export default class extends BulkActionsController { - connect() { - super.connect(); - } - - confirm() { - super.confirm("BulkActionsInOrdersList#resend_confirmation_email"); - } -} diff --git a/app/webpacker/controllers/send_invoice_controller.js b/app/webpacker/controllers/send_invoice_controller.js deleted file mode 100644 index 747735f0ee..0000000000 --- a/app/webpacker/controllers/send_invoice_controller.js +++ /dev/null @@ -1,11 +0,0 @@ -import BulkActionsController from "./bulk_actions_controller"; - -export default class extends BulkActionsController { - connect() { - super.connect(); - } - - confirm() { - super.confirm("BulkActionsInOrdersList#send_invoice"); - } -}