diff --git a/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee b/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee index 7aabe81f73..08a5050740 100644 --- a/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee @@ -4,11 +4,11 @@ angular.module("admin.standingOrders").controller "StandingOrdersController", ($ $scope.shop_id = if shops.length == 1 then shops[0].id else null $scope.shippingMethodsByID = ShippingMethods.byID $scope.paymentMethodsByID = PaymentMethods.byID + $scope.standingOrders = StandingOrders.all $scope.$watch "shop_id", -> if $scope.shop_id? - # CurrentShop.shop = $filter('filter')($scope.shops, {id: $scope.shop_id})[0] - $scope.standingOrders = StandingOrders.index("q[shop_id_eq]": $scope.shop_id) + StandingOrders.index("q[shop_id_eq]": $scope.shop_id, "q[canceled_at_null]": true) $scope.itemCount = (standingOrder) -> standingOrder.standing_line_items.reduce (sum, sli) -> diff --git a/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee b/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee index c099ed125d..f3e1cb3f4f 100644 --- a/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.standingOrders").factory 'StandingOrderPrototype', ($http, $injector, InfoDialog, StatusMessage) -> +angular.module("admin.standingOrders").factory 'StandingOrderPrototype', ($http, $injector, InfoDialog, ConfirmDialog, StatusMessage) -> errors: {} buildItem: (item) -> @@ -33,6 +33,13 @@ angular.module("admin.standingOrders").factory 'StandingOrderPrototype', ($http, StatusMessage.display 'failure', 'Oh no! I was unable to save your changes.' angular.extend(@errors, response.data.errors) + cancel: -> + ConfirmDialog.open('error', t('admin.standing_orders.confirm_cancel_msg'), {cancel: t('back'), confirm: t('yes_i_am_sure')}) + .then => + @$cancel().then (response) => + $injector.get('StandingOrders').afterCancel(@) if $injector.has('StandingOrders') + , -> + InfoDialog.open 'error', t('admin.standing_orders.cancel_failure_msg') cancelOrder: (order) -> if order.id? diff --git a/app/assets/javascripts/admin/standing_orders/services/standing_order_resource.js.coffee b/app/assets/javascripts/admin/standing_orders/services/standing_order_resource.js.coffee index b17df788d4..9b7a164fd9 100644 --- a/app/assets/javascripts/admin/standing_orders/services/standing_order_resource.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/services/standing_order_resource.js.coffee @@ -7,6 +7,11 @@ angular.module("admin.standingOrders").factory 'StandingOrderResource', ($resour method: 'PUT' params: id: '@id' + 'cancel': + method: 'PUT' + params: + id: '@id' + action: 'cancel' }) angular.extend(resource.prototype, StandingOrderPrototype) diff --git a/app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee b/app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee index 0fe055c7bb..355a6156f8 100644 --- a/app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/services/standing_orders.js.coffee @@ -1,5 +1,6 @@ angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOrderResource, StandingOrder) -> new class StandingOrders + all: [] byID: {} pristineByID: {} @@ -8,7 +9,9 @@ angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOr @load(data) load: (standingOrders) -> + @clear() for standingOrder in standingOrders + @all.push standingOrder @byID[standingOrder.id] = standingOrder @pristineByID[standingOrder.id] = angular.copy(standingOrder) @@ -20,7 +23,16 @@ angular.module("admin.standingOrders").factory 'StandingOrders', ($q, StandingOr return unless @byID[id]? @pristineByID[id] = angular.copy(@byID[id]) + afterCancel: (item) -> + i = @all.indexOf(item) + @all.splice(i,1) if i >= 0 + delete @byID[item.id] + delete @pristineByID[item.id] + afterRemoveItem: (id, deletedItemID) -> return unless @pristineByID[id]? for item, i in @pristineByID[id].standing_line_items when item.id == deletedItemID @pristineByID[id].standing_line_items.splice(i, 1) + + clear: -> + @all.length = 0 diff --git a/app/assets/javascripts/templates/admin/confirm_dialog.html.haml b/app/assets/javascripts/templates/admin/confirm_dialog.html.haml index 22094ae142..cfd6a011f8 100644 --- a/app/assets/javascripts/templates/admin/confirm_dialog.html.haml +++ b/app/assets/javascripts/templates/admin/confirm_dialog.html.haml @@ -4,5 +4,5 @@ %i.icon-question-sign .text{ ng: { bind: "::message" } } .action-buttons.text-center - %button{ ng: { click: "close()", bind: "::cancelText" } } - %button.red{ ng: { click: "confirm()", bind: "::confirmText" } } + %button.cancel{ ng: { click: "close()", bind: "::cancelText" } } + %button.confirm.red{ ng: { click: "confirm()", bind: "::confirmText" } } diff --git a/app/views/admin/standing_orders/_table.html.haml b/app/views/admin/standing_orders/_table.html.haml index 821f2ddea7..9d940f4ab4 100644 --- a/app/views/admin/standing_orders/_table.html.haml +++ b/app/views/admin/standing_orders/_table.html.haml @@ -4,13 +4,13 @@ %table.index#standing_orders %col.customer{ width: "20%", 'ng-show' => 'columns.customer.visible' } %col.schedule{ width: "15%", 'ng-show' => 'columns.schedule.visible' } - %col.items{ width: "15%", 'ng-show' => 'columns.items.visible' } - %col.orders{ width: "15%", 'ng-show' => 'columns.orders.visible' } + %col.items{ width: "10%", 'ng-show' => 'columns.items.visible' } + %col.orders{ width: "10%", 'ng-show' => 'columns.orders.visible' } %col.begins_on{ width: "10%", 'ng-show' => 'columns.begins_on.visible' } %col.ends_on{ width: "10%", 'ng-show' => 'columns.ends_on.visible' } %col.payment_method{ width: "20%", 'ng-show' => 'columns.payment_method.visible' } %col.shipping_method{ width: "20%", 'ng-show' => 'columns.shipping_method.visible' } - %col.actions{ width: "5%" } + %col.actions{ width: "10%" } %thead %tr -# %th.bulk @@ -47,6 +47,6 @@ %td.shipping_method{ ng: { show: 'columns.shipping_method.visible', bind: '::shippingMethodsByID[standingOrder.shipping_method_id].name' } } %td.actions %a.edit-standing-order.icon-edit.no-text{ ng: { href: '{{standingOrder.edit_path}}'} } - -# %a.delete-standing-order.icon-trash.no-text{ ng: { href: '{{standingOrder.delete_path}}'}, data: { method: 'delete', confirm: "Are you sure?" } } + %a.cancel-standing-order.icon-remove.no-text{ ng: { click: 'standingOrder.cancel()'} } %tr.panel-row{ object: "standingOrder", panels: "{products: 'standing_order_products', orders: 'standing_order_orders'}" } diff --git a/config/locales/en.yml b/config/locales/en.yml index d66d5176ef..ece02644b7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -129,10 +129,6 @@ en: say_yes: "Yes" then: then ongoing: Ongoing - closes: Closes - closed: Closed - could_not_cancel_the_order: Could not cancel the order - could_not_resume_the_order: Could not resume the order bill_address: Billing Address ship_address: Shipping Address sort_order_cycles_on_shopfront_by: "Sort Order Cycles On Shopfront By" @@ -816,6 +812,8 @@ en: orders: number: Number status: Status + confirm_cancel_msg: Are you sure you want to cancel this standing order? This action cannot be undone. + cancel_failure_msg: 'Sorry, cancellation failed!'
 stripe_connect_settings: edit: diff --git a/spec/features/admin/standing_orders_spec.rb b/spec/features/admin/standing_orders_spec.rb index c9b7080660..0f9654375d 100644 --- a/spec/features/admin/standing_orders_spec.rb +++ b/spec/features/admin/standing_orders_spec.rb @@ -97,6 +97,14 @@ feature 'Standing Orders' do expect(standing_order_order.reload.cancelled_at).to be nil end end + + # Cancelling a standing order + within "tr#so_#{standing_order.id}" do + find("a.cancel-standing-order").click + end + click_button "Yes, I'm sure" + expect(page).to have_no_selector "tr#so_#{standing_order.id}" + expect(standing_order.reload.canceled_at).to be_within(5.seconds).of Time.zone.now end end