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

Feature/currency sign after #1982

Merged
merged 14 commits into from
Nov 11, 2018
Merged
2 changes: 2 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"availableCacheTags": ["product", "category", "home", "checkout", "page-not-found", "compare", "my-account", "P", "C", "error"],
"invalidateCacheKey": "aeSu7aip",
"dynamicConfigReload": false,
"dynamicConfigContinueOnError": false,
"dynamicConfigExclude": ["ssr", "storeViews", "entities", "localForage", "shipping", "boost", "query"],
"dynamicConfigInclude": []
},
Expand Down Expand Up @@ -267,6 +268,7 @@
"defaultLocale": "en-US",
"currencyCode": "USD",
"currencySign": "$",
CodeSkills marked this conversation as resolved.
Show resolved Hide resolved
"currencySignPlacement": "append",
"dateFormat": "HH:mm D/M/YYYY",
"fullCountryName": "United States",
"fullLanguageName": "English",
Expand Down
21 changes: 18 additions & 3 deletions core/filters/price/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ export function price (value) {
if (isNaN(value)) {
return value
}
const formattedVal = parseFloat(value).toFixed(2)
let formattedVal = parseFloat(value).toFixed(2)
const storeView = currentStoreView()

const prependCurrency = (price) => {
return storeView.i18n.currencySign + price
}

const appendCurrency = (price) => {
return price + storeView.i18n.currencySign
}

if (storeView.i18n.currencySignPlacement === 'append') {
formattedVal = appendCurrency(formattedVal)
} else {
formattedVal = prependCurrency(formattedVal)
}

if (value >= 0) {
return storeView.i18n.currencySign + formattedVal
return formattedVal
} else {
return '-' + storeView.i18n.currencySign + Math.abs(formattedVal)
return '-' + formattedVal
}
}
80 changes: 52 additions & 28 deletions core/scripts/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ app.get('*', (req, res, next) => {
' </html>')
return next()
}
if (config.server.dynamicConfigReload) {
delete require.cache[require.resolve('config')]
config = require('config') // reload config
}
const context = {
url: req.url,
output: {
Expand Down Expand Up @@ -208,35 +204,63 @@ app.get('*', (req, res, next) => {
}).catch(errorHandler)
}

if (config.server.useOutputCache && cache) {
cache.get(
'page:' + req.url
).then(output => {
if (output !== null) {
if (output.headers) {
for (const header of Object.keys(output.headers)) {
res.setHeader(header, output.headers[header])
const dynamicCacheHandler = () => {
if (config.server.useOutputCache && cache) {
cache.get(
'page:' + req.url
).then(output => {
if (output !== null) {
if (output.headers) {
for (const header of Object.keys(output.headers)) {
res.setHeader(header, output.headers[header])
}
}
}
res.setHeader('X-VS-Cache', 'Hit')
if (output.body) {
res.end(output.body)
res.setHeader('X-VS-Cache', 'Hit')
if (output.body) {
res.end(output.body)
} else {
res.setHeader('Content-Type', 'text/html')
res.end(output.body)
}
res.end(output)
console.log(`cache hit [${req.url}], cached request: ${Date.now() - s}ms`)
next()
} else {
res.setHeader('Content-Type', 'text/html')
res.end(output.body)
res.setHeader('X-VS-Cache', 'Miss')
console.log(`cache miss [${req.url}], request: ${Date.now() - s}ms`)
dynamicRequestHandler(renderer) // render response
}
res.end(output)
console.log(`cache hit [${req.url}], cached request: ${Date.now() - s}ms`)
next()
} else {
res.setHeader('Content-Type', 'text/html')
res.setHeader('X-VS-Cache', 'Miss')
console.log(`cache miss [${req.url}], request: ${Date.now() - s}ms`)
dynamicRequestHandler(renderer) // render response
}
}).catch(errorHandler)
}).catch(errorHandler)
} else {
dynamicRequestHandler(renderer)
}
}

if (config.server.dynamicConfigReload) {
delete require.cache[require.resolve('config')]
config = require('config') // reload config
if (typeof serverExtensions.configProvider === 'function') {
serverExtensions.configProvider(req).then(loadedConfig => {
config = Object.assign(config, loadedConfig) // merge loaded conf with build time conf
dynamicCacheHandler()
}).catch(error => {
if (config.server.dynamicConfigContinueOnError) {
dynamicCacheHandler()
} else {
console.log('config provider error:', error)
if (req.url !== '/error') {
res.redirect('/error')
}
dynamicCacheHandler()
}
})
} else {
config = require('config') // reload config
dynamicCacheHandler()
}
} else {
dynamicRequestHandler(renderer)
dynamicCacheHandler()
}
})

Expand Down
23 changes: 22 additions & 1 deletion doc/Config docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,15 @@ The `defaultCountry` and the `defaultRegion` settings are being used for finding
"defaultLocale": "de-DE",
"currencyCode": "EUR",
"currencySign": "EUR",
"currencySignPlacement": "append", // v1.6+
"dateFormat": "HH:mm D-M-YYYY"
}
}
},
```
The internationalization settings are used by the translation engine (`defautlLocale`) and the [Language/Switcher.vue](../src/themes/default/components/core/blocks/Switcher/Language.vue) (`fullCountryName`, `fullLanguageName`). `currencyCode` is used for some of the API calls (rendering prices mostly) and `currencySign` is being used for displaying the prices in the frontend.
The internationalization settings are used by the translation engine (`defautlLocale`) and the [Language/Switcher.vue](../src/themes/default/components/core/blocks/Switcher/Language.vue) (`fullCountryName`, `fullLanguageName`). `currencyCode` is used for some of the API calls (rendering prices mostly) and `currencySign` is being used for displaying the prices in the frontend. From v1.6 you can use `currencySignPlacement` to define the position of currency sign to either "append" or "prepend".



```json
"mailchimp": {
Expand All @@ -472,3 +475,21 @@ You can put Your Google Analytics ID in here as to be used by the analytics exte
}
```
This is the URL endpoint of the Snow.dog Magento2 CMS extensions - need to be set when using the [`src/extensions/cms`](../Psrc/extensions/cms)

### Config Provider
From v1.6 you can use dynamic config provider to, for example, prefetch config from other API.

It is simple as exporting `configProvider` function within `src/server/index.js`
```javascript
module.exports.configProvider = (req) => {
const axios = require('axios')
return new Promise((resolve, reject) => axios.get('myapi.com/config', {
params: {
domain: req.headers.host
}
}).then(res => {
resolve(res.data)
}).catch(error => reject(error)))
}
```
This function receives the request as a first parameter so you can decide how to load the config by yourself. This function needs to return a Promise. The confing is then passed within `resolve(loadedConfig)`.
13 changes: 13 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@
module.exports.registerUserServerRoutes = (expressApp) => {
require('./example/generator')(expressApp)
}

// Use can use dynamic config by using this function below:
// (Needs to return a Promise)
// module.exports.configProvider = (req) => {
// const axios = require('axios')
// return new Promise((resolve, reject) => axios.get('myapi.com/config', {
// params: {
// domain: req.headers.host
// }
// }).then(res => {
// resolve(res.data)
// }).catch(error => reject(error)))
// }
Loading