Skip to content

Instantly share code, notes, and snippets.

@dviedma
Created April 26, 2021 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dviedma/6c2b3e89751c6c30809e506968036171 to your computer and use it in GitHub Desktop.
Save dviedma/6c2b3e89751c6c30809e506968036171 to your computer and use it in GitHub Desktop.
NIUA.prototype.getDetails = function () {
var that = this;
var promise = new Promise(function (resolve, reject) {
var data = {};
if (that.getCookie("profile_id") != null) {
if (sessionStorage.getItem("NIUA_details")) {
var returnedValue = JSON.parse(sessionStorage.getItem("NIUA_details"));
var expiryTime = new Date().getTime() - (that.appConfig.minutesBeforeExpire * 60 * 1000);
if (parseInt(returnedValue.timestamp) < expiryTime) {
/* Data is cached but it has already expired - reload it and re-cache it */
data = JSON.parse(sessionStorage.getItem("NIUA_details"));
that.getResponse("details").then(function (result) {
data = {
response: result,
timestamp: new Date().getTime()
};
sessionStorage.setItem("NIUA_details", JSON.stringify(data));
})['catch'](function (error) {
data = {
response: {
success: false
},
timestamp: new Date().getTime()
};
console.log(error);
})
resolve(data);
} else {
/* Data is cached and it has not yet expired - just return it */
data = JSON.parse(sessionStorage.getItem("NIUA_details"));
resolve(data);
}
} else {
/* There is no cached information - retrieve data and cache it */
that.getResponse("details").then(function (result) {
data = {
response: result,
timestamp: new Date().getTime()
};
sessionStorage.setItem("NIUA_details", JSON.stringify(data));
resolve(data);
})['catch'](function (error) {
data = {
response: {
success: false
},
timestamp: new Date().getTime()
};
console.log(error);
resolve(data);
})
}
} else {
/* profile_id is empty - user is not logged in - return success: false */
data = {
response: {
success: false
},
timestamp: new Date().getTime()
};
sessionStorage.removeItem("NIUA_details");
resolve(data);
}
});
return promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment