Dashboard: Fix multi-value variables in share links (#33216) (#33576)

This commit is contained in:
MrSummat 2021-05-28 08:23:23 +02:00 committed by GitHub
parent 53a3e386c6
commit 48fe3feae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -55,3 +55,34 @@ describe('parseKeyValue', () => {
expect(obj).toEqual({ custom: '', custom1: '001', custom2: '002', custom3: true });
});
});
describe('getUrlSearchParams', () => {
const { location } = window;
// @ts-ignore
delete window.location;
window.location = {
...location,
hash: '#hash',
host: 'www.domain.com:9877',
hostname: 'www.domain.com',
href: 'http://www.domain.com:9877/path/b?var1=a&var2=b&var2=c&var2=d&var3=a&var3=d&z#hash',
origin: 'http://www.domain.com:9877',
pathname: '/path/b',
port: '9877',
protocol: 'http:',
search: '?var1=a&var2=b&var2=c&var2=d&var3=a&var3=d&z',
};
let expectedParams = {
var1: ['a'],
var2: ['b', 'c', 'd'],
var3: ['a', 'd'],
z: true,
};
it('should take into account multi-value and boolean parameters', () => {
const params = urlUtil.getUrlSearchParams();
expect(params).toStrictEqual(expectedParams);
});
});

View File

@ -105,17 +105,21 @@ function appendQueryToUrl(url: string, stringToAppend: string) {
/**
* Return search part (as object) of current url
*/
function getUrlSearchParams() {
function getUrlSearchParams(): UrlQueryMap {
const search = window.location.search.substring(1);
const searchParamsSegments = search.split('&');
const params: any = {};
const params: UrlQueryMap = {};
for (const p of searchParamsSegments) {
const keyValuePair = p.split('=');
if (keyValuePair.length > 1) {
// key-value param
const key = decodeURIComponent(keyValuePair[0]);
const value = decodeURIComponent(keyValuePair[1]);
params[key] = value;
if (key in params) {
params[key] = [...(params[key] as any[]), value];
} else {
params[key] = [value];
}
} else if (keyValuePair.length === 1) {
// boolean param
const key = decodeURIComponent(keyValuePair[0]);