This commit is contained in:
2018-11-12 23:13:39 +00:00
parent c276d395fb
commit 251cd6dbb6
2 changed files with 70 additions and 11 deletions

View File

@@ -22,17 +22,65 @@ Methods
*/ */
const lt = {}; var lt = {};
export default { export default {
Get(key) { Get(key) {
debugger; console.log("locale::Get -> " + key);
if (!_.has(lt, key)) { if (!_.has(lt, key)) {
console.log("locale::Get Key not found");
return "?" + key + "?"; return "?" + key + "?";
} }
console.log("locale::Get Key was found");
return lt[key]; 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 //step 1: build an array of keys that we don't have already
var NeedIt = []; var NeedIt = [];
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
@@ -56,12 +104,16 @@ export default {
_.forEach(response.data, function(item) { _.forEach(response.data, function(item) {
lt[item.key] = item.value; lt[item.key] = item.value;
}); });
debugger; console.log(
return Promise.resolve(true); "locale::fetch, done and returning from fetch api call and processing "
);
return null;
}) //succeeded, nothing to return }) //succeeded, nothing to return
.catch(function(error) { .catch(function(error) {
return Promise.reject(error); return error;
}); });
}, },
ClearCache() {} ClearCache() {
lt = {};
}
}; };

View File

@@ -15,13 +15,16 @@ function addNavItem(title, icon, route) {
// Initialize the app // Initialize the app
// on change of authentication status // on change of authentication status
export default function initialize() { export default function initialize() {
console.log("initialize called");
//clear the nav items either way //clear the nav items either way
store.state.navItems = []; store.state.navItems = [];
//clear the locale text cache //clear the locale text cache
lt.ClearCache(); lt.ClearCache();
if (store.state.authenticated) { if (store.state.authenticated) {
console.log("initialize about to call fetch");
//fetch the required localized text keys into the cache //fetch the required localized text keys into the cache
lt.Fetch([ lt.SuperFetch([
"Home",
"Service", "Service",
"Dispatch", "Dispatch",
"Inventory", "Inventory",
@@ -31,8 +34,10 @@ export default function initialize() {
"HelpAboutAyaNova", "HelpAboutAyaNova",
"Logout" "Logout"
]) ])
.then(() => { .then(function() {
debugger; console.log(
"initialize back from fetch call, about to use the keys supposedly fetched now"
);
//put nav items into store //put nav items into store
//Everyone has a home //Everyone has a home
addNavItem(lt.Get("Home"), "home", "/"); addNavItem(lt.Get("Home"), "home", "/");
@@ -87,7 +92,9 @@ export default function initialize() {
addNavItem(lt.Get("Logout"), "sign-out-alt", "/login"); addNavItem(lt.Get("Logout"), "sign-out-alt", "/login");
}) })
.catch(function(error) { .catch(function(error) {
alert("initialize::LT->Prefetch failed: " + error); // oops, mom don't buy it
console.log(error.message);
// output: 'mom is not happy'
}); });
} }
} }