fix integer fields in removeParams; maintain page_size/sort

This commit is contained in:
Keith J. Grant
2021-05-18 12:00:20 -07:00
parent 25903431bc
commit fd5e22a3f6
4 changed files with 48 additions and 5 deletions

View File

@@ -73,6 +73,8 @@ class ListHeader extends React.Component {
Object.keys(oldParams).forEach(key => { Object.keys(oldParams).forEach(key => {
oldParams[key] = null; oldParams[key] = null;
}); });
delete oldParams.page_size;
delete oldParams.order_by;
const qs = replaceNamespacedParams(qsConfig, location.search, oldParams); const qs = replaceNamespacedParams(qsConfig, location.search, oldParams);
this.pushHistoryState(qs); this.pushHistoryState(qs);
} }

View File

@@ -71,7 +71,7 @@ describe('ListHeader', () => {
expect(history.location.search).toEqual(query); expect(history.location.search).toEqual(query);
const toolbar = wrapper.find('DataListToolbar'); const toolbar = wrapper.find('DataListToolbar');
toolbar.prop('clearAllFilters')(); toolbar.prop('clearAllFilters')();
expect(history.location.search).toEqual(''); expect(history.location.search).toEqual('?item.page_size=5');
}); });
test('should test handle search', () => { test('should test handle search', () => {

View File

@@ -163,11 +163,19 @@ export function removeParams(config, oldParams, paramsToRemove) {
...config.defaultParams, ...config.defaultParams,
}; };
Object.keys(oldParams).forEach(key => { Object.keys(oldParams).forEach(key => {
const value = removeParam(oldParams[key], paramsToRemove[key]); const valToRemove = paramsToRemove[key];
if (value == null && Object.prototype.hasOwnProperty.call(updated, key)) { const isInt = config.integerFields?.includes(key);
const updatedValue = removeParam(
oldParams[key],
isInt ? parseInt(valToRemove, 10) : valToRemove
);
if (
updatedValue == null &&
Object.prototype.hasOwnProperty.call(updated, key)
) {
return; return;
} }
updated[key] = value; updated[key] = updatedValue;
}); });
return updated; return updated;
} }
@@ -253,7 +261,8 @@ export function replaceParams(oldParams, newParams) {
* from other namespaces unaltered * from other namespaces unaltered
* @param {object} qs config object for namespacing params, filtering defaults * @param {object} qs config object for namespacing params, filtering defaults
* @param {string} the url query string to update * @param {string} the url query string to update
* @param {object} namespaced params to add or update * @param {object} namespaced params to add or update. use null to indicate
* a param should be deleted from the query string
* @return {string} url query string * @return {string} url query string
*/ */
export function replaceNamespacedParams(config, queryString, newParams) { export function replaceNamespacedParams(config, queryString, newParams) {

View File

@@ -570,6 +570,38 @@ describe('qs (qs.js)', () => {
page_size: 15, page_size: 15,
}); });
}); });
test('should remove integer fields when given string value', () => {
const config = {
namespace: null,
defaultParams: { page: 1, page_size: 15 },
integerFields: ['id', 'page', 'page_size'],
};
const oldParams = { id: 199, foo: 'bar', page: 1, page_size: 15 };
const toRemove = { id: '199' };
expect(removeParams(config, oldParams, toRemove)).toEqual({
foo: 'bar',
id: null,
page: 1,
page_size: 15,
});
});
test('should remove integer fields from array when given string value', () => {
const config = {
namespace: null,
defaultParams: { page: 1, page_size: 15 },
integerFields: ['id', 'page', 'page_size'],
};
const oldParams = { id: [199, 200], foo: 'bar', page: 1, page_size: 15 };
const toRemove = { id: '199' };
expect(removeParams(config, oldParams, toRemove)).toEqual({
foo: 'bar',
id: 200,
page: 1,
page_size: 15,
});
});
}); });
describe('_stringToObject', () => { describe('_stringToObject', () => {