Created
April 26, 2021 20:35
-
-
Save dviedma/6c2b3e89751c6c30809e506968036171 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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