mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-22 05:18:51 +00:00
Make a more generic solution: KeyValueMapStore
- As QueryPersistence, stored in the localStorage too but it is now more generic and has nothing to do with Query - Add some js unit testing
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, RequestMonitor, Orders, SortOptions, $window, $filter, $location, QueryPersistence) ->
|
||||
angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, RequestMonitor, Orders, SortOptions, $window, $filter, $location, KeyValueMapStore) ->
|
||||
$scope.RequestMonitor = RequestMonitor
|
||||
$scope.pagination = Orders.pagination
|
||||
$scope.orders = Orders.all
|
||||
@@ -15,11 +15,11 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, Reque
|
||||
$scope.poll = 0
|
||||
$scope.rowStatus = {}
|
||||
|
||||
QueryPersistence.storageKey = 'ordersFilters'
|
||||
QueryPersistence.storableFilters = ["q", "sorting", "page", "per_page"]
|
||||
KeyValueMapStore.localStorageKey = 'ordersFilters'
|
||||
KeyValueMapStore.storableKeys = ["q", "sorting", "page", "per_page"]
|
||||
|
||||
$scope.initialise = ->
|
||||
unless QueryPersistence.restoreFilters($scope)
|
||||
unless KeyValueMapStore.restoreValues($scope)
|
||||
$scope.setDefaults()
|
||||
|
||||
$scope.fetchResults()
|
||||
@@ -31,7 +31,7 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, Reque
|
||||
}
|
||||
|
||||
$scope.clearFilters = () ->
|
||||
QueryPersistence.clearFilters()
|
||||
KeyValueMapStore.clearKeyValueMap()
|
||||
$scope.setDefaults()
|
||||
$scope.fetchResults()
|
||||
|
||||
@@ -57,7 +57,7 @@ angular.module("admin.orders").controller "ordersCtrl", ($scope, $timeout, Reque
|
||||
per_page: $scope.per_page,
|
||||
page: page
|
||||
}
|
||||
QueryPersistence.setStoredFilters($scope)
|
||||
KeyValueMapStore.setStoredValues($scope)
|
||||
RequestMonitor.load(Orders.index(params).$promise)
|
||||
|
||||
$scope.appendStringIfNotEmpty = (baseString, stringToAppend) ->
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
angular.module("admin.indexUtils").factory 'KeyValueMapStore', (localStorageService)->
|
||||
new class KeyValueMapStore
|
||||
localStorageKey: ''
|
||||
storableKeys: []
|
||||
|
||||
constructor: ->
|
||||
localStorageService.setStorageType("sessionStorage")
|
||||
|
||||
getStoredKeyValueMap: ->
|
||||
localStorageService.get(@localStorageKey) || {}
|
||||
|
||||
setStoredValues: (source) ->
|
||||
keyValueMap = {}
|
||||
for key in @storableKeys
|
||||
keyValueMap[key] = source[key]
|
||||
localStorageService.set(@localStorageKey, keyValueMap)
|
||||
|
||||
restoreValues: (target) ->
|
||||
storedKeyValueMap = @getStoredKeyValueMap()
|
||||
|
||||
return false if _.isEmpty(storedKeyValueMap)
|
||||
|
||||
for k,v of storedKeyValueMap
|
||||
target[k] = v
|
||||
|
||||
return true
|
||||
|
||||
clearKeyValueMap: () ->
|
||||
localStorageService.remove(@localStorageKey)
|
||||
@@ -1,30 +0,0 @@
|
||||
angular.module("admin.indexUtils").factory 'QueryPersistence', (localStorageService)->
|
||||
new class QueryPersistence
|
||||
storageKey: ''
|
||||
storableFilters: []
|
||||
|
||||
constructor: ->
|
||||
localStorageService.setStorageType("sessionStorage")
|
||||
|
||||
getStoredFilters: ->
|
||||
localStorageService.get(@storageKey) || {}
|
||||
|
||||
setStoredFilters: (scope) ->
|
||||
filters = {}
|
||||
for key in @storableFilters
|
||||
filters[key] = scope[key]
|
||||
localStorageService.set(@storageKey, filters)
|
||||
|
||||
restoreFilters: (scope) ->
|
||||
storedFilters = @getStoredFilters()
|
||||
|
||||
unless _.isEmpty(storedFilters)
|
||||
for k,v of storedFilters
|
||||
scope[k] = v
|
||||
|
||||
return true
|
||||
|
||||
false
|
||||
|
||||
clearFilters: () ->
|
||||
localStorageService.remove(@storageKey)
|
||||
@@ -0,0 +1,38 @@
|
||||
describe "Test KeyValueMapStore service", ->
|
||||
|
||||
KeyValueMapStore = null
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin"
|
||||
|
||||
beforeEach inject (_KeyValueMapStore_) ->
|
||||
KeyValueMapStore = _KeyValueMapStore_
|
||||
|
||||
it "set and restore filters", ->
|
||||
KeyValueMapStore.localStorageKey = 'localStorageKey'
|
||||
KeyValueMapStore.storableKeys = ["a", "b", "c"]
|
||||
source =
|
||||
a: "1",
|
||||
b: "2",
|
||||
d: "4"
|
||||
KeyValueMapStore.setStoredValues(source)
|
||||
source = {}
|
||||
restored = KeyValueMapStore.restoreValues(source)
|
||||
expect(restored).toEqual true
|
||||
expect(source).toEqual {a: '1', b: '2'}
|
||||
|
||||
it "clear filters", ->
|
||||
KeyValueMapStore.storageKey = 'localStorageKey'
|
||||
KeyValueMapStore.storableFilters = ["a", "b", "c"]
|
||||
source =
|
||||
a: "1",
|
||||
b: "2",
|
||||
d: "4"
|
||||
KeyValueMapStore.setStoredValues(source)
|
||||
KeyValueMapStore.clearKeyValueMap()
|
||||
source = {}
|
||||
restored = KeyValueMapStore.restoreValues(source)
|
||||
expect(restored).toEqual false
|
||||
expect(source).toEqual {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user