Skip to content

Commit

Permalink
Merge pull request #101 from ONSdigital/ear-1699-list-collector-driiv…
Browse files Browse the repository at this point in the history
…ing-component

Ear 1699 list collector driiving component
  • Loading branch information
jasehumphr authored Jul 4, 2022
2 parents 7585daf + fc66309 commit 16c0065
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
89 changes: 89 additions & 0 deletions src/eq_schema/block-types/listCollector/drivingQuestion.js
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;
4 changes: 3 additions & 1 deletion src/eq_schema/block-types/listCollector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const AddBlock = require("./addBlock")
const EditBlock = require("./editBlock")
const RemoveBlock = require("./removeBlock")
const SummaryBlock = require("./summaryBlock")
const DrivingQuestion = require("./drivingQuestion")

module.exports = {
ListCollectorQuestion,
AddBlock,
EditBlock,
RemoveBlock,
SummaryBlock
SummaryBlock,
DrivingQuestion
}
12 changes: 10 additions & 2 deletions src/eq_schema/schema/Block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ const {
AddBlock,
EditBlock,
RemoveBlock,
SummaryBlock
SummaryBlock,
DrivingQuestion,
} = require("../../block-types/listCollector")

const pageTypeMappings = {
QuestionPage: "Question",
InterstitialPage: "Interstitial",
ListCollectorPage: "ListCollector"
ListCollectorPage: "ListCollector",
DrivingQuestionPage: "ListCollectorDrivingQuestion"
};

const getLastPage = flow(getOr([], "pages"), last);
Expand Down Expand Up @@ -101,6 +103,12 @@ class Block {
this.remove_block = new RemoveBlock(page)
this.summary = new SummaryBlock(page, ctx)
}
if (page.pageType === "DrivingQuestionPage") {
this.id = `block-driving${page.id}`;
this.for_list = page.listId
this.question = new DrivingQuestion(page, ctx)
this.routing_rules = DrivingQuestion.routingRules(page, ctx)
}
}

convertPageType(type) {
Expand Down
16 changes: 16 additions & 0 deletions src/eq_schema/schema/Group/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ class Group {
),
];
}
if (page.pageType === "ListCollectorPage") {
const drivingBlock = {
...page,
pageType: "DrivingQuestionPage",
}
delete drivingBlock.routing
delete drivingBlock.skipConditions
return [
new Block(
drivingBlock,
section.id,
ctx
),
block
]
}
return block;
})
);
Expand Down

0 comments on commit 16c0065

Please sign in to comment.