mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-15 23:57:48 +00:00
Automatically selecting a hub on inventory page when it is the only option
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
angular.module("admin.variantOverrides").controller "AdminVariantOverridesCtrl", ($scope, $http, $timeout, Indexer, Columns, Views, SpreeApiAuth, PagedFetcher, StatusMessage, RequestMonitor, hubs, producers, hubPermissions, InventoryItems, VariantOverrides, DirtyVariantOverrides) ->
|
||||
$scope.hubs = Indexer.index hubs
|
||||
$scope.hub = null
|
||||
$scope.hub_id = if hubs.length == 1 then hubs[0].id else null
|
||||
$scope.products = []
|
||||
$scope.producers = producers
|
||||
$scope.producersByID = Indexer.index producers
|
||||
@@ -59,10 +59,6 @@ angular.module("admin.variantOverrides").controller "AdminVariantOverridesCtrl",
|
||||
$scope.products = $scope.products.concat products
|
||||
VariantOverrides.ensureDataFor hubs, products
|
||||
|
||||
|
||||
$scope.selectHub = ->
|
||||
$scope.hub = $scope.hubs[$scope.hub_id]
|
||||
|
||||
$scope.displayDirty = ->
|
||||
if DirtyVariantOverrides.count() > 0
|
||||
num = if DirtyVariantOverrides.count() == 1 then "one override" else "#{DirtyVariantOverrides.count()} overrides"
|
||||
|
||||
@@ -2,11 +2,11 @@ angular.module("admin.variantOverrides").directive "trackInheritance", (VariantO
|
||||
require: "ngModel"
|
||||
link: (scope, element, attrs, ngModel) ->
|
||||
# This is a bit hacky, but it allows us to load the inherit property on the VO, but then not submit it
|
||||
scope.inherit = angular.equals scope.variantOverrides[scope.hub.id][scope.variant.id], VariantOverrides.newFor scope.hub.id, scope.variant.id
|
||||
scope.inherit = angular.equals scope.variantOverrides[scope.hub_id][scope.variant.id], VariantOverrides.newFor scope.hub_id, scope.variant.id
|
||||
|
||||
ngModel.$parsers.push (viewValue) ->
|
||||
if ngModel.$dirty && viewValue
|
||||
variantOverride = VariantOverrides.inherit(scope.hub.id, scope.variant.id)
|
||||
variantOverride = VariantOverrides.inherit(scope.hub_id, scope.variant.id)
|
||||
DirtyVariantOverrides.add variantOverride
|
||||
scope.displayDirty()
|
||||
viewValue
|
||||
|
||||
@@ -3,7 +3,7 @@ angular.module("admin.variantOverrides").directive "ofnTrackVariantOverride", (D
|
||||
link: (scope, element, attrs, ngModel) ->
|
||||
ngModel.$parsers.push (viewValue) ->
|
||||
if ngModel.$dirty
|
||||
variantOverride = scope.variantOverrides[scope.hub.id][scope.variant.id]
|
||||
variantOverride = scope.variantOverrides[scope.hub_id][scope.variant.id]
|
||||
scope.inherit = false
|
||||
DirtyVariantOverrides.add variantOverride
|
||||
scope.displayDirty()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
%hr.divider.sixteen.columns.alpha.omega{ ng: { show: 'hub && products.length > 0' } }
|
||||
.controls.sixteen.columns.alpha.omega{ ng: { show: 'hub && products.length > 0' } }
|
||||
%hr.divider.sixteen.columns.alpha.omega{ ng: { show: 'hub_id && products.length > 0' } }
|
||||
.controls.sixteen.columns.alpha.omega{ ng: { show: 'hub_id && products.length > 0' } }
|
||||
.eight.columns.alpha
|
||||
= render 'admin/shared/bulk_actions_dropdown'
|
||||
= render 'admin/shared/views_dropdown'
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
.filters.sixteen.columns.alpha
|
||||
.filter.four.columns.alpha
|
||||
%label{ :for => 'query', ng: {class: '{disabled: !hub.id}'} }=t('admin.quick_search')
|
||||
%label{ :for => 'query', ng: {class: '{disabled: !hub_id}'} }=t('admin.quick_search')
|
||||
%br
|
||||
%input.fullwidth{ :type => "text", :id => 'query', ng: { model: 'query', disabled: '!hub.id'} }
|
||||
%input.fullwidth{ :type => "text", :id => 'query', ng: { model: 'query', disabled: '!hub_id'} }
|
||||
.two.columns
|
||||
.filter_select.four.columns
|
||||
%label{ :for => 'hub_id', ng: { bind: "hub_id ? '#{t('admin.shop')}' : '#{t('admin.inventory.select_a_shop')}'" } }
|
||||
%br
|
||||
%select.select2.fullwidth#hub_id{ 'ng-model' => 'hub_id', name: 'hub_id', ng: { options: 'hub.id as hub.name for (id, hub) in hubs', change: 'selectHub()' } }
|
||||
%select.select2.fullwidth#hub_id{ 'ng-model' => 'hub_id', name: 'hub_id', ng: { options: 'hub.id as hub.name for (id, hub) in hubs' } }
|
||||
.filter_select.four.columns
|
||||
%label{ :for => 'producer_filter', ng: {class: '{disabled: !hub.id}'} }=t('admin.producer')
|
||||
%label{ :for => 'producer_filter', ng: {class: '{disabled: !hub_id}'} }=t('admin.producer')
|
||||
%br
|
||||
%input.ofn-select2.fullwidth{ :id => 'producer_filter', type: 'number', data: 'producers', blank: "{id: 0, name: 'All'}", ng: { model: 'producerFilter', disabled: '!hub.id' } }
|
||||
%input.ofn-select2.fullwidth{ :id => 'producer_filter', type: 'number', data: 'producers', blank: "{id: 0, name: 'All'}", ng: { model: 'producerFilter', disabled: '!hub_id' } }
|
||||
-# .filter_select{ :class => "three columns" }
|
||||
-# %label{ :for => 'distributor_filter' }Hub
|
||||
-# %br
|
||||
@@ -23,4 +23,4 @@
|
||||
.filter_clear.two.columns.omega
|
||||
%label{ :for => 'clear_all_filters' }
|
||||
%br
|
||||
%input.red.fullwidth{ :type => 'button', :id => 'clear_all_filters', :value => "#{t('admin.clear_all')}", ng: { click: "resetSelectFilters()", disabled: '!hub.id'} }
|
||||
%input.red.fullwidth{ :type => 'button', :id => 'clear_all_filters', :value => "#{t('admin.clear_all')}", ng: { click: "resetSelectFilters()", disabled: '!hub_id'} }
|
||||
|
||||
@@ -11,12 +11,12 @@
|
||||
%th.variant=t('(admin.variant')
|
||||
%th.add=t('admin.inventory.add')
|
||||
%tbody{ bindonce: true, ng: { repeat: 'product in filteredProducts | limitTo:productLimit' } }
|
||||
%tr{ id: "v_{{variant.id}}", ng: { repeat: 'variant in product.variants | inventoryVariants:hub.id:views' } }
|
||||
%tr{ id: "v_{{variant.id}}", ng: { repeat: 'variant in product.variants | inventoryVariants:hub_id:views' } }
|
||||
%td.producer{ bo: { bind: 'producersByID[product.producer_id].name'} }
|
||||
%td.product{ bo: { bind: 'product.name'} }
|
||||
%td.variant
|
||||
%span{ bo: { bind: 'variant.display_name || ""'} }
|
||||
.variant-override-unit{ bo: { bind: 'variant.unit_to_display'} }
|
||||
%td.add
|
||||
%button.fullwidth.icon-plus{ ng: { click: "setVisibility(hub.id,variant.id,true)" } }
|
||||
%button.fullwidth.icon-plus{ ng: { click: "setVisibility(hub_id,variant.id,true)" } }
|
||||
= t('admin.inventory.add')
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
%div.sixteen.columns.alpha.omega#loading{ ng: { cloak: true, if: 'hub && products.length == 0 && RequestMonitor.loading' } }
|
||||
%div.sixteen.columns.alpha.omega#loading{ ng: { cloak: true, if: 'hub_id && products.length == 0 && RequestMonitor.loading' } }
|
||||
%img.spinner{ src: "/assets/spinning-circles.svg" }
|
||||
%h1 LOADING INVENTORY
|
||||
|
||||
@@ -12,15 +12,15 @@
|
||||
%th.add=t('admin.inventory.add')
|
||||
%th.hide=t('admin.inventory.hide')
|
||||
%tbody{ bindonce: true, ng: { repeat: 'product in filteredProducts | limitTo:productLimit' } }
|
||||
%tr{ id: "v_{{variant.id}}", ng: { repeat: 'variant in product.variants | inventoryVariants:hub.id:views' } }
|
||||
%tr{ id: "v_{{variant.id}}", ng: { repeat: 'variant in product.variants | inventoryVariants:hub_id:views' } }
|
||||
%td.producer{ bo: { bind: 'producersByID[product.producer_id].name'} }
|
||||
%td.product{ bo: { bind: 'product.name'} }
|
||||
%td.variant
|
||||
%span{ bo: { bind: 'variant.display_name || ""'} }
|
||||
.variant-override-unit{ bo: { bind: 'variant.unit_to_display'} }
|
||||
%td.add
|
||||
%button.fullwidth.icon-plus{ ng: { click: "setVisibility(hub.id,variant.id,true)" } }
|
||||
%button.fullwidth.icon-plus{ ng: { click: "setVisibility(hub_id,variant.id,true)" } }
|
||||
= t('admin.inventory.add')
|
||||
%td.hide
|
||||
%button.fullwidth.hide.icon-remove{ ng: { click: "setVisibility(hub.id,variant.id,false)" } }
|
||||
%button.fullwidth.hide.icon-remove{ ng: { click: "setVisibility(hub_id,variant.id,false)" } }
|
||||
= t('admin.inventory.hide')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
%div{ ng: { show: '(newProductCount = (products | hubPermissions:hubPermissions:hub.id | newInventoryProducts:hub_id).length) > 0 && !views.new.visible && !alertDismissed' } }
|
||||
%div{ ng: { show: '(newProductCount = (products | hubPermissions:hubPermissions:hub_id | newInventoryProducts:hub_id).length) > 0 && !views.new.visible && !alertDismissed' } }
|
||||
%hr.divider.sixteen.columns.alpha.omega
|
||||
%alert-row{ message: "#{t('admin.inventory.new_products_alert_message', new_product_count: '{{ newProductCount }}')}",
|
||||
dismissed: "alertDismissed",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
%div.text-big.no-results{ ng: { show: 'hub && products.length > 0 && filteredProducts.length == 0' } }
|
||||
%div.text-big.no-results{ ng: { show: 'hub_id && products.length > 0 && filteredProducts.length == 0' } }
|
||||
%span{ ng: { show: 'views.inventory.visible && !filtersApplied()' } }=t('admin.inventory.currently_empty')
|
||||
%span{ ng: { show: 'views.inventory.visible && filtersApplied()' } }=t('admin.inventory.no_matching_products')
|
||||
%span{ ng: { show: 'views.hidden.visible && !filtersApplied()' } }=t('admin.inventory.no_hidden_products')
|
||||
|
||||
@@ -22,6 +22,6 @@
|
||||
%th.reset{ colspan: 2, ng: { show: 'columns.reset.visible' } }=t('admin.inventory.enable_reset')
|
||||
%th.inheritance{ ng: { show: 'columns.inheritance.visible' } }=t('admin.inventory.inherit')
|
||||
%th.visibility{ ng: { show: 'columns.visibility.visible' } }=t('admin.inventory.hide')
|
||||
%tbody{bindonce: true, ng: {repeat: 'product in filteredProducts = (products | hubPermissions:hubPermissions:hub.id | inventoryProducts:hub.id:views | attrFilter:{producer_id:producerFilter} | filter:query) | limitTo:productLimit' } }
|
||||
%tbody{bindonce: true, ng: {repeat: 'product in filteredProducts = (products | hubPermissions:hubPermissions:hub_id | inventoryProducts:hub_id:views | attrFilter:{producer_id:producerFilter} | filter:query) | limitTo:productLimit' } }
|
||||
= render 'admin/variant_overrides/products_product'
|
||||
= render 'admin/variant_overrides/products_variants'
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
%tr.variant{ id: "v_{{variant.id}}", ng: {repeat: 'variant in product.variants | inventoryVariants:hub.id:views'}}
|
||||
%tr.variant{ id: "v_{{variant.id}}", ng: {repeat: 'variant in product.variants | inventoryVariants:hub_id:views'}}
|
||||
%td.producer{ ng: { show: 'columns.producer.visible' } }
|
||||
%td.product{ ng: { show: 'columns.product.visible' } }
|
||||
%span{ bo: { bind: 'variant.display_name || ""'} }
|
||||
.variant-override-unit{ bo: { bind: 'variant.unit_to_display'} }
|
||||
%td.sku{ ng: { show: 'columns.sku.visible' } }
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-sku', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].sku'}, placeholder: '{{ variant.sku }}', 'ofn-track-variant-override' => 'sku'}
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-sku', type: 'text', ng: {model: 'variantOverrides[hub_id][variant.id].sku'}, placeholder: '{{ variant.sku }}', 'ofn-track-variant-override' => 'sku'}
|
||||
%td.price{ ng: { show: 'columns.price.visible' } }
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-price', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].price'}, placeholder: '{{ variant.price }}', 'ofn-track-variant-override' => 'price'}
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-price', type: 'text', ng: {model: 'variantOverrides[hub_id][variant.id].price'}, placeholder: '{{ variant.price }}', 'ofn-track-variant-override' => 'price'}
|
||||
%td.on_hand{ ng: { show: 'columns.on_hand.visible' } }
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-count_on_hand', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].count_on_hand'}, placeholder: '{{ variant.on_hand }}', 'ofn-track-variant-override' => 'count_on_hand'}
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-count_on_hand', type: 'text', ng: {model: 'variantOverrides[hub_id][variant.id].count_on_hand'}, placeholder: '{{ variant.on_hand }}', 'ofn-track-variant-override' => 'count_on_hand'}
|
||||
%td.on_demand{ ng: { show: 'columns.on_demand.visible' } }
|
||||
%input.field{ :type => 'checkbox', name: 'variant-overrides-{{ variant.id }}-on_demand', ng: { model: 'variantOverrides[hub.id][variant.id].on_demand' }, 'ofn-track-variant-override' => 'on_demand' }
|
||||
%input.field{ :type => 'checkbox', name: 'variant-overrides-{{ variant.id }}-on_demand', ng: { model: 'variantOverrides[hub_id][variant.id].on_demand' }, 'ofn-track-variant-override' => 'on_demand' }
|
||||
%td.reset{ ng: { show: 'columns.reset.visible' } }
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-resettable', type: 'checkbox', ng: {model: 'variantOverrides[hub.id][variant.id].resettable'}, placeholder: '{{ variant.resettable }}', 'ofn-track-variant-override' => 'resettable'}
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-resettable', type: 'checkbox', ng: {model: 'variantOverrides[hub_id][variant.id].resettable'}, placeholder: '{{ variant.resettable }}', 'ofn-track-variant-override' => 'resettable'}
|
||||
%td.reset{ ng: { show: 'columns.reset.visible' } }
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-default_stock', type: 'text', ng: {model: 'variantOverrides[hub.id][variant.id].default_stock'}, placeholder: '{{ variant.default_stock ? variant.default_stock : "Default stock"}}', 'ofn-track-variant-override' => 'default_stock'}
|
||||
%input{name: 'variant-overrides-{{ variant.id }}-default_stock', type: 'text', ng: {model: 'variantOverrides[hub_id][variant.id].default_stock'}, placeholder: '{{ variant.default_stock ? variant.default_stock : "Default stock"}}', 'ofn-track-variant-override' => 'default_stock'}
|
||||
%td.inheritance{ ng: { show: 'columns.inheritance.visible' } }
|
||||
%input.field{ :type => 'checkbox', name: 'variant-overrides-{{ variant.id }}-inherit', ng: { model: 'inherit' }, 'track-inheritance' => true }
|
||||
%td.visibility{ ng: { show: 'columns.visibility.visible' } }
|
||||
%button.icon-remove.hide.fullwidth{ :type => 'button', ng: { click: "setVisibility(hub.id,variant.id,false)" } }
|
||||
%button.icon-remove.hide.fullwidth{ :type => 'button', ng: { click: "setVisibility(hub_id,variant.id,false)" } }
|
||||
= t('admin.inventory.hide')
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
= render 'admin/variant_overrides/loading_flash'
|
||||
= render 'admin/variant_overrides/controls'
|
||||
= render 'admin/variant_overrides/no_results'
|
||||
%div{ ng: { cloak: true, show: 'hub && filteredProducts.length > 0' } }
|
||||
%div{ ng: { cloak: true, show: 'hub_id && filteredProducts.length > 0' } }
|
||||
= render 'admin/variant_overrides/new_products'
|
||||
= render 'admin/variant_overrides/hidden_products'
|
||||
= render 'admin/variant_overrides/products'
|
||||
|
||||
@@ -24,7 +24,7 @@ feature %q{
|
||||
it "displays a list of hub choices (ie. only those managed by the user)" do
|
||||
visit '/admin/inventory'
|
||||
|
||||
page.should have_select2 'hub_id', options: ['', hub.name]
|
||||
page.should have_select2 'hub_id', options: [hub.name] # Selects the hub automatically when only one is available
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -28,9 +28,20 @@ describe "VariantOverridesCtrl", ->
|
||||
StatusMessage = _StatusMessage_
|
||||
ctrl = $controller 'AdminVariantOverridesCtrl', { $scope: scope, hubs: hubs, producers: producers, products: products, hubPermissions: hubPermissions, VariantOverrides: VariantOverrides, DirtyVariantOverrides: DirtyVariantOverrides, StatusMessage: StatusMessage}
|
||||
|
||||
it "initialises the hub list and the chosen hub", ->
|
||||
expect(scope.hubs).toEqual { 1: {id: 1, name: 'Hub'} }
|
||||
expect(scope.hub).toBeNull()
|
||||
describe "when only one hub is available", ->
|
||||
it "initialises the hub list and the selects the only hub in the list", ->
|
||||
expect(scope.hubs).toEqual { 1: {id: 1, name: 'Hub'} }
|
||||
expect(scope.hub_id).toEqual 1
|
||||
|
||||
describe "when more than one hub is available", ->
|
||||
beforeEach ->
|
||||
inject ($controller) ->
|
||||
hubs = [{id: 1, name: 'Hub1'}, {id: 12, name: 'Hub2'}]
|
||||
$controller 'AdminVariantOverridesCtrl', { $scope: scope, hubs: hubs, producers: [], products: [], hubPermissions: []}
|
||||
|
||||
it "initialises the hub list and the selects the only hub in the list", ->
|
||||
expect(scope.hubs).toEqual { 1: {id: 1, name: 'Hub1'}, 12: {id: 12, name: 'Hub2'} }
|
||||
expect(scope.hub_id).toBeNull()
|
||||
|
||||
it "initialises select filters", ->
|
||||
expect(scope.producerFilter).toEqual 0
|
||||
@@ -45,17 +56,6 @@ describe "VariantOverridesCtrl", ->
|
||||
expect(scope.products).toEqual ['a', 'b', 'c', 'd']
|
||||
expect(VariantOverrides.ensureDataFor).toHaveBeenCalled()
|
||||
|
||||
describe "selecting a hub", ->
|
||||
it "sets the chosen hub", ->
|
||||
scope.hub_id = 1
|
||||
scope.selectHub()
|
||||
expect(scope.hub).toEqual hubs[0]
|
||||
|
||||
it "does nothing when no selection has been made", ->
|
||||
scope.hub_id = ''
|
||||
scope.selectHub
|
||||
expect(scope.hub).toBeNull
|
||||
|
||||
describe "updating", ->
|
||||
describe "error messages", ->
|
||||
it "returns an unauthorised message upon 401", ->
|
||||
|
||||
Reference in New Issue
Block a user