-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from ONSdigital/ear-1699-list-collector-driiv…
…ing-component Ear 1699 list collector driiving component
- Loading branch information
Showing
4 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/eq_schema/block-types/listCollector/drivingQuestion.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const convertPipes = require("../../../utils/convertPipes"); | ||
const { getInnerHTMLWithPiping } = require("../../../utils/HTMLUtils"); | ||
const { flow } = require("lodash/fp"); | ||
const { flatMap, find, findIndex } = require("lodash"); | ||
|
||
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping); | ||
|
||
const getAllPageIds = (questionnaire) => | ||
flatMap(questionnaire.sections, (section) => | ||
flatMap(section.folders, (folder) => | ||
flatMap(folder.pages, (page) => ({ sectionId: section.id, pageId: page.id })) | ||
) | ||
); | ||
|
||
const getNextBlockId = (page, ctx) => { | ||
let blockId | ||
const pageIds = getAllPageIds(ctx.questionnaireJson) | ||
const sectionId = find(pageIds, { pageId: page.id }).sectionId | ||
const pageIndex = findIndex(pageIds, { pageId: page.id }) | ||
if (pageIds[pageIndex + 1] && | ||
(pageIds[pageIndex + 1]).sectionId === sectionId) { | ||
blockId = pageIds[pageIndex + 1].pageId | ||
} | ||
|
||
return blockId | ||
} | ||
|
||
class DrivingQuestion { | ||
constructor(page, ctx) { | ||
this.id = `question-driving-${page.id}` | ||
this.type = "General" | ||
this.title = processPipe(ctx)(page.drivingQuestion) | ||
this.answers = [{ | ||
"id": `answer-driving-${page.id}`, | ||
"mandatory": true, | ||
"type": "Radio", | ||
"options": [ | ||
{ | ||
"label": page.drivingPositive, | ||
"value": page.drivingPositive, | ||
"action": { | ||
"type": "RedirectToListAddBlock", | ||
"params": { | ||
"block_id": `add-block-${page.id}`, | ||
"list_name": page.listId | ||
} | ||
} | ||
}, | ||
{ | ||
"label": page.drivingNegative, | ||
"value": page.drivingNegative | ||
} | ||
] | ||
}] | ||
} | ||
|
||
static routingRules(page, ctx) { | ||
const nextBlockId = getNextBlockId(page, ctx) | ||
|
||
let routingDest = {} | ||
|
||
if (nextBlockId) { | ||
routingDest.block = `block${nextBlockId}` | ||
} else { | ||
routingDest.section = "End" | ||
} | ||
routingDest.when = [ | ||
{ | ||
id: `answer-driving-${page.id}`, | ||
condition: "equals", | ||
value: page.drivingNegative | ||
} | ||
] | ||
|
||
return [ | ||
{ | ||
goto: routingDest | ||
}, | ||
{ | ||
goto: { | ||
block: `block${page.id}` | ||
} | ||
} | ||
] | ||
} | ||
|
||
} | ||
|
||
module.exports = DrivingQuestion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters