Skip to content

Commit

Permalink
Merge pull request #217 from ONSdigital/fix-repeating-section-list-su…
Browse files Browse the repository at this point in the history
…pplementary-data

EAR-2427 - Pipe list supplementary data into repeating section title
  • Loading branch information
sudeepkunhis authored Jun 13, 2024
2 parents fb1b13e + d444126 commit e99a66c
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 98 deletions.
21 changes: 18 additions & 3 deletions src/eq_schema/schema/Question/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const { getPagesByListId } = require("../../../utils/functions/pageGetters");
const Answer = require("../Answer");
const { getValueSource } = require("../../builders/valueSource");

const {
getSectionByPageId,
} = require("../../../utils/functions/sectionGetters");

const {
DATE,
DATE_RANGE,
Expand All @@ -31,19 +35,30 @@ const findMutualOption = flow(

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

const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping);
const processPipe = (ctx, isMultipleChoiceValue = false, isRepeatingSection) =>
flow(
convertPipes(ctx, isMultipleChoiceValue, isRepeatingSection),
getInnerHTMLWithPiping
);
const reversePipe = (ctx) =>
flow(wrapContents("contents"), reversePipeContent(ctx));

class Question {
constructor(question, ctx) {
this.id = `question${question.id}`;
this.title = processPipe(ctx)(question.title);

const section = getSectionByPageId(ctx, question.id);
const isRepeatingSection = section && section.repeatingSection;

this.title = processPipe(ctx, false, isRepeatingSection)(question.title);

if (question.qCode) {
this.q_code = question.qCode.trim();
}
if (question.descriptionEnabled && question.description) {
this.description = [convertPipes(ctx)(question.description)];
this.description = [
convertPipes(ctx, false, isRepeatingSection)(question.description),
];
}

if (question.guidanceEnabled && question.guidance) {
Expand Down
8 changes: 6 additions & 2 deletions src/eq_schema/schema/Section/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ const {

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

const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping);
const processPipe = (ctx, isMultipleChoiceValue = false, isRepeatingSection) =>
flow(
convertPipes(ctx, isMultipleChoiceValue, isRepeatingSection),
getInnerHTMLWithPiping
);

class Section {
constructor(section, ctx) {
Expand Down Expand Up @@ -76,7 +80,7 @@ class Section {
};

this.repeat.title = this.containsPiping(section.title)
? processPipe(ctx)(section.title)
? processPipe(ctx, false, section.repeatingSection)(section.title)
: {
text: `{repeat_title_placeholder}`,
placeholders: [placeholder],
Expand Down
30 changes: 18 additions & 12 deletions src/utils/builders/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ const convertPipes = require("../convertPipes");

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

const processPipe = (ctx, isMultipleChoiceValue) =>
flow(convertPipes(ctx, isMultipleChoiceValue), getInnerHTMLWithPiping);
const processPipe = (ctx, isMultipleChoiceValue, isRepeatingSection = false) =>
flow(
convertPipes(ctx, isMultipleChoiceValue, isRepeatingSection),
getInnerHTMLWithPiping
);

const buildContents = (title, ctx, isMultipleChoiceValue) => {
return processPipe(ctx, isMultipleChoiceValue)(title);
const buildContents = (
title,
ctx,
isMultipleChoiceValue,
isRepeatingSection = false
) => {
return processPipe(ctx, isMultipleChoiceValue, isRepeatingSection)(title);
};

const buildIntroductionTitle = () => {
Expand Down Expand Up @@ -40,14 +48,12 @@ const buildIntroductionTitle = () => {
};

const formatListNames = (questionnaire) => {
questionnaire.collectionLists.lists.forEach(
(list) => {
list.listName = list.listName.replace(/ /g,'_');
list.listName = list.listName.replace(/-/g,'_');
list.listName = list.listName.toLowerCase()
}
)
}
questionnaire.collectionLists.lists.forEach((list) => {
list.listName = list.listName.replace(/ /g, "_");
list.listName = list.listName.replace(/-/g, "_");
list.listName = list.listName.toLowerCase();
});
};

module.exports = {
buildContents,
Expand Down
12 changes: 6 additions & 6 deletions src/utils/compoundFunctions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const { flow } = require("lodash/fp");
const { parseContent, getInnerHTMLWithPiping } = require("../HTMLUtils");
const convertPipes = require("../convertPipes");

const processPipe = ctx => flow(convertPipes(ctx), getInnerHTMLWithPiping);
const processPipe = (ctx) => flow(convertPipes(ctx), getInnerHTMLWithPiping);

const wrapContents = propName => content => {
const wrapContents = (propName) => (content) => {
if (!propName || propName === "" || !content) {
return;
}
Expand All @@ -17,16 +17,16 @@ const wrapContents = propName => content => {
return { [propName]: result };
};

const reversePipeContent = ctx => data => {
const reversePipeContent = (ctx) => (data) => {
if (!data) {
return "";
}

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

content.map(items => {
content.map((items) => {
if (items.list) {
items.list = items.list.map(item => processPipe(ctx)(item));
items.list = items.list.map((item) => processPipe(ctx)(item));
}
if (items.description) {
items.description = processPipe(ctx)(items.description);
Expand All @@ -39,5 +39,5 @@ const reversePipeContent = ctx => data => {

module.exports = {
wrapContents,
reversePipeContent
reversePipeContent,
};
5 changes: 3 additions & 2 deletions src/utils/convertPipes/PlaceholderObjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const placeholderObjectBuilder = (
fallback,
AnswerType,
ctx,
conditionalTradAs
conditionalTradAs,
isRepeatingSection
) => {
let valueSource;
let argumentList;
Expand Down Expand Up @@ -90,7 +91,7 @@ const placeholderObjectBuilder = (
}
});

if (isListSupplementaryData) {
if (isListSupplementaryData && !isRepeatingSection) {
return {
placeholder: removeDash(placeholderName),
transforms: [
Expand Down
152 changes: 79 additions & 73 deletions src/utils/convertPipes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,102 +154,108 @@ const parseHTML = (html) => {
return cheerio.load(html)("body");
};

const getPipedData = (store) => (element, ctx, conditionalTradAs) => {
const { piped, ...elementData } = element.data();
const pipeConfig = PIPE_TYPES[piped];
const getPipedData =
(store) => (element, ctx, isRepeatingSection, conditionalTradAs) => {
const { piped, ...elementData } = element.data();
const pipeConfig = PIPE_TYPES[piped];

if (piped === "variable" && element.data().id === "total") {
return `%(total)s`;
}
if (piped === "variable" && element.data().id === "total") {
return `%(total)s`;
}

if (!pipeConfig) {
return "";
}
if (!pipeConfig) {
return "";
}

const entity = pipeConfig.retrieve(elementData, ctx);
const entity = pipeConfig.retrieve(elementData, ctx);

if (!entity) {
return "";
}
if (!entity) {
return "";
}

// Extract 'label' and 'secondaryLabel' values from 'entity'
const { label, secondaryLabel } = entity;
// Create a new element consisting of both 'label' and 'secondaryLabel' along with 'elementData' for 'DateRange' answer types
const dateRangeElement = { ...elementData, label, secondaryLabel };
// Extract 'label' and 'secondaryLabel' values from 'entity'
const { label, secondaryLabel } = entity;
// Create a new element consisting of both 'label' and 'secondaryLabel' along with 'elementData' for 'DateRange' answer types
const dateRangeElement = { ...elementData, label, secondaryLabel };

const placeholderName =
elementData.type === "DateRange"
? pipeConfig.placeholder(dateRangeElement)
: pipeConfig.placeholder(entity);
const placeholderName =
elementData.type === "DateRange"
? pipeConfig.placeholder(dateRangeElement)
: pipeConfig.placeholder(entity);

const identifier =
elementData.type === "DateRange"
? pipeConfig.render(elementData)
: pipeConfig.render(entity);
const identifier =
elementData.type === "DateRange"
? pipeConfig.render(elementData)
: pipeConfig.render(entity);

const answerType = pipeConfig.getType(entity);
const answerType = pipeConfig.getType(entity);

const fallback = pipeConfig.getFallback({ ...entity, ...elementData });
const fallback = pipeConfig.getFallback({ ...entity, ...elementData });

let placeholder;
let placeholder;

let dateFormat, unitType;
let dateFormat, unitType;

if (entity.properties) {
dateFormat = entity.properties.format;
unitType = entity.properties.unit;
}
if (entity.properties) {
dateFormat = entity.properties.format;
unitType = entity.properties.unit;
}

placeholder = placeholderObjectBuilder(
piped,
placeholderName,
identifier,
dateFormat,
unitType,
fallback,
answerType,
ctx,
conditionalTradAs
);
placeholder = placeholderObjectBuilder(
piped,
placeholderName,
identifier,
dateFormat,
unitType,
fallback,
answerType,
ctx,
conditionalTradAs,
isRepeatingSection
);

store.placeholders = [...store.placeholders, placeholder];
store.placeholders = [...store.placeholders, placeholder];

return `{${removeDash(placeholderName)}}`;
};
return `{${removeDash(placeholderName)}}`;
};

const convertPipes = (ctx, isMultipleChoiceValue) => (html) => {
if (!html) {
return html;
}
const convertPipes =
(ctx, isMultipleChoiceValue, isRepeatingSection = false) =>
(html) => {
if (!html) {
return html;
}

const store = {
text: "",
placeholders: [],
};
const store = {
text: "",
placeholders: [],
};

const $ = parseHTML(html);
const conditionalTradAs =
$.text().includes("(trad_as)") || $.text().includes("([Trad As])");
const $ = parseHTML(html);
const conditionalTradAs =
$.text().includes("(trad_as)") || $.text().includes("([Trad As])");

$.find("[data-piped]").each((index, element) => {
const $elem = cheerio(element);
$elem.replaceWith(getPipedData(store)($elem, ctx, conditionalTradAs));
});
$.find("[data-piped]").each((index, element) => {
const $elem = cheerio(element);
$elem.replaceWith(
getPipedData(store)($elem, ctx, isRepeatingSection, conditionalTradAs)
);
});

store.text = unescapePiping($.html(), isMultipleChoiceValue);
store.text = unescapePiping($.html(), isMultipleChoiceValue);

if (conditionalTradAs) {
store.text = store.text.replace("({trad_as})", "{trad_as}");
}

store.text = store.text.replace(/\s+$/, '');
if (conditionalTradAs) {
store.text = store.text.replace("({trad_as})", "{trad_as}");
}

if (!store.placeholders.length) {
return store.text;
}
store.text = store.text.replace(/\s+$/, "");

return store;
};
if (!store.placeholders.length) {
return store.text;
}

return store;
};

module.exports = convertPipes;
module.exports.getAllAnswers = getAllAnswers;
17 changes: 17 additions & 0 deletions src/utils/functions/sectionGetters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const getSectionByPageId = (ctx, pageId) => {
let result;
if (ctx && ctx.questionnaireJson && ctx.questionnaireJson.sections) {
ctx.questionnaireJson.sections.forEach((section) => {
section.folders.forEach((folder) => {
folder.pages.forEach((page) => {
if (page.id === pageId) {
result = section;
}
});
});
});
}
return result;
};

module.exports = { getSectionByPageId };

0 comments on commit e99a66c

Please sign in to comment.