From fd5e22a3f6ecfac0f8d47685503e6f5f87cff895 Mon Sep 17 00:00:00 2001 From: "Keith J. Grant" Date: Tue, 18 May 2021 12:00:20 -0700 Subject: [PATCH] fix integer fields in removeParams; maintain page_size/sort --- .../src/components/ListHeader/ListHeader.jsx | 2 ++ .../components/ListHeader/ListHeader.test.jsx | 2 +- awx/ui_next/src/util/qs.js | 17 +++++++--- awx/ui_next/src/util/qs.test.js | 32 +++++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/awx/ui_next/src/components/ListHeader/ListHeader.jsx b/awx/ui_next/src/components/ListHeader/ListHeader.jsx index 3e200a7c45..d24d3899ba 100644 --- a/awx/ui_next/src/components/ListHeader/ListHeader.jsx +++ b/awx/ui_next/src/components/ListHeader/ListHeader.jsx @@ -73,6 +73,8 @@ class ListHeader extends React.Component { Object.keys(oldParams).forEach(key => { oldParams[key] = null; }); + delete oldParams.page_size; + delete oldParams.order_by; const qs = replaceNamespacedParams(qsConfig, location.search, oldParams); this.pushHistoryState(qs); } diff --git a/awx/ui_next/src/components/ListHeader/ListHeader.test.jsx b/awx/ui_next/src/components/ListHeader/ListHeader.test.jsx index 76ddd2b8af..f7199fe4d4 100644 --- a/awx/ui_next/src/components/ListHeader/ListHeader.test.jsx +++ b/awx/ui_next/src/components/ListHeader/ListHeader.test.jsx @@ -71,7 +71,7 @@ describe('ListHeader', () => { expect(history.location.search).toEqual(query); const toolbar = wrapper.find('DataListToolbar'); toolbar.prop('clearAllFilters')(); - expect(history.location.search).toEqual(''); + expect(history.location.search).toEqual('?item.page_size=5'); }); test('should test handle search', () => { diff --git a/awx/ui_next/src/util/qs.js b/awx/ui_next/src/util/qs.js index fd952cba46..a5e0005626 100644 --- a/awx/ui_next/src/util/qs.js +++ b/awx/ui_next/src/util/qs.js @@ -163,11 +163,19 @@ export function removeParams(config, oldParams, paramsToRemove) { ...config.defaultParams, }; Object.keys(oldParams).forEach(key => { - const value = removeParam(oldParams[key], paramsToRemove[key]); - if (value == null && Object.prototype.hasOwnProperty.call(updated, key)) { + const valToRemove = paramsToRemove[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; } - updated[key] = value; + updated[key] = updatedValue; }); return updated; } @@ -253,7 +261,8 @@ export function replaceParams(oldParams, newParams) { * from other namespaces unaltered * @param {object} qs config object for namespacing params, filtering defaults * @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 */ export function replaceNamespacedParams(config, queryString, newParams) { diff --git a/awx/ui_next/src/util/qs.test.js b/awx/ui_next/src/util/qs.test.js index ee37ce05b6..d1806a2272 100644 --- a/awx/ui_next/src/util/qs.test.js +++ b/awx/ui_next/src/util/qs.test.js @@ -570,6 +570,38 @@ describe('qs (qs.js)', () => { 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', () => {