Combine related reflex actions

This commit is contained in:
Matt-Yorkley
2023-05-09 23:42:44 +01:00
parent bee46720f4
commit 95f1643cec
4 changed files with 51 additions and 57 deletions

View File

@@ -40,10 +40,58 @@ class Admin::OrdersReflex < ApplicationReflex
morph :nothing
end
def cancel_orders(params)
cancelled_orders = OrdersBulkCancelService.new(params, current_user).call
cable_ready.dispatch_event(name: "modal:close")
cancelled_orders.each do |order|
cable_ready.replace(
selector: dom_id(order),
html: render(partial: "spree/admin/orders/table_row", locals: { order: order })
)
end
cable_ready.broadcast
morph :nothing
end
def resend_confirmation_emails(params)
editable_orders.where(id: params[:bulk_ids]).find_each do |order|
next unless can? :resend, order
Spree::OrderMailer.confirm_email_for_customer(order.id, true).deliver_later
end
success("admin.resend_confirmation_emails_feedback", params[:bulk_ids].count)
end
def send_invoices(params)
count = 0
editable_orders.where(id: params[:bulk_ids]).find_each do |o|
next unless o.distributor.can_invoice? && (o.resumed? || o.complete?)
Spree::OrderMailer.invoice_email(o.id).deliver_later
count += 1
end
success("admin.send_invoice_feedback", count)
end
private
def authorize_order
@order = Spree::Order.find_by(id: element.dataset[:id])
authorize! :admin, @order
end
def success(i18n_key, count)
flash[:success] = with_locale { I18n.t(i18n_key, count: count) }
cable_ready.dispatch_event(name: "modal:close")
morph_admin_flashes
end
def editable_orders
Permissions::Order.new(current_user).editable_orders
end
end

View File

@@ -1,35 +0,0 @@
# frozen_string_literal: true
class BulkActionsInOrdersListReflex < ApplicationReflex
def resend_confirmation_email(order_ids)
editable_orders.where(id: order_ids).find_each do |o|
Spree::OrderMailer.confirm_email_for_customer(o.id, true).deliver_later if can? :resend, o
end
success("admin.resend_confirmation_emails_feedback", order_ids.count)
end
def send_invoice(order_ids)
count = 0
editable_orders.where(id: order_ids).find_each do |o|
next unless o.distributor.can_invoice? && (o.resumed? || o.complete?)
Spree::OrderMailer.invoice_email(o.id).deliver_later
count += 1
end
success("admin.send_invoice_feedback", count)
end
private
def success(i18n_key, count)
flash[:success] = I18n.t(i18n_key, count: count)
cable_ready.dispatch_event(name: "modal:close")
morph "#flashes", render(partial: "shared/flashes", locals: { flashes: flash })
end
def editable_orders
Permissions::Order.new(current_user).editable_orders
end
end

View File

@@ -1,19 +0,0 @@
# frozen_string_literal: true
class CancelOrdersReflex < ApplicationReflex
def confirm(params)
cancelled_orders = OrdersBulkCancelService.new(params, current_user).call
cable_ready.dispatch_event(name: "modal:close")
cancelled_orders.each do |order|
cable_ready.replace(
selector: dom_id(order),
html: render(partial: "spree/admin/orders/table_row", locals: { order: order })
)
end
cable_ready.broadcast
morph :nothing
end
end

View File

@@ -20,14 +20,14 @@
= render 'spree/admin/shared/custom-confirm'
= render ConfirmModalComponent.new(id: "resend_confirmation", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "BulkActionsInOrdersList#resend_confirmation_email") do
= render ConfirmModalComponent.new(id: "resend_confirmation", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "Admin::Orders#resend_confirmation_emails") do
.margin-bottom-30
= t('.resend_confirmation_confirm_html')
= render ConfirmModalComponent.new(id: "send_invoice", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "BulkActionsInOrdersList#send_invoice") do
= render ConfirmModalComponent.new(id: "send_invoice", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "Admin::Orders#send_invoices") do
.margin-bottom-30
= t('.send_invoice_confirm_html')
= 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
= render ConfirmModalComponent.new(id: "cancel_orders", confirm_actions: "click->bulk-actions#perform", controller: "bulk-actions", reflex: "Admin::Orders#cancel_orders", message: "spree/admin/orders/messages/cancel_orders") do
.margin-bottom-30
= t("js.admin.orders.cancel_the_order_html")