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

Add default session data #501

Merged
merged 3 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
24 changes: 24 additions & 0 deletions app/data/session-data-defaults.js
Original file line number Diff line number Diff line change
@@ -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

}
12 changes: 12 additions & 0 deletions docs/views/examples/pass-data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ <h3 class="heading-small">
<div class="code">
&lt;input type="radio" name="over-18" value="yes" {%raw%}{{ checked('over-18','yes') }}{%endraw%}&gt;

</div>
</pre>

<h3 class="heading-small">
Setting default data
</h3>

<p>You can set default values in this file in your prototype folder:
<pre>
<div class="code">
app/data/session-data-defaults.js

</div>
</pre>

Expand Down
24 changes: 24 additions & 0 deletions lib/template.session-data-defaults.js
Original file line number Diff line number Diff line change
@@ -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

}
13 changes: 13 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down