-
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 #88 from ONSdigital/added-list-collector
Added list collector
- Loading branch information
Showing
9 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
src/eq_schema/block-types/listCollector/__snapshots__/index.test.js.snap
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,126 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Add Block should build the Add block 1`] = ` | ||
AddBlock { | ||
"cancel_text": "Don’t need to add this item", | ||
"id": "add-block-listcollector-1", | ||
"question": Object { | ||
"answers": Array [ | ||
Answer { | ||
"id": "answeranswer-1", | ||
"mandatory": false, | ||
"type": undefined, | ||
}, | ||
], | ||
"id": "add-block-question-listcollector-1", | ||
"title": "Enter details", | ||
"type": "General", | ||
}, | ||
"type": "ListAddQuestion", | ||
} | ||
`; | ||
|
||
exports[`Edit Block should build the Edit block 1`] = ` | ||
EditBlock { | ||
"cancel_text": "Don’t need to edit this item", | ||
"id": "edit-block-listcollector-1", | ||
"question": Object { | ||
"answers": Array [ | ||
Answer { | ||
"id": "answeranswer-1", | ||
"mandatory": false, | ||
"type": undefined, | ||
}, | ||
], | ||
"id": "edit-block-question-listcollector-1", | ||
"title": "Enter details", | ||
"type": "General", | ||
}, | ||
"type": "ListEditQuestion", | ||
} | ||
`; | ||
|
||
exports[`Remove Block should build the remove block 1`] = ` | ||
RemoveBlock { | ||
"cancel_text": "Don’t need to remove this item?", | ||
"id": "remove-block-listcollector-1", | ||
"question": Object { | ||
"answers": Array [ | ||
Object { | ||
"id": "remove-confirmation-listcollector-1", | ||
"mandatory": true, | ||
"options": Array [ | ||
Object { | ||
"action": Object { | ||
"type": "RemoveListItemAndAnswers", | ||
}, | ||
"label": "Yes", | ||
"value": "Yes", | ||
}, | ||
Object { | ||
"label": "No", | ||
"value": "No", | ||
}, | ||
], | ||
"type": "Radio", | ||
}, | ||
], | ||
"id": "remove-block-question-listcollector-1", | ||
"title": "Are you sure you want to remove this item?", | ||
"type": "General", | ||
"warning": "All of the information about this item will be deleted", | ||
}, | ||
"type": "ListRemoveQuestion", | ||
} | ||
`; | ||
|
||
exports[`Summary Block should build the Summary block 1`] = ` | ||
SummaryBlock { | ||
"item_title": Object { | ||
"placeholders": Array [ | ||
Object { | ||
"placeholder": "item-text-listcollector-1", | ||
"transforms": Array [ | ||
Object { | ||
"arguments": Object { | ||
"delimiter": " ", | ||
"list_to_concatenate": Array [], | ||
}, | ||
"transform": "concatenate_list", | ||
}, | ||
], | ||
}, | ||
], | ||
"text": "{item-text-listcollector-1}", | ||
}, | ||
"title": "Summary", | ||
} | ||
`; | ||
|
||
exports[`list collector question should build valid list collector question 1`] = ` | ||
ListCollectorQuestion { | ||
"answers": Array [ | ||
Object { | ||
"id": "add-another-listcollector-1", | ||
"mandatory": true, | ||
"options": Array [ | ||
Object { | ||
"action": Object { | ||
"type": "RedirectToListAddBlock", | ||
}, | ||
"label": "Yes", | ||
"value": "Yes", | ||
}, | ||
Object { | ||
"label": "No", | ||
"value": "No", | ||
}, | ||
], | ||
"type": "Radio", | ||
}, | ||
], | ||
"id": "list-collector-question-listcollector-1", | ||
"title": "Add another", | ||
"type": "General", | ||
} | ||
`; |
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,34 @@ | ||
const convertPipes = require("../../../utils/convertPipes"); | ||
const { getInnerHTMLWithPiping } = require("../../../utils/HTMLUtils"); | ||
const { flow } = require("lodash/fp"); | ||
const { remove, cloneDeep, find } = require("lodash"); | ||
const Answer = require("../../schema/Answer"); | ||
|
||
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping); | ||
|
||
class AddBlock { | ||
constructor(page, ctx) { | ||
this.id = `add-block-${page.id}` | ||
this.type = "ListAddQuestion" | ||
this.cancel_text = "Don’t need to add this item" | ||
const listAnswers = find(ctx.questionnaireJson.collectionLists.lists, { id: page.listId }).answers | ||
this.question = { | ||
id: `add-block-question-${page.id}`, | ||
type: "General", | ||
title: processPipe(ctx)(page.addItemTitle), | ||
answers: this.buildAnswers(listAnswers, ctx) | ||
} | ||
} | ||
|
||
buildAnswers(answers, ctx) { | ||
return answers.map((answer) => { | ||
const tempAnswer = cloneDeep(answer); | ||
if (tempAnswer.options) { | ||
remove(tempAnswer.options, { mutuallyExclusive: true }); | ||
} | ||
return new Answer(tempAnswer, ctx); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = AddBlock; |
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,34 @@ | ||
const convertPipes = require("../../../utils/convertPipes"); | ||
const { getInnerHTMLWithPiping } = require("../../../utils/HTMLUtils"); | ||
const { flow } = require("lodash/fp"); | ||
const { remove, cloneDeep, find } = require("lodash"); | ||
const Answer = require("../../schema/Answer"); | ||
|
||
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping); | ||
|
||
class EditBlock { | ||
constructor(page, ctx) { | ||
this.id = `edit-block-${page.id}` | ||
this.type = "ListEditQuestion" | ||
this.cancel_text = "Don’t need to edit this item" | ||
const listAnswers = find(ctx.questionnaireJson.collectionLists.lists, { id: page.listId }).answers | ||
this.question = { | ||
id: `edit-block-question-${page.id}`, | ||
type: "General", | ||
title: processPipe(ctx)(page.addItemTitle), | ||
answers: this.buildAnswers(listAnswers, ctx) | ||
} | ||
} | ||
|
||
buildAnswers(answers, ctx) { | ||
return answers.map((answer) => { | ||
const tempAnswer = cloneDeep(answer); | ||
if (tempAnswer.options) { | ||
remove(tempAnswer.options, { mutuallyExclusive: true }); | ||
} | ||
return new Answer(tempAnswer, ctx); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = EditBlock; |
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,13 @@ | ||
const ListCollectorQuestion = require("./listCollectorQuestion") | ||
const AddBlock = require("./addBlock") | ||
const EditBlock = require("./editBlock") | ||
const RemoveBlock = require("./removeBlock") | ||
const SummaryBlock = require("./summaryBlock") | ||
|
||
module.exports = { | ||
ListCollectorQuestion, | ||
AddBlock, | ||
EditBlock, | ||
RemoveBlock, | ||
SummaryBlock | ||
} |
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,72 @@ | ||
const { | ||
ListCollectorQuestion, | ||
AddBlock, | ||
EditBlock, | ||
RemoveBlock, | ||
SummaryBlock | ||
} = require("."); | ||
|
||
const createListCollectorPage = () => ({ | ||
id: "listcollector-1", | ||
listId: "list1", | ||
anotherTitle: "Add another", | ||
anotherPositive: "Yes", | ||
anotherNegative: "No", | ||
addItemTitle: "Enter details" | ||
}) | ||
|
||
const createCtx = () => ({ | ||
questionnaireJson: { | ||
id: "questionnaire-1", | ||
collectionLists: { | ||
lists: [ | ||
{ | ||
id: "list1", | ||
answers: [ | ||
{ | ||
id: "answer-1", | ||
displayName: "Answer 1", | ||
properties: { | ||
required: false, | ||
} | ||
} | ||
] | ||
}] | ||
} | ||
} | ||
}) | ||
|
||
describe("list collector question", () => { | ||
it("should build valid list collector question", () => { | ||
const confirmation = new ListCollectorQuestion(createListCollectorPage()); | ||
expect(confirmation).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("Add Block", () => { | ||
it("should build the Add block", () => { | ||
const confirmation = new AddBlock(createListCollectorPage(), createCtx()); | ||
expect(confirmation).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("Edit Block", () => { | ||
it("should build the Edit block", () => { | ||
const confirmation = new EditBlock(createListCollectorPage(), createCtx()); | ||
expect(confirmation).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("Remove Block", () => { | ||
it("should build the remove block", () => { | ||
const confirmation = new RemoveBlock(createListCollectorPage()); | ||
expect(confirmation).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("Summary Block", () => { | ||
it("should build the Summary block", () => { | ||
const confirmation = new SummaryBlock(createListCollectorPage(), createCtx()); | ||
expect(confirmation).toMatchSnapshot(); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
src/eq_schema/block-types/listCollector/listCollectorQuestion.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,33 @@ | ||
const convertPipes = require("../../../utils/convertPipes"); | ||
const { getInnerHTMLWithPiping } = require("../../../utils/HTMLUtils"); | ||
const { flow } = require("lodash/fp"); | ||
|
||
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping); | ||
|
||
class ListCollectorQuestion { | ||
constructor(page, ctx) { | ||
this.id = `list-collector-question-${page.id}` | ||
this.type = "General" | ||
this.title = processPipe(ctx)(page.anotherTitle) | ||
this.answers = [{ | ||
"id": `add-another-${page.id}`, | ||
"mandatory": true, | ||
"type": "Radio", | ||
"options": [ | ||
{ | ||
"label": page.anotherPositive, | ||
"value": page.anotherPositive, | ||
"action": { | ||
"type": "RedirectToListAddBlock" | ||
} | ||
}, | ||
{ | ||
"label": page.anotherNegative, | ||
"value": page.anotherNegative | ||
} | ||
] | ||
}] | ||
} | ||
} | ||
|
||
module.exports = ListCollectorQuestion; |
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,33 @@ | ||
class RemoveBlock { | ||
constructor(page) { | ||
this.id = `remove-block-${page.id}` | ||
this.type = "ListRemoveQuestion" | ||
this.cancel_text = "Don’t need to remove this item?" | ||
this.question = { | ||
id: `remove-block-question-${page.id}`, | ||
type: "General", | ||
title: "Are you sure you want to remove this item?", | ||
warning: "All of the information about this item will be deleted", | ||
answers: [{ | ||
id: `remove-confirmation-${page.id}`, | ||
mandatory: true, | ||
type: "Radio", | ||
options: [ | ||
{ | ||
label: "Yes", | ||
value: "Yes", | ||
action: { | ||
type: "RemoveListItemAndAnswers" | ||
} | ||
}, | ||
{ | ||
label: "No", | ||
value: "No" | ||
} | ||
] | ||
}] | ||
} | ||
} | ||
} | ||
|
||
module.exports = RemoveBlock; |
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,34 @@ | ||
const { find, filter } = require("lodash"); | ||
|
||
class SummaryBlock { | ||
constructor(page, ctx) { | ||
this.title = "Summary" | ||
const listAnswers = find(ctx.questionnaireJson.collectionLists.lists, { id: page.listId }).answers | ||
this.item_title = { | ||
text: `{item-text-${page.id}}`, | ||
placeholders: [ | ||
{ | ||
placeholder: `item-text-${page.id}`, | ||
transforms: [ | ||
{ | ||
arguments: { | ||
delimiter: " ", | ||
list_to_concatenate: this.buildList(listAnswers) | ||
}, | ||
transform: "concatenate_list" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
buildList(answers) { | ||
return filter(answers, { type: "TextField" }).map((answer) => ({ | ||
source: "answers", | ||
identifier: `answer${answer.id}` | ||
})); | ||
} | ||
} | ||
|
||
module.exports = SummaryBlock; |
Oops, something went wrong.