|
1 | | -import React from 'react'; |
2 | | -import {AdSlot, DFPSlotsProvider, DFPManager} from 'react-dfp'; |
3 | | -import {connect} from 'react-redux'; |
4 | | -import {withError} from './with-error'; |
5 | | - |
| 1 | +import React from "react"; |
| 2 | +import { AdSlot, DFPManager, DFPSlotsProvider } from "react-dfp"; |
| 3 | +import { connect } from "react-redux"; |
| 4 | +import { withError } from "./with-error"; |
6 | 5 |
|
7 | 6 | /** |
8 | 7 | * This is a function which can be used to manage ad units in a single place. A component must be created, and used with the `adtype` parameter. These ads are lazy-loaded and single-request mode is disabled by default which can be overwritten as follows. |
@@ -36,34 +35,62 @@ import {withError} from './with-error'; |
36 | 35 | * @param {Object} params |
37 | 36 | * @param {string} params.defaultNetworkID - Network Id of the Ad Provider |
38 | 37 | * @param {Object} params.config - Configuration of the ads (see example) |
| 38 | + * @param {string} params.slotId - A unique string identify the Ad slot |
39 | 39 | * @param {function} params.targeting - Function which takes in the current state, props from the parent component and returns targetting parameters |
40 | 40 | * @param {boolean} params.collapseEmptyDivs (default true) |
41 | 41 | * @param {boolean} params.lazyLoad (default true) |
42 | 42 | * @param {boolean} params.singleRequest - Run Dfp in Single Request Mode (default false) |
43 | 43 | * @category Ads |
44 | 44 | * @returns {Component} A component that can |
45 | 45 | */ |
46 | | -export function createDfpAdComponent({ defaultNetworkID, config, targeting, collapseEmptyDivs = true, lazyLoad = true, singleRequest = false }) { |
47 | | - return connect((state, ownProps) => ({ |
48 | | - targetingArguments: targeting(state, ownProps), |
49 | | - defaultNetworkID: defaultNetworkID, |
50 | | - config: config, |
51 | | - collapseEmptyDivs: collapseEmptyDivs, |
52 | | - lazyLoad: lazyLoad, |
53 | | - singleRequest: singleRequest |
54 | | - }), () => ({}))(withError(DfpAdBase)); |
| 46 | +export function createDfpAdComponent({ |
| 47 | + defaultNetworkID, |
| 48 | + config, |
| 49 | + slotId, |
| 50 | + targeting, |
| 51 | + collapseEmptyDivs = true, |
| 52 | + lazyLoad = true, |
| 53 | + singleRequest = false |
| 54 | +}) { |
| 55 | + return connect( |
| 56 | + (state, ownProps) => ({ |
| 57 | + targetingArguments: targeting(state, ownProps), |
| 58 | + defaultNetworkID: defaultNetworkID, |
| 59 | + config: config, |
| 60 | + slotId: slotId, |
| 61 | + collapseEmptyDivs: collapseEmptyDivs, |
| 62 | + lazyLoad: lazyLoad, |
| 63 | + singleRequest: singleRequest |
| 64 | + }), |
| 65 | + () => ({}) |
| 66 | + )(withError(DfpAdBase)); |
55 | 67 | } |
56 | 68 |
|
57 | | -function DfpAdBase({defaultNetworkID, config, collapseEmptyDivs, targetingArguments, adtype, lazyLoad, singleRequest}) { |
| 69 | +function DfpAdBase({ |
| 70 | + defaultNetworkID, |
| 71 | + config, |
| 72 | + slotId, |
| 73 | + collapseEmptyDivs, |
| 74 | + targetingArguments, |
| 75 | + adtype, |
| 76 | + lazyLoad, |
| 77 | + singleRequest |
| 78 | +}) { |
58 | 79 | const adConfig = config[adtype]; |
59 | | - return <DFPSlotsProvider dfpNetworkId={defaultNetworkID} |
60 | | - collapseEmptyDivs={collapseEmptyDivs} |
61 | | - targetingArguments={targetingArguments} |
62 | | - sizeMapping={adConfig.viewPortSizeMapping} |
63 | | - lazyLoad={lazyLoad} |
64 | | - singleRequest={singleRequest}> |
65 | | - <AdSlot {...adConfig} /> |
66 | | - </DFPSlotsProvider>; |
| 80 | + |
| 81 | + const adProps = { slotId, ...adConfig }; |
| 82 | + return ( |
| 83 | + <DFPSlotsProvider |
| 84 | + dfpNetworkId={defaultNetworkID} |
| 85 | + collapseEmptyDivs={collapseEmptyDivs} |
| 86 | + targetingArguments={targetingArguments} |
| 87 | + sizeMapping={adConfig.viewPortSizeMapping} |
| 88 | + lazyLoad={lazyLoad} |
| 89 | + singleRequest={singleRequest} |
| 90 | + > |
| 91 | + <AdSlot {...adProps} /> |
| 92 | + </DFPSlotsProvider> |
| 93 | + ); |
67 | 94 | } |
68 | 95 |
|
69 | 96 | export function refreshDfpAds(adSlots) { |
|
0 commit comments