JSFiddle - React, Tailwind, and code Playground
by philll_t
Babel + JSX
let QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
}();
let serialize = function(obj, url) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return url + "?" + str.join("&");
}
const sampleIncomingParams = {
"param1" : "Bar",
"param2" : "foo"
}
/////
class redirectPromise {
called = false;
constructor(URL, outgoing, callback){
const goNow = function(url){
this.goNow(url);
}
// Get params:
const qString = this.getParams();
// Check if this is the first time around
if(qString.first){
alert("second time around");
}else{
// prepare for redirect
this.called = true;
outgoing['first'] = true;
let prepared_URL = serialize(outgoing, URL);
callback();
// Redirect now
}
}
then(after){
// Get uri
const carriedParams = this.getParams();
if(this.called){
after(carriedParams);
}
}
// Other actions
failed(){
// Should do something when failed
}
goNow(url){
alert("Going to" + url);
}
getParams () {
const urlParams = sampleIncomingParams;
...