From b8972d545c36e24ba041dc1faa0b55ebc2e36de1 Mon Sep 17 00:00:00 2001 From: Matthew Taylor Date: Thu, 15 Oct 2015 23:05:57 -0400 Subject: [PATCH] refactor cookieMixinFactory isolate cookie business logic from react mixin logic so that they are more modular. And use the cookie business logic to set translation objects on the window --- server/template.html | 4 +- src/init.js | 78 +++++++++++++++++++++++++++++++ src/lib/jar.js | 32 +++++++++++++ src/mixins/cookieMixinFactory.jsx | 25 ++-------- src/session.js | 21 --------- 5 files changed, 117 insertions(+), 43 deletions(-) create mode 100644 src/init.js create mode 100644 src/lib/jar.js delete mode 100644 src/session.js diff --git a/server/template.html b/server/template.html index 88d2db840..56e1fc6da 100644 --- a/server/template.html +++ b/server/template.html @@ -25,8 +25,8 @@ - - + + diff --git a/src/init.js b/src/init.js new file mode 100644 index 000000000..de9d01ffc --- /dev/null +++ b/src/init.js @@ -0,0 +1,78 @@ +var api = require('./mixins/api.jsx').api; +var jar = require('./lib/jar'); +// var locales = require('../languages.json'); +// var translations = { +// en: require('../en.json') +// }; +// for (var id in locales) { +// try { +// translations[id] = require('../locales/' + id + '.json'); +// } catch +// } +var translations = require('../locales/translations.json'); + +require('custom-event-polyfill'); + +// Session +(function () { + window._session = {}; + + /** + * Binds the object to private session variable and dispatches a global + * "session" event. + * + * @param {object} Session object + * + * @return {void} + */ + window.updateSession = function (session) { + window._session = session; + var sessionEvent = new CustomEvent('session', session); + window.dispatchEvent(sessionEvent); + }; + + /** + * Gets a session object from the local proxy method. Calls "updateSession" + * once session has been returned from the proxy. + * + * @return {void} + */ + window.refreshSession = function () { + api({ + host: '', + uri: '/session/' + }, function (err, body) { + window.updateSession(body); + }); + }; + + // Fetch session + window.refreshSession(); +})(); + +// L10N +(function () { + /** + * Bind locale code from cookie if available. Uses navigator language API as a fallback. + * + * @return {string} + */ + function updateLocale () { + var obj = jar.get('scratchlanguage'); + if (typeof obj === 'undefined') { + obj = window.navigator.userLanguage || window.navigator.language; + } + if (typeof translations[obj] === 'undefined') { + // Fall back on the split + obj = obj.split('-')[0]; + } + if (typeof translations[obj] === 'undefined') { + // Language appears to not be supported – return `null` + obj = null; + } + return obj; + } + + window._locale = updateLocale() || 'en'; + window._translations = translations; +})(); diff --git a/src/lib/jar.js b/src/lib/jar.js new file mode 100644 index 000000000..d4f509780 --- /dev/null +++ b/src/lib/jar.js @@ -0,0 +1,32 @@ +var cookie = require('cookie'); +var xhr = require('xhr'); + +var module = module.exports = {}; + +module.get = function (name, callback) { + // Get cookie by name + var obj = cookie.parse(document.cookie) || {}; + + // Handle optional callback + if (typeof callback === 'function') { + if (typeof obj === 'undefined') return callback('Cookie not found.'); + return callback(null, obj[name]); + } + + return obj[name]; +}; + +module.use = function (name, uri, callback) { + // Attempt to get cookie + module.get(name, function (err, obj) { + if (typeof obj !== 'undefined') return callback(null, obj); + + // Make XHR request to cookie setter uri + xhr({ + uri: uri + }, function (err) { + if (err) return callback(err); + module.get(name, callback); + }); + }); +}; diff --git a/src/mixins/cookieMixinFactory.jsx b/src/mixins/cookieMixinFactory.jsx index b08ab4320..ff3dfa2fd 100644 --- a/src/mixins/cookieMixinFactory.jsx +++ b/src/mixins/cookieMixinFactory.jsx @@ -1,31 +1,16 @@ -var cookie = require('cookie'); -var xhr = require('xhr'); - +var jar = require('../lib/jar'); var cookieMixinFactory = function (cookieName, cookieSetter) { var capitalizedCookieName = cookieName.charAt(0).toUpperCase() + cookieName.slice(1); var getterName = "get" + capitalizedCookieName; var userName = "use" + capitalizedCookieName; - var mixin = {} + var mixin = {}; mixin[getterName] = function (callback) { - var obj = cookie.parse(document.cookie) || {}; - if (typeof obj[cookieName] === 'undefined') return callback('Cookie not found.'); - callback(null, obj[cookieName]); + jar.get(cookieName, callback); }; mixin[userName] = function (callback) { - this[getterName](function (err, cookieValue) { - if (cookieValue) return callback(null, cookieValue); - xhr({ - 'uri': cookieSetter - }, function (err) { - if (err) return callback(err); - this[getterName](function (err, cookieValue) { - if (err) return callback(err); - callback(err, cookieValue); - }); - }.bind(this)); - }.bind(this)); - } + jar.use(cookieName, cookieSetter, callback); + }; return mixin; }; diff --git a/src/session.js b/src/session.js deleted file mode 100644 index a28efb679..000000000 --- a/src/session.js +++ /dev/null @@ -1,21 +0,0 @@ -var api = require('./mixins/api.jsx').api; -require('custom-event-polyfill'); - -window._session = {}; - -window.updateSession = function (session) { - window._session = session; - var sessionEvent = new CustomEvent('session', session); - window.dispatchEvent(sessionEvent); -}; - -window.refreshSession = function () { - api({ - host: '', - uri: '/session/' - }, function (err, body) { - window.updateSession(body); - }); -}; - -window.refreshSession();