From d10ba66221bac3d67cfc6d2a4180a9a1e3eca977 Mon Sep 17 00:00:00 2001 From: Mike Shaw Date: Mon, 8 Jan 2018 16:36:32 +0000 Subject: [PATCH 1/3] Add default session data If provided, the app will try to merge data from a defaults file with data from the session. Defaults will only apply if there isn't a corresponding value already in the session, so data can be overridden within the prototype if necessary. --- app/data/session-data-defaults.js | 24 ++++++++++++++++++++++++ lib/template.session-data-defaults.js | 24 ++++++++++++++++++++++++ lib/utils.js | 13 +++++++++++++ start.js | 15 +++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 app/data/session-data-defaults.js create mode 100644 lib/template.session-data-defaults.js diff --git a/app/data/session-data-defaults.js b/app/data/session-data-defaults.js new file mode 100644 index 0000000000..9e4921b0ab --- /dev/null +++ b/app/data/session-data-defaults.js @@ -0,0 +1,24 @@ +/* + +Provide default values for user session data. These are automatically added +via the `autoStoreData` middleware. Values will only be added to the +session if a value doesn't already exist. This may be useful for testing +journeys where users are returning or logging in to an existing application. + +============================================================================ + +Example usage: + +"full-name": "Sarah Philips", + +"options-chosen": [ "foo", "bar" ] + +============================================================================ + +*/ + +module.exports = { + + // Insert values here + +} diff --git a/lib/template.session-data-defaults.js b/lib/template.session-data-defaults.js new file mode 100644 index 0000000000..9e4921b0ab --- /dev/null +++ b/lib/template.session-data-defaults.js @@ -0,0 +1,24 @@ +/* + +Provide default values for user session data. These are automatically added +via the `autoStoreData` middleware. Values will only be added to the +session if a value doesn't already exist. This may be useful for testing +journeys where users are returning or logging in to an existing application. + +============================================================================ + +Example usage: + +"full-name": "Sarah Philips", + +"options-chosen": [ "foo", "bar" ] + +============================================================================ + +*/ + +module.exports = { + + // Insert values here + +} diff --git a/lib/utils.js b/lib/utils.js index 95fedf6502..f1e00edc06 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -252,12 +252,25 @@ var storeData = function (input, store) { } } +// Get session default data from file +let sessionDataDefaults = {} + +const sessionDataDefaultsFile = path.join(__dirname, '/../app/data/session-data-defaults.js') + +try { + sessionDataDefaults = require(sessionDataDefaultsFile) +} catch (e) { + console.error('Could not load the session data defaults from app/data/session-data-defaults.js. Might be a syntax error?') +} + // Middleware - store any data sent in session, and pass it to all views exports.autoStoreData = function (req, res, next) { if (!req.session.data) { req.session.data = {} } + req.session.data = Object.assign({}, sessionDataDefaults, req.session.data) + storeData(req.body, req.session) storeData(req.query, req.session) diff --git a/start.js b/start.js index c8cc4152d9..315e446fde 100644 --- a/start.js +++ b/start.js @@ -17,6 +17,21 @@ if (!envExists) { .pipe(fs.createWriteStream(path.join(__dirname, '/.env'))) } +// Create template session data defaults file if it doesn't exist +const dataDirectory = path.join(__dirname, '/app/data') +const sessionDataDefaultsFile = path.join(dataDirectory, '/session-data-defaults.js') +const sessionDataDefaultsFileExists = fs.existsSync(sessionDataDefaultsFile) + +if (!sessionDataDefaultsFileExists) { + console.log('Creating session data defaults file') + if (!fs.existsSync(dataDirectory)) { + fs.mkdirSync(dataDirectory) + } + + fs.createReadStream(path.join(__dirname, '/lib/template.session-data-defaults.js')) + .pipe(fs.createWriteStream(sessionDataDefaultsFile)) +} + // Run gulp const spawn = require('cross-spawn') From 148b3477debc96e63f6977a027a76cbe19b37df4 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Tue, 8 May 2018 18:48:43 +0100 Subject: [PATCH 2/3] default session data documentation --- docs/views/examples/pass-data/index.html | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/views/examples/pass-data/index.html b/docs/views/examples/pass-data/index.html index 7541ad28d9..bf976c9ced 100644 --- a/docs/views/examples/pass-data/index.html +++ b/docs/views/examples/pass-data/index.html @@ -91,6 +91,18 @@

<input type="radio" name="over-18" value="yes" {%raw%}{{ checked('over-18','yes') }}{%endraw%}> +
+ + +

+ Setting default data +

+ +

You can set default values in this file in your prototype folder: +

+
+ app/data/session-data-defaults.js +
From ebdf461c2496cd62ef0fd5e749f7857c1cce2bf0 Mon Sep 17 00:00:00 2001 From: Joe Lanman Date: Tue, 8 May 2018 18:49:01 +0100 Subject: [PATCH 3/3] CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb62c97c1a..6ca55cc09f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Unreleased +New features: + +- [#501 Add default session data](https://github.com/alphagov/govuk_prototype_kit/pull/501) + Bug fixes: - [#491 Remove redundant Google Analytics](https://github.com/alphagov/govuk_prototype_kit/pull/491)