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 cdd05c2c63..3324b92ae4 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 @@ -1,3 +1,9 @@ -angular.module("admin.standingOrders").controller "StandingOrdersController", ($scope, StandingOrders, Columns) -> - $scope.standingOrders = StandingOrders.index({ams_prefix: 'index'}) # {enterprise_id: $scope.shop_id} +angular.module("admin.standingOrders").controller "StandingOrdersController", ($scope, StandingOrders, Columns, shops) -> $scope.columns = Columns.columns + $scope.shops = shops + $scope.shop_id = if shops.length == 1 then shops[0].id else null + + $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, ams_prefix: 'index') diff --git a/app/views/admin/standing_orders/_data.html.haml b/app/views/admin/standing_orders/_data.html.haml index c955b94b0c..ebd0d75cbf 100644 --- a/app/views/admin/standing_orders/_data.html.haml +++ b/app/views/admin/standing_orders/_data.html.haml @@ -1,5 +1,6 @@ -= admin_inject_json_ams "admin.standingOrders", "standingOrder", @standing_order, Api::Admin::StandingOrderSerializer -= admin_inject_json_ams_array "admin.standingOrders", "customers", @customers, Api::Admin::CustomerSerializer -= admin_inject_json_ams_array "admin.standingOrders", "schedules", @schedules, Api::Admin::IdNameSerializer -= admin_inject_json_ams_array "admin.standingOrders", "paymentMethods", @payment_methods, Api::Admin::IdNameSerializer -= admin_inject_json_ams_array "admin.standingOrders", "shippingMethods", @shipping_methods, Api::Admin::IdNameSerializer += admin_inject_json_ams "admin.standingOrders", "standingOrder", @standing_order, Api::Admin::StandingOrderSerializer if @standing_order += admin_inject_json_ams_array "admin.standingOrders", "shops", @shops, Api::Admin::IdNameSerializer if @shops += admin_inject_json_ams_array "admin.standingOrders", "customers", @customers, Api::Admin::IdEmailSerializer if @customers += admin_inject_json_ams_array "admin.standingOrders", "schedules", @schedules, Api::Admin::IdNameSerializer if @schedules += admin_inject_json_ams_array "admin.standingOrders", "paymentMethods", @payment_methods, Api::Admin::IdNameSerializer if @payment_methods += admin_inject_json_ams_array "admin.standingOrders", "shippingMethods", @shipping_methods, Api::Admin::IdNameSerializer if @shipping_methods diff --git a/app/views/admin/standing_orders/_filters.html.haml b/app/views/admin/standing_orders/_filters.html.haml new file mode 100644 index 0000000000..50501b0e6e --- /dev/null +++ b/app/views/admin/standing_orders/_filters.html.haml @@ -0,0 +1,12 @@ +.row.filters + .sixteen.columns.alpha.omega + .filter_select.five.columns.alpha +   + -# %label{ :for => 'quick_search', ng: {class: '{disabled: !shop_id}'} }=t('admin.quick_search') + -# %br + -# %input.fullwidth{ :type => "text", :id => 'quick_search', ng: { model: 'quickSearch', disabled: '!shop_id' }, :placeholder => "Search by email..." } + .filter_select.four.columns + %label{ :for => 'shop_id', ng: { bind: "shop_id ? '#{t('admin.shop')}' : '#{t('admin.variant_overrides.index.select_a_shop')}'" } } + %br + %input.ofn-select2.fullwidth#shop_id{ 'ng-model' => 'shop_id', name: 'shop_id', data: 'shops' } + .seven.columns.omega   diff --git a/app/views/admin/standing_orders/index.html.haml b/app/views/admin/standing_orders/index.html.haml index 638760ac26..a898a5463c 100644 --- a/app/views/admin/standing_orders/index.html.haml +++ b/app/views/admin/standing_orders/index.html.haml @@ -5,6 +5,8 @@ = "ng-app='admin.standingOrders'" = admin_inject_column_preferences module: 'admin.standingOrders' += render 'data' %div{ ng: { controller: 'StandingOrdersController' } } + = render 'filters' = render 'table' diff --git a/spec/features/admin/standing_orders_spec.rb b/spec/features/admin/standing_orders_spec.rb index d4e1a2f444..599a00d538 100644 --- a/spec/features/admin/standing_orders_spec.rb +++ b/spec/features/admin/standing_orders_spec.rb @@ -7,16 +7,30 @@ feature 'Standing Orders' do context "as an enterprise user", js: true do let!(:user) { create_enterprise_user(enterprise_limit: 10) } let!(:shop) { create(:distributor_enterprise, owner: user) } + let!(:shop2) { create(:distributor_enterprise, owner: user) } + let!(:shop_unmanaged) { create(:distributor_enterprise) } before { quick_login_as user } context 'listing standing orders' do let!(:standing_order) { create(:standing_order, shop: shop) } + let!(:standing_order2) { create(:standing_order, shop: shop2) } + let!(:standing_order_unmanaged) { create(:standing_order, shop: shop_unmanaged) } it "passes the smoke test" do visit admin_standing_orders_path - expect(page).to have_selector 'table#standing_orders td.customer', text: standing_order.customer.email + expect(page).to have_select2 "shop_id", with_options: [shop.name, shop2.name], without_options: [shop_unmanaged.name] + + select2_select shop2.name, from: "shop_id" + + # Loads standing orders for that shop + expect(page).to have_selector "tr#so_#{standing_order2.id}" + expect(page).to have_no_selector "tr#so_#{standing_order.id}" + expect(page).to have_no_selector "tr#so_#{standing_order_unmanaged.id}" + within "tr#so_#{standing_order2.id}" do + expect(page).to have_selector "td.customer", text: standing_order2.customer.email + end end end diff --git a/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee b/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee index 6dbfdf67c7..1b2567c9c4 100644 --- a/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee @@ -1,19 +1,18 @@ describe "StandingOrdersCtrl", -> scope = null http = null - shops = null + shops = [ + { name: "Shop 1", id: 1 } + { name: "Shop 2", id: 2 } + { name: "Shop 3", id: 3 } + ] beforeEach -> module('admin.standingOrders') - # module ($provide) -> - # $provide.value 'columns', [] - # null - - # shops = [ - # { name: "Shop 1", id: 1 } - # { name: "Shop 2", id: 2 } - # { name: "Shop 3", id: 3 } - # ] + module ($provide) -> + # $provide.value 'columns', [] + $provide.value 'shops', shops + null inject ($controller, $rootScope, _StandingOrderResource_, $httpBackend) -> scope = $rootScope @@ -24,28 +23,20 @@ describe "StandingOrdersCtrl", -> compare: (actual, expected) -> { pass: angular.equals(actual, expected) } - # it "has no shop pre-selected", inject (CurrentShop) -> - # expect(CurrentShop.shop).toEqual {} + it "has no shop pre-selected", -> + expect(scope.shop_id).toEqual null - describe "initialization", -> # setting shop_id on scope + describe "setting shop_id on scope", -> standingOrder = { id: 5, customer_id: 3, schedule_id: 1} standingOrders = [standingOrder] - beforeEach inject -> - scope.standing_orders_form = jasmine.createSpyObj('standing_orders_form', ['$setPristine']) - http.expectGET('/admin/standing_orders.json?ams_prefix=index').respond 200, standingOrders - # scope.$apply -> - # scope.shop_id = 3 + beforeEach -> + http.expectGET('/admin/standing_orders.json?ams_prefix=index&q%5Bshop_id_eq%5D=3').respond 200, standingOrders + scope.$apply -> scope.shop_id = 3 http.flush() # it "sets the CurrentShop", inject (CurrentShop) -> # expect(CurrentShop.shop).toEqual shops[2] - # - # it "sets the form state to pristine", -> - # expect(scope.standingOrders_form.$setPristine).toHaveBeenCalled() - # - # it "clears all changes", inject (pendingChanges) -> - # expect(pendingChanges.removeAll).toHaveBeenCalled() it "retrieves the list of standingOrders", -> expect(scope.standingOrders).toDeepEqual standingOrders