Skip to content

Commit

Permalink
fix(journal): cache filters typo
Browse files Browse the repository at this point in the history
This commit fixes a typo that caused filters to no longer be cached on
the journal.  The code has also been improved by only checking for the
cacheable flag when the filter is attempting to be cached.  This avoids
having to add any values to the modules that use FilterService.

Additionally, I've updated all files I've manipulated to pass ESLint.

Closes #2521.
  • Loading branch information
jniles committed Feb 8, 2018
1 parent 1287f2a commit 14a28c0
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 73 deletions.
113 changes: 70 additions & 43 deletions client/src/js/services/FilterService.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,35 @@ function FilterService() {
};

FilterList.prototype._resetCustomFilters = function resetCustomFilters() {
this._filterActiveFilters().forEach(function (filter) {
this._filterActiveFilters().forEach(filter => {
// only by default remove custom values
if (!filter._isDefault) {
this.resetFilterState(filter._key);
}
}.bind(this));
});
};

// @TODO registerDefaultFilter and registerCustomFilter could use the same underlying function
// with a toggle between the array to populate and the default value
FilterList.prototype.registerDefaultFilters = function registerDefaultFilters(filterDefinitions) {
var formattedFilters = filterDefinitions.map(function (filterDefinition) {
var filter = new Filter(filterDefinition.key, filterDefinition.label, filterDefinition.valueFilter, filterDefinition.comparitor);
const formattedFilters = filterDefinitions.map(filterDefinition => {
const filter = new Filter(
filterDefinition.key,
filterDefinition.label,
filterDefinition.valueFilter,
filterDefinition.comparitor
);

filter.setDefault(true);

if (filterDefinition.defaultValue) {
if (angular.isDefined(filterDefinition.defaultValue)) {
filter.setValue(filterDefinition.defaultValue);
}

if (angular.isDefined(filterDefinition.cacheable)) {
filter.setCacheable(filterDefinition.cacheable);
}

return filter;
});

Expand All @@ -42,9 +52,20 @@ function FilterService() {
};

FilterList.prototype.registerCustomFilters = function registerCustomFilters(filterDefinitions) {
var formattedFilters = filterDefinitions.map(function (filterDefinition) {
var filter = new Filter(filterDefinition.key, filterDefinition.label, filterDefinition.valueFilter, filterDefinition.comparitor);
const formattedFilters = filterDefinitions.map(filterDefinition => {
const filter = new Filter(
filterDefinition.key,
filterDefinition.label,
filterDefinition.valueFilter,
filterDefinition.comparitor
);

filter.setDefault(false);

if (angular.isDefined(filterDefinition.cacheable)) {
filter.setCacheable(filterDefinition.cacheable);
}

return filter;
});

Expand All @@ -66,9 +87,9 @@ function FilterService() {
// { key : value }
// ]
FilterList.prototype.assignFilters = function assignFilters(valueList) {
valueList.forEach(function (valueMap) {
valueList.forEach(valueMap => {
this.assignFilter(valueMap.key, valueMap.value, valueMap.displayValue, valueMap.comparitor);
}.bind(this));
});
};

// alias for `assignFilters`, clears the currently active filters before
Expand All @@ -80,37 +101,35 @@ function FilterService() {

// uses angular.copy() to break references to old values
FilterList.prototype.replaceFiltersFromState = function replaceFiltersFromState(stateFilterList) {
var changes = angular.copy(stateFilterList);
const changes = angular.copy(stateFilterList);
this.replaceFilters(changes);
};

// return filters for the view - this method will always be compatible with the bhFilter component
FilterList.prototype.formatView = function formatView() {
var activeFilters = this._filterActiveFilters();
var activeKeys = activeFilters.map(function (filter) {
return filter._key;
});
const activeFilters = this._filterActiveFilters();
const activeKeys = activeFilters.map(filter => filter._key);

function keysInActive(filter) {
return activeKeys.indexOf(filter._key) !== -1;
return activeKeys.includes(filter._key);
}

// parse into two lists
return {
defaultFilters: this._defaultFilters.filter(keysInActive),
customFilters: this._customFilters.filter(keysInActive),
defaultFilters : this._defaultFilters.filter(keysInActive),
customFilters : this._customFilters.filter(keysInActive),
};
};

// format filters for the server
// sendClientTimestamp - this will send an attribute hidden to the user
// returns a JSON object with active filters
FilterList.prototype.formatHTTP = function formatHTTP(hasClientTimestamp) {
var clientTimestamp = angular.isDefined(hasClientTimestamp) ? hasClientTimestamp : false;
var activeFilters = this._filterActiveFilters();
const clientTimestamp = angular.isDefined(hasClientTimestamp) ? hasClientTimestamp : false;
const activeFilters = this._filterActiveFilters();

// format current filters correctly
var httpFilters = activeFilters.reduce(function (aggregate, filter) {
const httpFilters = activeFilters.reduce((aggregate, filter) => {
aggregate[filter._key] = filter._value;
return aggregate;
}, {});
Expand All @@ -124,54 +143,53 @@ function FilterService() {

// returns an array of labels and overriden labels that is built for the FilterParser API
FilterList.prototype.formatHTTPLabels = function formatHTTPLabels() {
var activeFilters = this._filterActiveFilters();

return activeFilters.map(function (filter) {
return filter._key.concat(':', filter._label);
});
const activeFilters = this._filterActiveFilters();
return activeFilters.map(filter => `${filter._key}:${filter._label}`);
};

FilterList.prototype.formatCache = function formatCache() {
return angular.copy(this._filterIndex);
};
const copy = angular.copy(this._filterIndex);

FilterList.prototype.removeUnCachableFilters = function removeUnCachableFilters(filters) {
filters.forEach(filter => {
if (angular.isDefined(filter.cacheable) && filter.cachable === 0) {
delete this._filterIndex[filter.key];
// reset all values of filters that should not be cached
Object.values(copy).forEach(filter => {
if (!filter.isCacheable()) {
filter.setValue(null, null);
}
});

return copy;
};

// replaces current filters with filters from cache
FilterList.prototype.loadCache = function loadCache(storedCache) {
Object.keys(storedCache).forEach(function (key) {
var cached = storedCache[key];
var currentFilter = this._filterIndex[key];
Object.keys(storedCache).forEach(key => {
const cached = storedCache[key];
const currentFilter = this._filterIndex[key];
if (currentFilter) {
currentFilter.setValue(cached._value, cached._displayValue);
}
}.bind(this));
});
};

FilterList.prototype._indexList = function indexList(index, list) {
index = list.reduce(function (aggregateIndex, filterDefinition) {
list.reduce((aggregateIndex, filterDefinition) => {
aggregateIndex[filterDefinition._key] = filterDefinition;
return aggregateIndex;
}, index);
};

// returns a flat array of filters that have values (and should be applied)
FilterList.prototype._filterActiveFilters = function filterActiveFilters() {
var filtered = [];
const filtered = [];

Object.keys(this._filterIndex).forEach(function (key) {
var filter = this._filterIndex[key];
Object.keys(this._filterIndex).forEach(key => {
const filter = this._filterIndex[key];

if (filter._value !== null && filter._value !== undefined && filter._value.length !== '') {
filtered.push(angular.copy(filter));
}
}.bind(this));
});

return filtered;
};

Expand All @@ -186,8 +204,8 @@ function FilterService() {
* @returns {Object} - { key : displayValue }
*/
FilterList.prototype.getDisplayValueMap = function getDisplayValueMap() {
var viewFilters = this.formatView().customFilters;
return viewFilters.reduce(function (o, filter) {
const viewFilters = this.formatView().customFilters;
return viewFilters.reduce((o, filter) => {
o[filter._key] = filter.displayValue;
return o;
}, {});
Expand All @@ -210,10 +228,19 @@ function Filter(key, label, valueFilter, comparitor) {
this._isDefault = null;
this._displayValue = null;

this.setDefault = function setDefault(value) {
// default to cacheable filters
this._isCacheable = true;

this.setDefault = (value) => {
this._isDefault = value;
};

this.setCacheable = (bool) => {
this._isCacheable = bool;
};

this.isCacheable = () => this._isCacheable;

this.setValue = function setValue(value, displayValue) {
this._value = value;
this._displayValue = displayValue;
Expand Down
17 changes: 8 additions & 9 deletions client/src/js/services/TransactionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ angular.module('bhima.services')
TransactionService.$inject = ['$http', 'util', '$uibModal'];

function TransactionService($http, util, Modal) {
var service = this;
var baseUrl = '/transactions/';
const service = this;
const baseUrl = '/transactions/';

service.remove = remove;
service.comment = comment;
Expand All @@ -21,22 +21,21 @@ function TransactionService($http, util, Modal) {
* payment as necessary.
*/
function remove(uuid) {
var url = baseUrl.concat(uuid);
const url = baseUrl.concat(uuid);
return $http.delete(url)
.then(util.unwrapHttpResponse);
}


/**
* @method comment
*
* @description
* This function comments on individual lines of a transaction. It is used by
* the comment modal to modify, remove or add comments to transactions.
*/
function comment(params) {
var url = baseUrl.concat('comments');
return $http.put(url, { params : params })
const url = baseUrl.concat('comments');
return $http.put(url, { params })
.then(util.unwrapHttpResponse);
}

Expand All @@ -48,12 +47,12 @@ function TransactionService($http, util, Modal) {
* rows of transactions.
*/
function openCommentModal(rows) {
var config = {
const config = {
templateUrl : 'modules/journal/modals/comment.modal.html',
controller : 'CommentModalController',
controllerAs : '$ctrl',
resolve : {
params : function paramsProvider() { return rows; },
params : () => rows,
},
};

Expand All @@ -67,7 +66,7 @@ function TransactionService($http, util, Modal) {
* This function loads the history of a given transaction from the database.
*/
function historyFn(uuid) {
var url = baseUrl.concat(uuid, '/history');
const url = baseUrl.concat(uuid, '/history');
return $http.get(url)
.then(util.unwrapHttpResponse);
}
Expand Down
10 changes: 7 additions & 3 deletions client/src/modules/journal/journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,15 @@ function JournalController(

// runs on startup
function startup() {
const params = $state.params.filters;
Journal.filters.replaceFiltersFromState(params);
const { filters } = $state.params;
if (filters.length > 0) {
Journal.filters.replaceFiltersFromState(filters);
} else {
Journal.loadCachedFilters();
}

load(Journal.filters.formatHTTP(true));
vm.latestViewFilters = Journal.filters.formatView();
Journal.filters.removeUnCachableFilters(params);
}

startup();
Expand Down

0 comments on commit 14a28c0

Please sign in to comment.