Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add node params works when hashrouting enabled #2490

Merged
merged 11 commits into from
Jan 26, 2022
28 changes: 7 additions & 21 deletions core/src/core-api/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,15 @@ class LuigiRouting {
}
const url = new URL(location);
if (LuigiConfig.getConfigValue('routing.useHashRouting')) {
const [hashValue, givenQueryParamsString] = url.hash.split('?');
const searchParams = new URLSearchParams(givenQueryParamsString);
this._modifySearchParam(params, searchParams);
url.hash = hashValue;
if (searchParams.toString() !== '') {
url.hash += `?${decodeURIComponent(searchParams.toString())}`;
}
url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash);
} else {
this._modifySearchParam(params, url.searchParams);
RoutingHelpers.modifySearchParam(params, url.searchParams);
}

this.handleBrowserHistory(keepBrowserHistory, url.href);
LuigiConfig.configChanged();
}

// Adds and remove properties from searchParams
_modifySearchParam(params, searchParams, paramPrefix) {
for (const [key, value] of Object.entries(params)) {
const paramKey = paramPrefix ? `${paramPrefix}${key}` : key;

searchParams.set(paramKey, value);
if (value === undefined) {
searchParams.delete(paramKey);
}
}
}

addNodeParams(params, keepBrowserHistory) {
if (!GenericHelpers.isObject(params)) {
console.log('Params argument must be an object');
Expand All @@ -86,7 +68,11 @@ class LuigiRouting {

const paramPrefix = RoutingHelpers.getContentViewParamPrefix();
const url = new URL(location);
this._modifySearchParam(params, url.searchParams, paramPrefix);
if (LuigiConfig.getConfigValue('routing.useHashRouting')) {
url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash, paramPrefix);
} else {
RoutingHelpers.modifySearchParam(params, url.searchParams, paramPrefix);
}

this.handleBrowserHistory(keepBrowserHistory, url.href);
LuigiConfig.configChanged();
Expand Down
24 changes: 24 additions & 0 deletions core/src/utilities/helpers/routing-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,30 @@ class RoutingHelpersClass {
};
component.showAlert(alertSettings, false);
}

// Adds and remove properties from searchParams
modifySearchParam(params, searchParams, paramPrefix) {
JohannesDoberer marked this conversation as resolved.
Show resolved Hide resolved
for (const [key, value] of Object.entries(params)) {
const paramKey = paramPrefix ? `${paramPrefix}${key}` : key;

searchParams.set(paramKey, value);
if (value === undefined) {
searchParams.delete(paramKey);//TODO change to key
JohannesDoberer marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

addParamsOnHashRouting(params, hash, paramPrefix){
let localhash = hash;
const [hashValue, givenQueryParamsString] = localhash.split('?');
const searchParams = new URLSearchParams(givenQueryParamsString);
this.modifySearchParam(params, searchParams, paramPrefix);
localhash = hashValue;
if (searchParams.toString() !== '') {
localhash += `?${decodeURIComponent(searchParams.toString())}`;
}
return localhash;
}
}

export const RoutingHelpers = new RoutingHelpersClass();
9 changes: 5 additions & 4 deletions core/test/core-api/routing.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach } from 'mocha';
import { LuigiRouting, LuigiConfig } from '../../src/core-api';
import { RoutingHelpers } from '../../src/utilities/helpers';

const chai = require('chai');
const assert = chai.assert;
Expand Down Expand Up @@ -146,14 +147,14 @@ describe('Luigi routing', function() {
afterEach(() => {
sinon.restore();
});
it('_modifySearchParam', () => {
it('modifySearchParam', () => {
const searchParams = new URLSearchParams('mario=rocks');
LuigiRouting._modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams);
RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams);
assert.equal(searchParams.toString(), 'test=tets&luigi=rocks');
});
it('_modifySearchParam with paramPrefix', () => {
it('modifySearchParam with paramPrefix', () => {
const searchParams = new URLSearchParams('~mario=rocks');
LuigiRouting._modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~');
RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~');
assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks');
});
});
Expand Down
21 changes: 21 additions & 0 deletions core/test/utilities/helpers/routing-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,25 @@ describe('Routing-helpers', () => {
assert.equal(actual, expected);
});
});
describe('modifySearchParam', () => {
beforeEach(() => {
sinon
.stub(LuigiConfig, 'getConfigValue')
.withArgs('routing.useHashRouting')
.returns(false);
});
afterEach(() => {
sinon.restore();
});
it('modifySearchParam', () => {
const searchParams = new URLSearchParams('mario=rocks');
RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams);
assert.equal(searchParams.toString(), 'test=tets&luigi=rocks');
});
it('modifySearchParam with paramPrefix', () => {
const searchParams = new URLSearchParams('~mario=rocks');
RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~');
assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks');
});
});
});