Skip to content

Commit

Permalink
Merge pull request #117 from ONSdigital/EAR-1893-piping-date-range-an…
Browse files Browse the repository at this point in the history
…swers

EAR-1893 - piping date range answers
  • Loading branch information
sudeepkunhis authored Oct 27, 2022
2 parents cf5bad5 + 6886e7b commit a367e12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
36 changes: 26 additions & 10 deletions src/utils/convertPipes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ const getCalculatedSummary = (ctx, pageId) =>
(page) => page.id === pageId
);

// Define a new function to format labels
const formatter = (label) => {
if (label) {
var formattedLabel = label;
formattedLabel = formattedLabel.replace(/[^a-zA-Z0-9 ]/g, "");
formattedLabel = formattedLabel.replace(/ /g, "_");
return formattedLabel;
} else {
return "untitled_answer";
}
};

const PIPE_TYPES = {
answers: {
retrieve: ({ id, type }, ctx) => {
Expand All @@ -51,15 +63,14 @@ const PIPE_TYPES = {
return getAnswer(ctx, tempId);
},
render: ({ id }) => id,
placeholder: ({ label }) => {
if (label) {
var formattedLabel = label;
formattedLabel = formattedLabel.replace(/[^a-zA-Z0-9 ]/g, "");
formattedLabel = formattedLabel.replace(/ /g, "_");
return formattedLabel
}
else {
return 'untitled_answer';
placeholder: ({ label, secondaryLabel, type, id }) => {
// The placeholders will now be 'label' and 'secondaryLabel' values, hence different for the piped date range values.
if (type === "DateRange") {
return id.endsWith("from")
? formatter(label)
: formatter(secondaryLabel);
} else {
return formatter(label);
}
},
getType: ({ type }) => type,
Expand Down Expand Up @@ -134,9 +145,14 @@ const getPipedData = (store) => (element, ctx) => {
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 };

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

const identifier =
Expand Down
8 changes: 5 additions & 3 deletions src/utils/convertPipes/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,15 @@ describe("convertPipes", () => {

describe("formatting", () => {
it("should format Date Range answers with `format_date`", () => {
const html = createPipe({ id: "3" });
const html = createPipe({
id: "3",
});
expect(convertPipes(createContext())(html)).toEqual(
createWrapper(
"{Its_Q3}",
"{untitled_answer}",
createTransformation(
{
placeholder: "Its_Q3",
placeholder: "untitled_answer",
identifier: "answer3",
source: "answers",
argument: "date_to_format",
Expand Down

0 comments on commit a367e12

Please sign in to comment.