Make changes to code to improve readability
This commit is contained in:
committed by
Benedikt Rötsch
parent
a110d848c0
commit
cdb0a07ff7
2
app.js
2
app.js
@@ -38,7 +38,7 @@ app.use(async function (request, response, next) {
|
||||
deliveryToken: process.env.CONTENTFUL_DELIVERY_TOKEN,
|
||||
previewToken: process.env.CONTENTFUL_PREVIEW_TOKEN,
|
||||
editorialFeatures: false,
|
||||
// Overwrite default settings using those stored in a cookie
|
||||
// Overwrite default settings using those stored in cookie, if present
|
||||
...request.cookies.theExampleAppSettings
|
||||
}
|
||||
|
||||
|
||||
@@ -3,24 +3,25 @@ const url = require('url')
|
||||
module.exports = (modifier) => {
|
||||
return (request, response, next) => {
|
||||
const baseUrl = url.format({ protocol: request.protocol, host: request.get('host') })
|
||||
const parts = url.parse(request.url).pathname.split('/').filter(Boolean)
|
||||
let items = []
|
||||
const urlComponents = url.parse(request.url).pathname.split('/').filter(Boolean)
|
||||
let breadcrumbs = []
|
||||
|
||||
items.push({ label: 'Home', url: baseUrl })
|
||||
items = items.concat(parts.map((part, i, array) => {
|
||||
breadcrumbs.push({ label: 'Home', url: baseUrl })
|
||||
// Map components of the path to breadcrumbs with resolvable URLs
|
||||
let mappedComponents = urlComponents.map((component, i, array) => {
|
||||
const path = array.slice(0, i + 1).join('/')
|
||||
return {
|
||||
label: part.replace(/-/g, ' '),
|
||||
label: component.replace(/-/g, ' '),
|
||||
url: url.resolve(baseUrl, path),
|
||||
path: path
|
||||
}
|
||||
})
|
||||
)
|
||||
breadcrumbs = breadcrumbs.concat(mappedComponents)
|
||||
if (modifier) {
|
||||
items = items.map(modifier)
|
||||
breadcrumbs = breadcrumbs.map(modifier)
|
||||
}
|
||||
// Make it global
|
||||
request.app.locals.breadcrumb = items
|
||||
request.app.locals.breadcrumb = breadcrumbs
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ module.exports.postSettings = async (request, response, next) => {
|
||||
}
|
||||
}
|
||||
|
||||
// When no errors occurred
|
||||
// If no errors, then cache the new settings in the cookie
|
||||
if (!errorList.length) {
|
||||
// Store new settings
|
||||
updateCookie(response, SETTINGS_NAME, settings)
|
||||
|
||||
Reference in New Issue
Block a user