-
Notifications
You must be signed in to change notification settings - Fork 21
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
Feature/replace injector pattern with modules #85
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,25 @@ | ||
import { PropertyNames, VariableNames } from '../data/enum/configNames'; | ||
import { getValue } from '../util/injector'; | ||
import { CONFIG_MANAGER } from '../data/Injectables'; | ||
import configManager from '../util/configManager'; | ||
|
||
const getLocaleConfig = () => { | ||
const configManager = getValue(CONFIG_MANAGER); | ||
const languages = configManager.getProperty(PropertyNames.AVAILABLE_LOCALES).map(locale => { | ||
if (typeof locale === 'string') { | ||
return { | ||
code: locale, | ||
urlPrefix: locale, | ||
translationKey: locale, | ||
}; | ||
} | ||
return locale; | ||
}); | ||
|
||
const languages = configManager.getProperty(PropertyNames.AVAILABLE_LOCALES).map(locale => { | ||
if (typeof locale === 'string') { | ||
return { | ||
code: locale, | ||
urlPrefix: locale, | ||
translationKey: locale, | ||
}; | ||
} | ||
return locale; | ||
}); | ||
|
||
const config = { | ||
persistent: false, | ||
defaultCode: configManager.getProperty(PropertyNames.DEFAULT_LOCALE), | ||
languages, | ||
}; | ||
|
||
return { | ||
localeEnabled: configManager.getVariable(VariableNames.LOCALE_ENABLED), | ||
localeRoutingEnabled: configManager.getVariable(VariableNames.LOCALE_ROUTING_ENABLED), | ||
config, | ||
}; | ||
const config = { | ||
languages, | ||
persistent: false, | ||
defaultCode: configManager.getProperty(PropertyNames.DEFAULT_LOCALE), | ||
}; | ||
|
||
export default getLocaleConfig; | ||
export default { | ||
config, | ||
localeEnabled: configManager.getVariable(VariableNames.LOCALE_ENABLED), | ||
localeRoutingEnabled: configManager.getVariable(VariableNames.LOCALE_ROUTING_ENABLED), | ||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
const Params = { | ||
export default { | ||
ID: 'id', | ||
SLUG: 'slug', | ||
}; | ||
|
||
export default Params; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,50 @@ | ||
import VueRouter from 'vue-router'; | ||
import Vue from 'vue'; | ||
import VueRouter from 'vue-router'; | ||
import { routeParser } from 'vue-i18n-manager'; | ||
import localeConfig from '../config/localeConfig'; | ||
import configManager from '../util/configManager'; | ||
import { PropertyNames, VariableNames } from '../data/enum/configNames'; | ||
import getLocaleConfig from '../config/localeConfig'; | ||
import { CONFIG_MANAGER } from '../data/Injectables'; | ||
import { getValue } from '../util/injector'; | ||
|
||
import routes from './routes'; | ||
|
||
Vue.use(VueRouter); | ||
|
||
let router = null; | ||
|
||
const getRouter = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you aware that you are changing the behaviour of the running code with this change(s)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that the behaviour is changed but not how it affects the implementation, do you have an example of lost control? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's say the router setup needs a value that is set in another file. You would like to control the order of how things are executed. Having to rely on changing the import order to (hopefully) change the order of execution is error-prone and unclear. |
||
if (!router) { | ||
const localeConfig = getLocaleConfig(); | ||
const configManager = getValue(CONFIG_MANAGER); | ||
|
||
const processedRoutes = | ||
localeConfig.localeEnabled && localeConfig.localeRoutingEnabled | ||
? routeParser(routes, configManager.getProperty(PropertyNames.DEFAULT_LOCALE)) | ||
: routes.concat({ | ||
path: '*', | ||
redirect: '/', | ||
}); | ||
|
||
router = new VueRouter({ | ||
mode: 'history', | ||
routes: processedRoutes, | ||
base: configManager.getVariable(VariableNames.PUBLIC_PATH), | ||
}); | ||
|
||
router.beforeEach((to, from, next) => { | ||
const persistQueryParams = configManager.getProperty(PropertyNames.PERSIST_QUERY_PARAMS); | ||
|
||
let redirect = false; | ||
const { ...query } = to.query; | ||
|
||
if (persistQueryParams && persistQueryParams.length > 0) { | ||
persistQueryParams.forEach(queryParam => { | ||
if ( | ||
typeof from.query[queryParam] !== 'undefined' && | ||
typeof query[queryParam] === 'undefined' | ||
) { | ||
query[queryParam] = from.query[queryParam]; | ||
|
||
redirect = true; | ||
} | ||
}); | ||
} | ||
|
||
if (redirect) { | ||
next({ path: to.path, query }); | ||
} else { | ||
next(); | ||
const processedRoutes = | ||
localeConfig.localeEnabled && localeConfig.localeRoutingEnabled | ||
? routeParser(routes, configManager.getProperty(PropertyNames.DEFAULT_LOCALE)) | ||
: routes.concat({ | ||
path: '*', | ||
redirect: '/', | ||
}); | ||
|
||
const router = new VueRouter({ | ||
mode: 'history', | ||
routes: processedRoutes, | ||
base: configManager.getVariable(VariableNames.PUBLIC_PATH), | ||
}); | ||
|
||
router.beforeEach((to, from, next) => { | ||
const persistQueryParams = configManager.getProperty(PropertyNames.PERSIST_QUERY_PARAMS); | ||
|
||
let redirect = false; | ||
const { ...query } = to.query; | ||
|
||
if (persistQueryParams && persistQueryParams.length > 0) { | ||
persistQueryParams.forEach(queryParam => { | ||
if ( | ||
typeof from.query[queryParam] !== 'undefined' && | ||
typeof query[queryParam] === 'undefined' | ||
) { | ||
query[queryParam] = from.query[queryParam]; | ||
redirect = true; | ||
} | ||
}); | ||
} | ||
|
||
return router; | ||
}; | ||
if (redirect) { | ||
next({ path: to.path, query }); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
|
||
export default getRouter; | ||
export default router; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,10 @@ | ||
import Vuex from 'vuex'; | ||
import Vue from 'vue'; | ||
import Vuex from 'vuex'; | ||
import modules from './modules'; | ||
|
||
Vue.use(Vuex); | ||
|
||
let store = null; | ||
|
||
const getStore = () => { | ||
if (!store) { | ||
store = new Vuex.Store({ | ||
modules, | ||
strict: process.env.NODE_ENV !== 'production', | ||
}); | ||
} | ||
|
||
return store; | ||
}; | ||
|
||
export default getStore; | ||
export default new Vuex.Store({ | ||
modules, | ||
strict: process.env.NODE_ENV !== 'production', | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import ConfigManager from 'seng-config'; | ||
import config from '../config/config'; | ||
|
||
const configManager = new ConfigManager(); | ||
configManager.init(config.config, config.environment); | ||
|
||
export default configManager; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this file located in the util folder and not in the config folder? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair point. It used to be created and registered in util/setupInjects.js, I’ve separated the ‘injectables’ into separate files but felt relocating/refactoring should be another issue. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import axios from 'axios'; | ||
import { URLNames } from '../data/enum/configNames'; | ||
import configManager from './configManager'; | ||
|
||
const gateway = axios.create({ | ||
baseURL: configManager.getURL(URLNames.API), | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
responseType: 'json', | ||
}); | ||
|
||
gateway.interceptors.response.use( | ||
response => | ||
response && response.data && typeof response.data.data !== 'undefined' | ||
? { ...response, ...response.data } | ||
: response, | ||
error => { | ||
throw error; | ||
}, | ||
); | ||
|
||
export default gateway; |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had all enums like this in vue skeleton but after a comment of Narie we changed it to the const with the seperate export default. It had something to do with webstorm. I am not a big fan of a direct export default. I almost always use the const with the separate export default. So when you read the code you understand what is exported. In this case you have to look at the filename but doesn't always work because you can have multiple exports in one file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, webstorm has/d better autocomplete / autoimport if the const has the same name as the file/import name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point on the autocompletion, I did not consider this.