Skip to content

Commit

Permalink
finish reducer function for ad placeholder
Browse files Browse the repository at this point in the history
Co-authored-by: Jamie B <[email protected]>
  • Loading branch information
cemms1 and JamieB-gu committed Sep 11, 2023
1 parent 12370f0 commit 4a5593f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
61 changes: 50 additions & 11 deletions dotcom-rendering/src/lib/adPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FEElement } from '../types/content';
import type { AdPlaceholderSlot, FEElement } from '../types/content';

type Accumulator = {
elements: FEElement[];
Expand Down Expand Up @@ -41,25 +41,64 @@ const isSuitablePosition = (
return (paragraphCounter - firstAdIndex) % adEveryNParagraphs === 0;
};

const insertPlaceholder = (acc: Accumulator): Accumulator => {};
const insertPlaceholder = (
elements: FEElement[],
numberOfAdsInserted: Accumulator['numberOfAdsInserted'],
): FEElement[] => {
const placeholder: AdPlaceholderSlot = {
_type: 'model.dotcomrendering.pageElements.AdPlaceholderSlot',
// We only insert square ads for the first ad in the article
isSquare: numberOfAdsInserted === 0,
};
return [...elements, placeholder];
};

/**
* Inserts advert placeholders
*/
const adPlaceholder = (elements: FEElement[]): FEElement[] => {
const elementsWithAds = elements.reduce(
(acc: Accumulator, el: FEElement): Accumulator => {
(acc: Accumulator, el: FEElement, idx: number): Accumulator => {
const isLastElement = elements.length === idx + 1;

isSuitablePosition(el, acc.paragraphCounter, isLastElement)
? insertPlaceholder(acc)
: acc;
const {
elements: prevElements,
paragraphCounter: prevParagraphCounter,
numberOfAdsInserted: prevNumberOfAdsInserted,
} = acc;

const currentParagraphCounter =
el._type ===
'model.dotcomrendering.pageElements.TextBlockElement'
? prevParagraphCounter + 1
: prevParagraphCounter;

const shouldInsertAd = isSuitablePosition(
currentParagraphCounter,
prevNumberOfAdsInserted,
isLastElement,
);

const currentElements = shouldInsertAd
? insertPlaceholder(prevElements, prevNumberOfAdsInserted)
: prevElements;

const currentNumberOfAdsInserted = shouldInsertAd
? prevNumberOfAdsInserted + 1
: prevNumberOfAdsInserted;

return {
elements: currentElements,
paragraphCounter: currentParagraphCounter,
numberOfAdsInserted: currentNumberOfAdsInserted,
};
},
{
elements: [],
paragraphCounter: 0,
numberOfAdsInserted: 0,
},
[],
);

// TODO - remove the last ad slot if it is in the last paragraph position?
elementsWithAds;

return elementsWithAds;
return elementsWithAds.elements;
};
1 change: 1 addition & 0 deletions dotcom-rendering/src/types/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ interface WitnessTypeBlockElement extends ThirdPartyEmbeddedContent {
| WitnessTypeDataText;
}
export type FEElement =
| AdPlaceholderSlot
| AudioAtomBlockElement
| AudioBlockElement
| BlockquoteBlockElement
Expand Down

0 comments on commit 4a5593f

Please sign in to comment.