mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Merge pull request #10297 from cyrillefr/convert-admin-order-cancellation-workflow-to-stimulus-reflex
Convert admin order cancellation workflow to stimulus reflex
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
angular.module("admin.orders").controller "bulkCancelCtrl", ($scope, $http, $timeout) ->
|
||||
|
||||
$scope.cancelOrder = (orderIds, sendEmailCancellation, restock_items) ->
|
||||
$http(
|
||||
method: 'post'
|
||||
url: "/admin/orders/bulk_cancel?order_ids=#{orderIds}&send_cancellation_email=#{sendEmailCancellation}&restock_items=#{restock_items}" ).then(->
|
||||
window.location.reload()
|
||||
)
|
||||
|
||||
$scope.cancelSelectedOrders = ->
|
||||
ofnCancelOrderAlert((confirm, sendEmailCancellation, restock_items) ->
|
||||
if confirm
|
||||
$scope.cancelOrder $scope.selected_orders, sendEmailCancellation, restock_items
|
||||
)
|
||||
@@ -1,10 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ConfirmModalComponent < ModalComponent
|
||||
def initialize(id:, confirm_actions: nil, controllers: nil)
|
||||
def initialize(id:, confirm_actions: nil, controllers: nil, message: nil)
|
||||
super(id: id, close_button: true)
|
||||
@confirm_actions = confirm_actions
|
||||
@controllers = controllers
|
||||
@message = message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
.reveal-modal.fade.tiny.help-modal{ "data-modal-target": "modal" }
|
||||
= content
|
||||
|
||||
= render @message if @message
|
||||
|
||||
.modal-actions
|
||||
%input{ class: "button icon-plus #{close_button_class}", type: 'button', value: t('js.admin.modals.cancel'), "data-action": "click->modal#close" }
|
||||
%input{ class: "button icon-plus primary", type: 'button', value: t('js.admin.modals.confirm'), "data-action": @confirm_actions }
|
||||
|
||||
@@ -65,18 +65,6 @@ module Spree
|
||||
load_spree_api_key
|
||||
end
|
||||
|
||||
def bulk_cancel
|
||||
order_ids = params[:order_ids].split(',')
|
||||
|
||||
Spree::Order.where(id: order_ids).find_each do |order|
|
||||
order.send_cancellation_email = params[:send_cancellation_email] != "false"
|
||||
order.restock_items = params.fetch(:restock_items, "true") == "true"
|
||||
order.cancel
|
||||
end
|
||||
|
||||
flash[:success] = Spree.t(:order_updated)
|
||||
end
|
||||
|
||||
def fire
|
||||
event = params[:e]
|
||||
@order.send_cancellation_email = params[:send_cancellation_email] != "false"
|
||||
|
||||
9
app/reflexes/cancel_orders_reflex.rb
Normal file
9
app/reflexes/cancel_orders_reflex.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CancelOrdersReflex < ApplicationReflex
|
||||
def confirm(params)
|
||||
OrdersBulkCancelService.new(params).call
|
||||
cable_ready.dispatch_event(name: "modal:close")
|
||||
# flash[:success] = Spree.t(:order_updated)
|
||||
end
|
||||
end
|
||||
17
app/services/orders_bulk_cancel_service.rb
Normal file
17
app/services/orders_bulk_cancel_service.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class OrdersBulkCancelService
|
||||
def initialize(params)
|
||||
@order_ids = params[:order_ids]
|
||||
@send_cancellation_email = params[:send_cancellation_email]
|
||||
@restock_items = params[:restock_items]
|
||||
end
|
||||
|
||||
def call
|
||||
Spree::Order.where(id: @order_ids).find_each do |order|
|
||||
order.send_cancellation_email = @send_cancellation_email
|
||||
order.restock_items = @restock_items
|
||||
order.cancel
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -37,7 +37,7 @@
|
||||
%span.name.invoices-modal{'ng-controller' => 'bulkInvoiceCtrl', 'ng-click' => 'createBulkInvoice()' }
|
||||
= t('.print_invoices')
|
||||
%div.menu_item
|
||||
%span.name{'ng-controller' => 'bulkCancelCtrl', 'ng-click' => 'cancelSelectedOrders()' }
|
||||
%span.name{ "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "cancel_orders" }
|
||||
= t('.cancel_orders')
|
||||
|
||||
= render partial: 'admin/shared/angular_per_page_controls', locals: { position: "right", model: "orders" }
|
||||
@@ -124,3 +124,6 @@
|
||||
= render ConfirmModalComponent.new(id: "resend_confirmation", confirm_actions: "click->resend-confirmation-email#confirm", controllers: "resend-confirmation-email") do
|
||||
.margin-bottom-30
|
||||
= t('.resend_confirmation_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
|
||||
.margin-bottom-30
|
||||
= t("js.admin.orders.cancel_the_order_html")
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
.modal-message
|
||||
.form
|
||||
%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")
|
||||
%br
|
||||
%input{ type: "checkbox", name: "restock_items", id: "restock_items", checked: "true" }
|
||||
%label{ for: "restock_items" }
|
||||
= t("js.admin.orders.restock_items")
|
||||
.margin-bottom-30
|
||||
|
||||
30
app/webpacker/controllers/cancel_orders_controller.js
Normal file
30
app/webpacker/controllers/cancel_orders_controller.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import ApplicationController from "./application_controller";
|
||||
|
||||
export default class extends ApplicationController {
|
||||
connect() {
|
||||
super.connect();
|
||||
}
|
||||
|
||||
confirm() {
|
||||
const send_cancellation_email = document.querySelector(
|
||||
"#send_cancellation_email"
|
||||
).checked;
|
||||
const restock_items = document.querySelector("#restock_items").checked;
|
||||
const order_ids = [];
|
||||
|
||||
document
|
||||
.querySelectorAll("#listing_orders input[name='order_ids[]']:checked")
|
||||
.forEach((checkbox) => {
|
||||
order_ids.push(checkbox.value);
|
||||
});
|
||||
|
||||
const params = {
|
||||
order_ids: order_ids,
|
||||
send_cancellation_email: send_cancellation_email,
|
||||
restock_items: restock_items,
|
||||
};
|
||||
this.stimulate("CancelOrdersReflex#confirm", params).then(() =>
|
||||
window.location.reload()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,6 @@ Spree::Core::Engine.routes.draw do
|
||||
end
|
||||
|
||||
resource :account, :controller => 'users'
|
||||
match '/admin/orders/bulk_cancel' => 'admin/orders#bulk_cancel', :as => "admin_bulk_cancel", via: :post
|
||||
|
||||
match '/admin/orders/bulk_management' => 'admin/orders#bulk_management', :as => "admin_bulk_order_management", via: :get
|
||||
match '/admin/payment_methods/show_provider_preferences' => 'admin/payment_methods#show_provider_preferences', :via => :get
|
||||
|
||||
@@ -29,15 +29,20 @@ describe '
|
||||
expect(page).to have_selector('span', text: 'COMPLETE', count: 2)
|
||||
|
||||
page.check('selectAll')
|
||||
page.find('.ofn-drop-down').click
|
||||
page.find('.menu').find('span', text: 'Cancel Orders').click
|
||||
page.find("span.icon-reorder", text: "ACTIONS").click
|
||||
within ".ofn-drop-down-with-prepend .menu" do
|
||||
page.find("span", text: "Cancel Orders").click
|
||||
end
|
||||
|
||||
within '.modal' do
|
||||
click_on "OK"
|
||||
within '.reveal-modal' do
|
||||
expect {
|
||||
find_button("Confirm").click
|
||||
}.to change { o1.reload.state }.from('complete').to('canceled')
|
||||
.and change { o2.reload.state }.from('complete').to('canceled')
|
||||
end
|
||||
|
||||
# Verify that the orders have a STATE of CANCELLED
|
||||
expect(page).to have_selector('span', text: 'CANCELLED', count: 2)
|
||||
expect(page).to have_selector('span.canceled', text: 'CANCELLED', count: 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -432,7 +432,7 @@ describe '
|
||||
expect(page).to have_content "Are you sure you want to proceed?"
|
||||
expect(page).to have_content "This will cancel the current order."
|
||||
|
||||
within "#custom-confirm.modal" do
|
||||
within ".reveal-modal" do
|
||||
expect {
|
||||
find_button("Cancel").click # Cancels the cancel action
|
||||
}.to_not enqueue_job(ActionMailer::MailDeliveryJob).exactly(:twice)
|
||||
@@ -443,9 +443,9 @@ describe '
|
||||
page.find("span", text: "Cancel Orders").click
|
||||
end
|
||||
|
||||
within "#custom-confirm.modal" do
|
||||
within ".reveal-modal" do
|
||||
expect {
|
||||
find_button("OK").click # Confirms the cancel action
|
||||
find_button("Confirm").click # Confirms the cancel action
|
||||
}.to_not enqueue_job(ActionMailer::MailDeliveryJob).exactly(:twice)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user