-
Notifications
You must be signed in to change notification settings - Fork 0
/
wizard.js
48 lines (41 loc) · 1.2 KB
/
wizard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const utils = require('./lib/utils.js')
/**
* Get next, back and current paths in user journey.
* @param {object} journey - Sequence of paths in user journey
* @param {object} req - Express request
* @returns {object} Next and back paths
*/
const wizard = (journey, req) => {
const { baseUrl, method, originalUrl, path, session } = req
const { data } = session
const paths = Object.keys(journey)
const index = paths.indexOf(path)
let fork
let next
let back
if (index !== -1) {
fork = utils.getFork(journey[path], req)
next = fork || paths[index + 1] || ''
back = paths[index - 1] || ''
}
// Point back to where we forked from
if (path === data['forked-to']) {
back = data['forked-from']
}
// Remove the saved fork if we return to it
if (path === data['forked-from'] && method === 'GET') {
delete data['forked-from']
delete data['forked-to']
}
// Add a new fork
if (fork && method === 'POST') {
data['forked-from'] = path
data['forked-to'] = fork
}
return {
next: utils.getPathWithSearchParams(originalUrl, next, baseUrl),
back: utils.getPathWithSearchParams(originalUrl, back, baseUrl),
current: originalUrl
}
}
module.exports = wizard