mirror of
https://github.com/ansible/awx.git
synced 2026-03-20 18:37:39 -02:30
Rewrite updateQueryString to preserve namespaces
* Refactors ActivityStream to use updateQueryString
This commit is contained in:
@@ -22,8 +22,7 @@ import useRequest from '../../util/useRequest';
|
|||||||
import {
|
import {
|
||||||
getQSConfig,
|
getQSConfig,
|
||||||
parseQueryString,
|
parseQueryString,
|
||||||
replaceParams,
|
updateQueryString,
|
||||||
encodeNonDefaultQueryString,
|
|
||||||
} from '../../util/qs';
|
} from '../../util/qs';
|
||||||
import { ActivityStreamAPI } from '../../api';
|
import { ActivityStreamAPI } from '../../api';
|
||||||
|
|
||||||
@@ -96,16 +95,14 @@ function ActivityStream() {
|
|||||||
}, [fetchActivityStream]);
|
}, [fetchActivityStream]);
|
||||||
|
|
||||||
const pushHistoryState = urlParamsToAdd => {
|
const pushHistoryState = urlParamsToAdd => {
|
||||||
let searchParams = parseQueryString(QS_CONFIG, location.search);
|
const pageOneQs = updateQueryString(QS_CONFIG, location.search, {
|
||||||
searchParams = replaceParams(searchParams, { page: 1 });
|
page: 1,
|
||||||
const encodedParams = encodeNonDefaultQueryString(QS_CONFIG, searchParams, {
|
});
|
||||||
|
const qs = updateQueryString(null, pageOneQs, {
|
||||||
type: urlParamsToAdd.get('type'),
|
type: urlParamsToAdd.get('type'),
|
||||||
});
|
});
|
||||||
history.push(
|
|
||||||
encodedParams
|
history.push(qs ? `${location.pathname}?${qs}` : location.pathname);
|
||||||
? `${location.pathname}?${encodedParams}`
|
|
||||||
: location.pathname
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -266,12 +266,33 @@ export function replaceParams(oldParams, newParams) {
|
|||||||
* @return {string} url query string
|
* @return {string} url query string
|
||||||
*/
|
*/
|
||||||
export function updateQueryString(config, queryString, newParams) {
|
export function updateQueryString(config, queryString, newParams) {
|
||||||
const oldParams = parseQueryString(config, queryString);
|
const allParams = parseFullQueryString(queryString);
|
||||||
const updatedParams = replaceParams(oldParams, newParams);
|
const { namespace = null, defaultParams = {} } = config || {};
|
||||||
const nonNamespacedParams = parseQueryString({}, queryString);
|
Object.keys(newParams).forEach(key => {
|
||||||
return encodeNonDefaultQueryString(
|
const val = newParams[key];
|
||||||
config,
|
const fullKey = namespace ? `${namespace}.${key}` : key;
|
||||||
updatedParams,
|
if (val === null || val === defaultParams[key]) {
|
||||||
nonNamespacedParams
|
delete allParams[fullKey];
|
||||||
);
|
} else {
|
||||||
|
allParams[fullKey] = newParams[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return encodeQueryString(allParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseFullQueryString(queryString) {
|
||||||
|
const allParams = {};
|
||||||
|
queryString
|
||||||
|
.replace(/^\?/, '')
|
||||||
|
.split('&')
|
||||||
|
.map(s => s.split('='))
|
||||||
|
.forEach(([rawKey, rawValue]) => {
|
||||||
|
if (!rawKey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const key = decodeURIComponent(rawKey);
|
||||||
|
const value = decodeURIComponent(rawValue);
|
||||||
|
allParams[key] = mergeParam(allParams[key], value);
|
||||||
|
});
|
||||||
|
return allParams;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -858,6 +858,15 @@ describe('qs (qs.js)', () => {
|
|||||||
integerFields: ['page'],
|
integerFields: ['page'],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
test('should add param to empty query string', () => {
|
||||||
|
const newParams = {
|
||||||
|
page: 3,
|
||||||
|
};
|
||||||
|
expect(updateQueryString(config, '', newParams)).toEqual(
|
||||||
|
'template.page=3'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
test('should update namespaced param', () => {
|
test('should update namespaced param', () => {
|
||||||
const query = 'template.name__icontains=workflow&template.page=2';
|
const query = 'template.name__icontains=workflow&template.page=2';
|
||||||
const newParams = {
|
const newParams = {
|
||||||
@@ -910,16 +919,25 @@ describe('qs (qs.js)', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// This fix needed after we're confident refactoring components
|
test('should update non-namespaced param', () => {
|
||||||
// to use updateQueryString provides equivalent functionality
|
const query =
|
||||||
test.skip('should not alter params of other namespaces', () => {
|
'activity_stream.name__icontains=workflow&activity_stream.page=2';
|
||||||
|
const newParams = {
|
||||||
|
type: 'job',
|
||||||
|
};
|
||||||
|
expect(updateQueryString(null, query, newParams)).toEqual(
|
||||||
|
'activity_stream.name__icontains=workflow&activity_stream.page=2&type=job'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not alter params of other namespaces', () => {
|
||||||
const query =
|
const query =
|
||||||
'template.name__icontains=workflow&template.page=2&credential.page=3';
|
'template.name__icontains=workflow&template.page=2&credential.page=3';
|
||||||
const newParams = {
|
const newParams = {
|
||||||
page: 3,
|
page: 3,
|
||||||
};
|
};
|
||||||
expect(updateQueryString(config, query, newParams)).toEqual(
|
expect(updateQueryString(config, query, newParams)).toEqual(
|
||||||
'template.name__icontains=workflow&template.page=3&credential.page=3'
|
'credential.page=3&template.name__icontains=workflow&template.page=3'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user