mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Add basic hard filter adding js infrastructure for BPE
This commit is contained in:
@@ -138,10 +138,15 @@ productsApp.controller "AdminBulkProductsCtrl", [
|
||||
["Items", "items"]
|
||||
]
|
||||
|
||||
$scope.filterTypes = [
|
||||
{ name: "Equals", ransack_predicate: "eq" }
|
||||
]
|
||||
|
||||
$scope.perPage = 25
|
||||
$scope.currentPage = 1
|
||||
$scope.products = []
|
||||
$scope.filteredProducts = []
|
||||
$scope.currentFilters = []
|
||||
$scope.totalCount = -> $scope.filteredProducts.length
|
||||
$scope.totalPages = -> Math.ceil($scope.totalCount()/$scope.perPage)
|
||||
$scope.firstVisibleProduct = -> ($scope.currentPage-1)*$scope.perPage+1
|
||||
@@ -225,6 +230,12 @@ productsApp.controller "AdminBulkProductsCtrl", [
|
||||
onHand
|
||||
|
||||
|
||||
$scope.addFilter = (filter) ->
|
||||
if Object.keys($scope.columns).indexOf(filter.property) >= 0
|
||||
if $scope.filterTypes.map( (filter_type) -> filter_type.ransack_predicate ).indexOf(filter.ransack_predicate) >= 0
|
||||
$scope.currentFilters.push filter
|
||||
|
||||
|
||||
$scope.editWarn = (product, variant) ->
|
||||
if ($scope.dirtyProductCount() > 0 and confirm("Unsaved changes will be lost. Continue anyway?")) or ($scope.dirtyProductCount() == 0)
|
||||
window.location = "/admin/products/" + product.permalink_live + ((if variant then "/variants/" + variant.id else "")) + "/edit"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
describe "filtering products", ->
|
||||
describe "filtering products for submission to database", ->
|
||||
it "accepts an object or an array and only returns an array", ->
|
||||
expect(filterSubmitProducts([])).toEqual []
|
||||
expect(filterSubmitProducts({})).toEqual []
|
||||
@@ -639,6 +639,7 @@ describe "AdminBulkProductsCtrl", ->
|
||||
]
|
||||
scope.updateProducts "list of dirty products"
|
||||
httpBackend.flush()
|
||||
timeout.flush()
|
||||
expect(scope.displaySuccess).toHaveBeenCalled()
|
||||
|
||||
it "runs displayFailure() when post return data does not match $scope.products", ->
|
||||
@@ -898,6 +899,33 @@ describe "AdminBulkProductsCtrl", ->
|
||||
]
|
||||
|
||||
|
||||
describe "filtering products", ->
|
||||
describe "adding a filter to the filter list", ->
|
||||
it "adds objects sent to addFilter() to $scope.currentFilters", ->
|
||||
filterObject1 = {property: "name", ransack_predicate: "eq", value: "Product1"}
|
||||
filterObject2 = {property: "name", ransack_predicate: "eq", value: "Product2"}
|
||||
scope.addFilter(filterObject1)
|
||||
scope.addFilter(filterObject2)
|
||||
expect(scope.currentFilters).toEqual [filterObject1, filterObject2]
|
||||
|
||||
it "ignores objects sent to addFilter() which do not contain a 'property' with a corresponding key in $scope.columns", ->
|
||||
filterObject1 = {property: Object.keys(scope.columns)[0], ransack_predicate: "eq", value: "value1"}
|
||||
filterObject2 = {property: Object.keys(scope.columns)[2], ransack_predicate: "eq", value: "value2"}
|
||||
filterObject3 = {property: "some_random_property", ransack_predicate: "eq", value: "value3"}
|
||||
scope.addFilter(filterObject1)
|
||||
scope.addFilter(filterObject2)
|
||||
scope.addFilter(filterObject3)
|
||||
expect(scope.currentFilters).toEqual [filterObject1, filterObject2]
|
||||
|
||||
it "ignores objects sent to addFilter() which do not contain a query with a corresponding key in ", ->
|
||||
filterObject1 = {property: Object.keys(scope.columns)[0], ransack_predicate: "eq", value: "value1"}
|
||||
filterObject2 = {property: Object.keys(scope.columns)[2], ransack_predicate: "eq", value: "value2"}
|
||||
filterObject3 = {property: Object.keys(scope.columns)[4], ransack_predicate: "something", value: "value3"}
|
||||
scope.addFilter(filterObject1)
|
||||
scope.addFilter(filterObject2)
|
||||
scope.addFilter(filterObject3)
|
||||
expect(scope.currentFilters).toEqual [filterObject1, filterObject2]
|
||||
|
||||
|
||||
describe "converting arrays of objects with ids to an object with ids as keys", ->
|
||||
it "returns an object", ->
|
||||
|
||||
Reference in New Issue
Block a user