injectionEngine
Each array item is iterated over and has each of the provided rules applied to it. If the rule condition passes then the component specified by the rule is injected after the current item. A counter of how many of such components have been injected is kept (by using the rule's componentType) and exposed by the function for other rules to use. Different rules might be adding the same components, in which case the relevant counter will be updated. A counter of how many times each rule has passed is also kept and exposed (by using the rule's unique id). Please ensure that when modifying/adding rules ids are unique.
Usage:
import { injectionRules } from '@dennisdigital/injection-rules-helpers';
const bodyInjected = injectionEngine({
body,
rules,
amp,
props: propsToInject,
});
render() {
return (
<Body
bodyInjected={injectBodyData} template={template}
/>
)
}
Defining Rules
The rules
parameter is an array of objects containing all the rules you want to apply in the body elements. Each object will have the following structure:
{
id: 'inline-and-side',
componentType: 'desktopSideAndInlineAd'
condition: options => shouldInject(options),
component: ({ componentCount: adCount }) => getAdsConfig(adCount),
resetCurrentItemTypeCurrentCounter: true,
};
condition
This is a function you must provide to decide if your component will be injected at this point. When called each condition
function will be provided an object parameter with the following properties which can be used for your logic
amp
Boolean that says whether the page is amp or not (defaults to false
)
bodyElements
Array of body elements you can check against
bodyIndex
The current index of the body element being checked against
currentRuleInjectedComponentCount
The total number of current type of components injected (uses the shared componentType to keep track)
currentRuleComponentTypeWordCount
The total number of word count by specific rule (uses unique rule id to keep track)
currentType
The current type of body element being checked against
currentTypeCurrentCount
The current number of times the current type has been iterated over in the array after the count reset took place (this number will equal the currentTypeTotalCount
if no reset took place)
currentTypeTotalCount
The total number of times that the current type has been iterated over in the array
injectedArray
Injected array up to the current iteration
nextType
The next type of body element in the array
prevType
The next type of body element in the array
props
The props being passed into the injectionEngine to be used by the rules
totalWordCount
Total word count up to the current iteration
component
This can either return an Array of data objects that the <Body/>
component knows how to parse, or just an Array of normal React components
Data object example
component: (props) => [{
type: 'HEADER',
data: `Hi there ${props.name}`,
}]
React component example
component: (props) => [
<Card
title={props.title}
/>
]
or
component: ({ componentCount: adCount }) => getAdsConfig(adCount),
The component function has access to the props that are passed into the injectionEngine function as well as the componentCount that is provided by the injectionEngine, (how many times this component has been injected) are available by the function (props are not being used in this case)
resetCurrentItemTypeCurrentCounter
The function keeps track of the count (both total and current - ie. for when counter gets reset) and matches this count against the rules to decide whether to inject or not.
For example imagine that we want to inject a component every 7 text block. The injectionEngine
function exposes two properties, the currentTypeCurrentCount
and the currentTypeTotalCount
. Both count the number of current type blocks elapsed. The difference is that when we inject a block and the resetCurrentItemTypeCurrentCounter
flag is true
, the currentTypeCurrentCount
will get reset so it starts counting from zero again and when it reaches 7, the confition will be met again. The currentTypeTotalCount
will continue counting the total number of current type blocks iterated over without being reset.