diff --git a/app/ayanova/src/api/locale.js b/app/ayanova/src/api/locale.js index 7f4c13ce..a08f286c 100644 --- a/app/ayanova/src/api/locale.js +++ b/app/ayanova/src/api/locale.js @@ -22,17 +22,65 @@ Methods */ -const lt = {}; +var lt = {}; export default { Get(key) { - debugger; + console.log("locale::Get -> " + key); if (!_.has(lt, key)) { + console.log("locale::Get Key not found"); return "?" + key + "?"; } + console.log("locale::Get Key was found"); return lt[key]; }, - async Fetch(keys) { + SuperFetch(keys) { + return new Promise(function(resolve, reject) { + console.log("locale::fetch -> " + keys); + //step 1: build an array of keys that we don't have already + var NeedIt = []; + for (var i = 0; i < keys.length; i++) { + if (!_.has(lt, keys[i])) { + NeedIt.push(keys[i]); + } + } + fetch(config.apiUrl + "locale/subset", { + method: "post", + mode: "cors", + headers: { + Accept: "application/json, text/plain, */*", + "Content-Type": "application/json", + Authorization: "Bearer " + getToken() + }, + body: JSON.stringify(NeedIt) + }) + .then(api.status) + .then(api.json) + .then(response => { + _.forEach(response.data, function(item) { + lt[item.key] = item.value; + }); + console.log( + "locale::fetch, done and returning from fetch api call and processing " + ); + resolve(); + }) //succeeded, nothing to return + .catch(function(error) { + reject(error); + }); + + // var z = keys.length; + // if (isMomHappy) { + + // resolve(true); // fulfilled + // } else { + // var reason = new Error("mom is not happy"); + // reject(reason); // reject + // } + }); + }, + Fetch(keys) { + console.log("locale::fetch -> " + keys); //step 1: build an array of keys that we don't have already var NeedIt = []; for (var i = 0; i < keys.length; i++) { @@ -56,12 +104,16 @@ export default { _.forEach(response.data, function(item) { lt[item.key] = item.value; }); - debugger; - return Promise.resolve(true); + console.log( + "locale::fetch, done and returning from fetch api call and processing " + ); + return null; }) //succeeded, nothing to return .catch(function(error) { - return Promise.reject(error); + return error; }); }, - ClearCache() {} + ClearCache() { + lt = {}; + } }; diff --git a/app/ayanova/src/utils/initialize.js b/app/ayanova/src/utils/initialize.js index 93a25504..32d5b948 100644 --- a/app/ayanova/src/utils/initialize.js +++ b/app/ayanova/src/utils/initialize.js @@ -15,13 +15,16 @@ function addNavItem(title, icon, route) { // Initialize the app // on change of authentication status export default function initialize() { + console.log("initialize called"); //clear the nav items either way store.state.navItems = []; //clear the locale text cache lt.ClearCache(); if (store.state.authenticated) { + console.log("initialize about to call fetch"); //fetch the required localized text keys into the cache - lt.Fetch([ + lt.SuperFetch([ + "Home", "Service", "Dispatch", "Inventory", @@ -31,8 +34,10 @@ export default function initialize() { "HelpAboutAyaNova", "Logout" ]) - .then(() => { - debugger; + .then(function() { + console.log( + "initialize back from fetch call, about to use the keys supposedly fetched now" + ); //put nav items into store //Everyone has a home addNavItem(lt.Get("Home"), "home", "/"); @@ -87,7 +92,9 @@ export default function initialize() { addNavItem(lt.Get("Logout"), "sign-out-alt", "/login"); }) .catch(function(error) { - alert("initialize::LT->Prefetch failed: " + error); + // oops, mom don't buy it + console.log(error.message); + // output: 'mom is not happy' }); } }