Skip to content

Commit

Permalink
Merge pull request #70 from kadena-community/feat/ag/finish-xchain-qu…
Browse files Browse the repository at this point in the history
…erystring-params
  • Loading branch information
alber70g authored Nov 9, 2023
2 parents ab5d6c8 + 297fe6d commit d8ad373
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/xchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ function validateServer() {
console.log(err);
setError(err);
}

setUrlParam('network', event.target.value);
},
false,
);
Expand Down Expand Up @@ -670,6 +672,8 @@ function validatePact() {
console.log(err);
setError(err);
}

setUrlParam('requestKey', event.target.value);
},
false,
);
Expand Down Expand Up @@ -739,6 +743,9 @@ function isAccountEligibleForGasPayment() {
window.addEventListener(
'load',
async function () {
fillNetworkIdFromQueryString();
fillRequestKeyFromQueryString();

State.server = State.server ? State.server : 'api.chainweb.com';
State.networkId = State.networkId ? State.networkId : 'mainnet01';
State.requestKey = State.requestKey ? State.requestKey : '';
Expand Down Expand Up @@ -1033,3 +1040,38 @@ function makeRawRequestInit(stringBody) {
body: stringBody,
};
}

function fillNetworkIdFromQueryString() {
const network = getParameterByName('network');
if (network) {
document.getElementById('server').value = network;
State.server = network;
}
}

function fillRequestKeyFromQueryString() {
const requestKey = getParameterByName('requestKey');
if (requestKey) {
document.getElementById('pact-id').value = requestKey;
State.requestKey = requestKey;
}
}

function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\\$&');
const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`);
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

function setUrlParam(key, value) {
const urlParams = new URLSearchParams(window.location.search);
urlParams.set(key, value);
window.history.replaceState(
{},
'',
`${window.location.pathname}?${urlParams}`,
);
}

0 comments on commit d8ad373

Please sign in to comment.