Skip to content

Commit e99a66c

Browse files
authored
Merge pull request #217 from ONSdigital/fix-repeating-section-list-supplementary-data
EAR-2427 - Pipe list supplementary data into repeating section title
2 parents fb1b13e + d444126 commit e99a66c

File tree

7 files changed

+147
-98
lines changed

7 files changed

+147
-98
lines changed

src/eq_schema/schema/Question/index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const { getPagesByListId } = require("../../../utils/functions/pageGetters");
1616
const Answer = require("../Answer");
1717
const { getValueSource } = require("../../builders/valueSource");
1818

19+
const {
20+
getSectionByPageId,
21+
} = require("../../../utils/functions/sectionGetters");
22+
1923
const {
2024
DATE,
2125
DATE_RANGE,
@@ -31,19 +35,30 @@ const findMutualOption = flow(
3135

3236
const findMutuallyExclusive = flow(get("answers"), find(findMutualOption));
3337

34-
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping);
38+
const processPipe = (ctx, isMultipleChoiceValue = false, isRepeatingSection) =>
39+
flow(
40+
convertPipes(ctx, isMultipleChoiceValue, isRepeatingSection),
41+
getInnerHTMLWithPiping
42+
);
3543
const reversePipe = (ctx) =>
3644
flow(wrapContents("contents"), reversePipeContent(ctx));
3745

3846
class Question {
3947
constructor(question, ctx) {
4048
this.id = `question${question.id}`;
41-
this.title = processPipe(ctx)(question.title);
49+
50+
const section = getSectionByPageId(ctx, question.id);
51+
const isRepeatingSection = section && section.repeatingSection;
52+
53+
this.title = processPipe(ctx, false, isRepeatingSection)(question.title);
54+
4255
if (question.qCode) {
4356
this.q_code = question.qCode.trim();
4457
}
4558
if (question.descriptionEnabled && question.description) {
46-
this.description = [convertPipes(ctx)(question.description)];
59+
this.description = [
60+
convertPipes(ctx, false, isRepeatingSection)(question.description),
61+
];
4762
}
4863

4964
if (question.guidanceEnabled && question.guidance) {

src/eq_schema/schema/Section/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ const {
1818

1919
const translateRoutingAndSkipRules = require("../../builders/routing2");
2020

21-
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping);
21+
const processPipe = (ctx, isMultipleChoiceValue = false, isRepeatingSection) =>
22+
flow(
23+
convertPipes(ctx, isMultipleChoiceValue, isRepeatingSection),
24+
getInnerHTMLWithPiping
25+
);
2226

2327
class Section {
2428
constructor(section, ctx) {
@@ -76,7 +80,7 @@ class Section {
7680
};
7781

7882
this.repeat.title = this.containsPiping(section.title)
79-
? processPipe(ctx)(section.title)
83+
? processPipe(ctx, false, section.repeatingSection)(section.title)
8084
: {
8185
text: `{repeat_title_placeholder}`,
8286
placeholders: [placeholder],

src/utils/builders/index.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@ const convertPipes = require("../convertPipes");
33

44
const { getInnerHTMLWithPiping } = require("../HTMLUtils");
55

6-
const processPipe = (ctx, isMultipleChoiceValue) =>
7-
flow(convertPipes(ctx, isMultipleChoiceValue), getInnerHTMLWithPiping);
6+
const processPipe = (ctx, isMultipleChoiceValue, isRepeatingSection = false) =>
7+
flow(
8+
convertPipes(ctx, isMultipleChoiceValue, isRepeatingSection),
9+
getInnerHTMLWithPiping
10+
);
811

9-
const buildContents = (title, ctx, isMultipleChoiceValue) => {
10-
return processPipe(ctx, isMultipleChoiceValue)(title);
12+
const buildContents = (
13+
title,
14+
ctx,
15+
isMultipleChoiceValue,
16+
isRepeatingSection = false
17+
) => {
18+
return processPipe(ctx, isMultipleChoiceValue, isRepeatingSection)(title);
1119
};
1220

1321
const buildIntroductionTitle = () => {
@@ -40,14 +48,12 @@ const buildIntroductionTitle = () => {
4048
};
4149

4250
const formatListNames = (questionnaire) => {
43-
questionnaire.collectionLists.lists.forEach(
44-
(list) => {
45-
list.listName = list.listName.replace(/ /g,'_');
46-
list.listName = list.listName.replace(/-/g,'_');
47-
list.listName = list.listName.toLowerCase()
48-
}
49-
)
50-
}
51+
questionnaire.collectionLists.lists.forEach((list) => {
52+
list.listName = list.listName.replace(/ /g, "_");
53+
list.listName = list.listName.replace(/-/g, "_");
54+
list.listName = list.listName.toLowerCase();
55+
});
56+
};
5157

5258
module.exports = {
5359
buildContents,

src/utils/compoundFunctions/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const { flow } = require("lodash/fp");
22
const { parseContent, getInnerHTMLWithPiping } = require("../HTMLUtils");
33
const convertPipes = require("../convertPipes");
44

5-
const processPipe = ctx => flow(convertPipes(ctx), getInnerHTMLWithPiping);
5+
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping);
66

7-
const wrapContents = propName => content => {
7+
const wrapContents = (propName) => (content) => {
88
if (!propName || propName === "" || !content) {
99
return;
1010
}
@@ -17,16 +17,16 @@ const wrapContents = propName => content => {
1717
return { [propName]: result };
1818
};
1919

20-
const reversePipeContent = ctx => data => {
20+
const reversePipeContent = (ctx) => (data) => {
2121
if (!data) {
2222
return "";
2323
}
2424

2525
const content = data.contents ? data.contents : data.content;
2626

27-
content.map(items => {
27+
content.map((items) => {
2828
if (items.list) {
29-
items.list = items.list.map(item => processPipe(ctx)(item));
29+
items.list = items.list.map((item) => processPipe(ctx)(item));
3030
}
3131
if (items.description) {
3232
items.description = processPipe(ctx)(items.description);
@@ -39,5 +39,5 @@ const reversePipeContent = ctx => data => {
3939

4040
module.exports = {
4141
wrapContents,
42-
reversePipeContent
42+
reversePipeContent,
4343
};

src/utils/convertPipes/PlaceholderObjectBuilder.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ const placeholderObjectBuilder = (
5050
fallback,
5151
AnswerType,
5252
ctx,
53-
conditionalTradAs
53+
conditionalTradAs,
54+
isRepeatingSection
5455
) => {
5556
let valueSource;
5657
let argumentList;
@@ -90,7 +91,7 @@ const placeholderObjectBuilder = (
9091
}
9192
});
9293

93-
if (isListSupplementaryData) {
94+
if (isListSupplementaryData && !isRepeatingSection) {
9495
return {
9596
placeholder: removeDash(placeholderName),
9697
transforms: [

src/utils/convertPipes/index.js

+79-73
Original file line numberDiff line numberDiff line change
@@ -154,102 +154,108 @@ const parseHTML = (html) => {
154154
return cheerio.load(html)("body");
155155
};
156156

157-
const getPipedData = (store) => (element, ctx, conditionalTradAs) => {
158-
const { piped, ...elementData } = element.data();
159-
const pipeConfig = PIPE_TYPES[piped];
157+
const getPipedData =
158+
(store) => (element, ctx, isRepeatingSection, conditionalTradAs) => {
159+
const { piped, ...elementData } = element.data();
160+
const pipeConfig = PIPE_TYPES[piped];
160161

161-
if (piped === "variable" && element.data().id === "total") {
162-
return `%(total)s`;
163-
}
162+
if (piped === "variable" && element.data().id === "total") {
163+
return `%(total)s`;
164+
}
164165

165-
if (!pipeConfig) {
166-
return "";
167-
}
166+
if (!pipeConfig) {
167+
return "";
168+
}
168169

169-
const entity = pipeConfig.retrieve(elementData, ctx);
170+
const entity = pipeConfig.retrieve(elementData, ctx);
170171

171-
if (!entity) {
172-
return "";
173-
}
172+
if (!entity) {
173+
return "";
174+
}
174175

175-
// Extract 'label' and 'secondaryLabel' values from 'entity'
176-
const { label, secondaryLabel } = entity;
177-
// Create a new element consisting of both 'label' and 'secondaryLabel' along with 'elementData' for 'DateRange' answer types
178-
const dateRangeElement = { ...elementData, label, secondaryLabel };
176+
// Extract 'label' and 'secondaryLabel' values from 'entity'
177+
const { label, secondaryLabel } = entity;
178+
// Create a new element consisting of both 'label' and 'secondaryLabel' along with 'elementData' for 'DateRange' answer types
179+
const dateRangeElement = { ...elementData, label, secondaryLabel };
179180

180-
const placeholderName =
181-
elementData.type === "DateRange"
182-
? pipeConfig.placeholder(dateRangeElement)
183-
: pipeConfig.placeholder(entity);
181+
const placeholderName =
182+
elementData.type === "DateRange"
183+
? pipeConfig.placeholder(dateRangeElement)
184+
: pipeConfig.placeholder(entity);
184185

185-
const identifier =
186-
elementData.type === "DateRange"
187-
? pipeConfig.render(elementData)
188-
: pipeConfig.render(entity);
186+
const identifier =
187+
elementData.type === "DateRange"
188+
? pipeConfig.render(elementData)
189+
: pipeConfig.render(entity);
189190

190-
const answerType = pipeConfig.getType(entity);
191+
const answerType = pipeConfig.getType(entity);
191192

192-
const fallback = pipeConfig.getFallback({ ...entity, ...elementData });
193+
const fallback = pipeConfig.getFallback({ ...entity, ...elementData });
193194

194-
let placeholder;
195+
let placeholder;
195196

196-
let dateFormat, unitType;
197+
let dateFormat, unitType;
197198

198-
if (entity.properties) {
199-
dateFormat = entity.properties.format;
200-
unitType = entity.properties.unit;
201-
}
199+
if (entity.properties) {
200+
dateFormat = entity.properties.format;
201+
unitType = entity.properties.unit;
202+
}
202203

203-
placeholder = placeholderObjectBuilder(
204-
piped,
205-
placeholderName,
206-
identifier,
207-
dateFormat,
208-
unitType,
209-
fallback,
210-
answerType,
211-
ctx,
212-
conditionalTradAs
213-
);
204+
placeholder = placeholderObjectBuilder(
205+
piped,
206+
placeholderName,
207+
identifier,
208+
dateFormat,
209+
unitType,
210+
fallback,
211+
answerType,
212+
ctx,
213+
conditionalTradAs,
214+
isRepeatingSection
215+
);
214216

215-
store.placeholders = [...store.placeholders, placeholder];
217+
store.placeholders = [...store.placeholders, placeholder];
216218

217-
return `{${removeDash(placeholderName)}}`;
218-
};
219+
return `{${removeDash(placeholderName)}}`;
220+
};
219221

220-
const convertPipes = (ctx, isMultipleChoiceValue) => (html) => {
221-
if (!html) {
222-
return html;
223-
}
222+
const convertPipes =
223+
(ctx, isMultipleChoiceValue, isRepeatingSection = false) =>
224+
(html) => {
225+
if (!html) {
226+
return html;
227+
}
224228

225-
const store = {
226-
text: "",
227-
placeholders: [],
228-
};
229+
const store = {
230+
text: "",
231+
placeholders: [],
232+
};
229233

230-
const $ = parseHTML(html);
231-
const conditionalTradAs =
232-
$.text().includes("(trad_as)") || $.text().includes("([Trad As])");
234+
const $ = parseHTML(html);
235+
const conditionalTradAs =
236+
$.text().includes("(trad_as)") || $.text().includes("([Trad As])");
233237

234-
$.find("[data-piped]").each((index, element) => {
235-
const $elem = cheerio(element);
236-
$elem.replaceWith(getPipedData(store)($elem, ctx, conditionalTradAs));
237-
});
238+
$.find("[data-piped]").each((index, element) => {
239+
const $elem = cheerio(element);
240+
$elem.replaceWith(
241+
getPipedData(store)($elem, ctx, isRepeatingSection, conditionalTradAs)
242+
);
243+
});
238244

239-
store.text = unescapePiping($.html(), isMultipleChoiceValue);
245+
store.text = unescapePiping($.html(), isMultipleChoiceValue);
240246

241-
if (conditionalTradAs) {
242-
store.text = store.text.replace("({trad_as})", "{trad_as}");
243-
}
244-
245-
store.text = store.text.replace(/\s+$/, '');
247+
if (conditionalTradAs) {
248+
store.text = store.text.replace("({trad_as})", "{trad_as}");
249+
}
246250

247-
if (!store.placeholders.length) {
248-
return store.text;
249-
}
251+
store.text = store.text.replace(/\s+$/, "");
250252

251-
return store;
252-
};
253+
if (!store.placeholders.length) {
254+
return store.text;
255+
}
256+
257+
return store;
258+
};
253259

254260
module.exports = convertPipes;
255261
module.exports.getAllAnswers = getAllAnswers;

src/utils/functions/sectionGetters.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const getSectionByPageId = (ctx, pageId) => {
2+
let result;
3+
if (ctx && ctx.questionnaireJson && ctx.questionnaireJson.sections) {
4+
ctx.questionnaireJson.sections.forEach((section) => {
5+
section.folders.forEach((folder) => {
6+
folder.pages.forEach((page) => {
7+
if (page.id === pageId) {
8+
result = section;
9+
}
10+
});
11+
});
12+
});
13+
}
14+
return result;
15+
};
16+
17+
module.exports = { getSectionByPageId };

0 commit comments

Comments
 (0)