Skip to content

Commit

Permalink
Add Express.js locals to Manage prototype Nunjucks context
Browse files Browse the repository at this point in the history
Management pages use locals like `serviceName` which custom Nunjucks environments don’t provide

Here we’re restoring the same `app.locals` that the route’s `res.render()` included by default previously
  • Loading branch information
colinrotherham committed Oct 6, 2023
1 parent 1ff357e commit 1cd2b4f
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions lib/manage-prototype-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,28 @@ function getCsrfTokenHandler (req, res) {

// Clear all data in session
function getClearDataHandler (req, res) {
res.send(nunjucksManagementEnv.render(getManagementView('clear-data.njk')))
const { locals } = req.app
res.send(nunjucksManagementEnv.render(getManagementView('clear-data.njk'), locals))
}

function postClearDataHandler (req, res) {
const { locals } = req.app

req.session.data = {}
res.send(nunjucksManagementEnv.render(getManagementView('clear-data-success.njk')))
res.send(nunjucksManagementEnv.render(getManagementView('clear-data-success.njk'), locals))
}

// Render password page with a returnURL to redirect people to where they came from
function getPasswordHandler (req, res) {
const { locals } = req.app
const returnURL = req.query.returnURL || '/'
const error = req.query.error
res.send(nunjucksManagementEnv.render(getManagementView('password.njk'), { returnURL, error }))

res.send(nunjucksManagementEnv.render(getManagementView('password.njk'), {
...locals,
returnURL,
error
}))
}

// Check authentication password
Expand All @@ -130,10 +139,12 @@ function postPasswordHandler (req, res) {
// Middleware to ensure the routes specified below will render the manage-prototype-not-available
// view when the prototype is not running in development
function developmentOnlyMiddleware (req, res, next) {
const { locals } = req.app

if (config.getConfig().isDevelopment || req.url.startsWith('/dependencies/govuk-frontend')) {
next()
} else {
res.send(nunjucksManagementEnv.render(getManagementView('manage-prototype-not-available.njk')))
res.send(nunjucksManagementEnv.render(getManagementView('manage-prototype-not-available.njk'), locals))
}
}

Expand Down Expand Up @@ -173,14 +184,17 @@ async function readFileIfExists (path) {

async function getHomeHandler (req, res) {
const pageName = 'Home'

const { locals } = req.app
const { serviceName } = config.getConfig()

const originalHomepage = await fse.readFile(path.join(packageDir, 'prototype-starter', 'app', 'views', 'index.html'), 'utf8')
const currentHomepage = await readFileIfExists(path.join(appViewsDir, 'index.html'))

const kitPackage = await lookupPackageInfo('govuk-prototype-kit')

const viewData = {
res.send(nunjucksManagementEnv.render(getManagementView('index.njk'), {
...locals,
currentUrl: req.originalUrl,
currentSection: pageName,
links: managementLinks,
Expand All @@ -195,9 +209,7 @@ async function getHomeHandler (req, res) {
html: 'Edit the homepage in the file <strong>app/views/index.html</strong>'
}
]
}

res.send(nunjucksManagementEnv.render(getManagementView('index.njk'), viewData))
}))
}

function exampleTemplateConfig (packageName, { name, path }) {
Expand Down Expand Up @@ -237,6 +249,8 @@ function getPluginTemplates () {

async function getTemplatesHandler (req, res) {
const pageName = 'Templates'

const { locals } = req.app
const availableTemplates = getPluginTemplates()

const commonTemplatesPackageName = '@govuk-prototype-kit/common-templates'
Expand All @@ -251,6 +265,7 @@ async function getTemplatesHandler (req, res) {
}

res.send(nunjucksManagementEnv.render(getManagementView('templates.njk'), {
...locals,
currentSection: pageName,
links: managementLinks,
availableTemplates,
Expand Down Expand Up @@ -299,10 +314,12 @@ function getTemplatesViewHandler (req, res) {
}

function getTemplatesInstallHandler (req, res) {
const { locals } = req.app
const templateConfig = locateTemplateConfig(req)

if (templateConfig) {
res.send(nunjucksManagementEnv.render(getManagementView('template-install.njk'), {
...locals,
currentSection: 'Templates',
pageName: `Create new ${templateConfig.name}`,
currentUrl: req.originalUrl,
Expand Down Expand Up @@ -389,9 +406,12 @@ async function postTemplatesInstallHandler (req, res) {

function getTemplatesPostInstallHandler (req, res) {
const pageName = 'Page created'

const { locals } = req.app
const chosenUrl = req.query['chosen-url']

res.send(nunjucksManagementEnv.render(getManagementView('template-post-install.njk'), {
...locals,
currentSection: 'Templates',
pageName,
links: managementLinks,
Expand Down Expand Up @@ -514,12 +534,15 @@ const verbs = {

async function getPluginsHandler (req, res) {
const isInstalledPage = req.route.path.endsWith('installed')
const { locals } = req.app
const { search = '' } = req.query || {}
const pageName = 'Plugins'
const { plugins, status, updates = 0, found = 0 } = await prepareForPluginPage(isInstalledPage, search)
const foundMessage = found === 1 ? found + ' Plugin found' : found + ' Plugins found'
const updatesMessage = updates ? updates === 1 ? updates + ' UPDATE AVAILABLE' : updates + ' UPDATES AVAILABLE' : ''
const model = {

res.send(nunjucksManagementEnv.render(getManagementView('plugins.njk'), {
...locals,
currentSection: pageName,
links: managementLinks,
isInstalledPage,
Expand All @@ -529,8 +552,7 @@ async function getPluginsHandler (req, res) {
updatesMessage,
foundMessage,
status
}
res.send(nunjucksManagementEnv.render(getManagementView('plugins.njk'), model))
}))
}

async function postPluginsHandler (req, res) {
Expand Down Expand Up @@ -591,6 +613,7 @@ function modeIsComplete (mode, { installedVersion, latestVersion, version, insta
async function getPluginsModeHandler (req, res) {
const isSameOrigin = req.headers['sec-fetch-site'] === 'same-origin'
const mode = getModeFromRequest(req)
const { locals } = req.app
const { version } = req.query
const verb = verbs[mode]

Expand Down Expand Up @@ -626,6 +649,7 @@ async function getPluginsModeHandler (req, res) {
}

res.send(nunjucksManagementEnv.render(getManagementView('plugin-install-or-uninstall.njk'), {
...locals,
currentSection: 'Plugins',
pageName,
currentUrl: req.originalUrl,
Expand Down

0 comments on commit 1cd2b4f

Please sign in to comment.