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

fixed blank toc bug when only arcgis layers are loaded #230

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions src/helpers/esriHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const login = (callback) => {
});
};

export function processToken(esriLogin) {
export function processToken(esriLogin, callback = undefined) {
let configSecured = {};
helpers.waitForLoad("settings", Date.now(), 30, () => {
configSecured = window.config.configSecured;
Expand All @@ -88,55 +88,59 @@ export function processToken(esriLogin) {
if (!esriLogin["IssueDate"]) esriLogin["IssueDate"] = currentTime;
if (!esriLogin["RenewalDate"]) esriLogin["RenewalDate"] = currentTime + parseInt(esriLogin.expires_in) - configSecured.securedArcGIS.maxActiveTime; //maxActiveTime = 43200000 = 12 hours in ms
if (esriLogin["access_token"]) sessionStorage.removeItem("esriJSAPIOAuth");
helpers.saveToStorage("ArcGIS_Token", esriLogin);
helpers.saveToStorage("ArcGIS_Token", esriLogin, { allowSaveToServer: false });
helpers.addIsLoaded("esriLogin");
if (callback) callback(esriLogin);
}

export async function getAccessToken(callback) {
let configSecured = {};
helpers.waitForLoad("settings", Date.now(), 30, () => {
configSecured = window.config.configSecured;
});
const forceAppRefresh = (tokenInfo) => {
const refreshDelay = 5000;

const forceAppRefresh = (tokenInfo, refreshDelay = 5000, title = "ArcGIS Token expired!", message = "ArcGIS Token expired, refreshing page...", color = helpers.messageColors.red) => {
const reloadTimeout = tokenInfo.RenewalDate - Date.now();

setTimeout(() => {
//force refresh of page if left active for longer than the maxActiveTime
helpers.showMessage("ArcGIS Token expired!", "ArcGIS Token expired, refreshing page...", helpers.messageColors.red, refreshDelay, true);
helpers.showMessage(title, message, color, refreshDelay, true);
console.warn("ArcGIS Token expired. Page will automatically refresh.");
setTimeout(() => {
window.location.reload();
}, refreshDelay);
}, reloadTimeout - refreshDelay);
};
let esriLogin = sessionStorage.getItem("esriJSAPIOAuth");
if (esriLogin) processToken(JSON.parse(esriLogin));
esriLogin = helpers.getItemsFromStorage("ArcGIS_Token");
if (esriLogin)
processToken(JSON.parse(esriLogin), (retLogin) => {
esriLogin = retLogin;
});
else esriLogin = helpers.getItemsFromStorage("ArcGIS_Token");

if (esriLogin && esriLogin.access_token && Date.now() < esriLogin.RenewalDate) {
forceAppRefresh(esriLogin);
if (callback) callback(esriLogin.access_token);
else return esriLogin.access_token;
} else {
login((esriLogin) => {
processToken({
expires_in: esriLogin["expires"] - Date.now(),
IssueDate: esriLogin["creationTime"],
ExpiryDate: esriLogin["expires"],
RenewalDate: parseInt(esriLogin["expires"]) - configSecured.securedArcGIS.maxActiveTime, //maxActiveTime = 43200000 = 12 hours in ms
username: esriLogin["userId"],
ssl: esriLogin["ssl"],
access_token: esriLogin["token"],
state: { portalUrl: esriLogin["server"] },
});

//autoLogin();
helpers.waitForLoad(["esriLogin"], Date.now(), 60, () => {
const esriLogin = helpers.getItemsFromStorage("ArcGIS_Token");
forceAppRefresh(esriLogin);
if (esriLogin && Date.now() < esriLogin.RenewalDate) callback(esriLogin.access_token);
});
processToken(
{
expires_in: esriLogin["expires"] - Date.now(),
IssueDate: esriLogin["creationTime"],
ExpiryDate: esriLogin["expires"],
RenewalDate: parseInt(esriLogin["expires"]) - configSecured.securedArcGIS.maxActiveTime, //maxActiveTime = 43200000 = 12 hours in ms
username: esriLogin["userId"],
ssl: esriLogin["ssl"],
access_token: esriLogin["token"],
state: { portalUrl: esriLogin["server"] },
},
(retLogin) => {
esriLogin = retLogin;
if (esriLogin && Date.now() < esriLogin.RenewalDate) callback(esriLogin.access_token);
else forceAppRefresh(esriLogin, 2500, "Acquiring ArcGIS Token", "Acquiring ArcGIS Token, refreshing page...", helpers.messageColors.yellow);
}
);
});
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,15 +871,16 @@ export function stringDivider(str, width, spaceReplacer) {
return str;
}

export function saveToStorage(storageKey, item) {
export function saveToStorage(storageKey, item, options = {}) {
const { allowSaveToServer = true } = options;
try {
if (typeof item === "object") window.localStorage.setItem(storageKey, JSON.stringify(item));
else window.localStorage.setItem(storageKey, JSON.stringify(item));
} catch (e) {
console.log(e);
cleanupStorage();
}
if (window.security.includes("saveToServer")) setUserStorage();
if (window.security.includes("saveToServer") && allowSaveToServer) setUserStorage();
}
export function removeFromStorage(storageKey) {
try {
Expand Down
Loading