Skip to content

Commit

Permalink
Merge pull request #618 from Financial-Times/ADSDEV-924_add_ads_to_li…
Browse files Browse the repository at this point in the history
…ve_blogs

ADSDEV-924 Insert ads into live blogs
  • Loading branch information
alevito authored Oct 14, 2021
2 parents 011cb5b + 75d3597 commit 0d6ab16
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
7 changes: 5 additions & 2 deletions components/x-live-blog-post/src/LiveBlogPost.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const LiveBlogPost = (props) => {
standout = {},
articleUrl,
showShareButtons = false,
byline
byline,
ad
} = props

const showBreakingNewsLabel = standout.breakingNews || isBreakingNews
Expand All @@ -26,7 +27,8 @@ const LiveBlogPost = (props) => {
className={`live-blog-post ${styles['live-blog-post']}`}
data-trackable="live-post"
id={`post-${id || postId}`}
data-x-component="live-blog-post">
data-x-component="live-blog-post"
>
<div className="live-blog-post__meta">
<Timestamp publishedTimestamp={publishedDate || publishedTimestamp} />
</div>
Expand All @@ -38,6 +40,7 @@ const LiveBlogPost = (props) => {
dangerouslySetInnerHTML={{ __html: bodyHTML || content }}
/>
{showShareButtons && <ShareButtons postId={id || postId} articleUrl={articleUrl} title={title} />}
{ad}
</article>
)
}
Expand Down
12 changes: 10 additions & 2 deletions components/x-live-blog-wrapper/src/LiveBlogWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ const withLiveBlogWrapperActions = withActions({
}
})

const BaseLiveBlogWrapper = ({ posts = [], articleUrl, showShareButtons, id, liveBlogWrapperElementRef }) => {
const BaseLiveBlogWrapper = ({
posts = [],
ads = {},
articleUrl,
showShareButtons,
id,
liveBlogWrapperElementRef
}) => {
posts.sort((a, b) => {
const timestampA = a.publishedDate || a.publishedTimestamp
const timestampB = b.publishedDate || b.publishedTimestamp
Expand All @@ -37,12 +44,13 @@ const BaseLiveBlogWrapper = ({ posts = [], articleUrl, showShareButtons, id, liv
return 0
})

const postElements = posts.map((post) => (
const postElements = posts.map((post, index) => (
<LiveBlogPost
key={`live-blog-post-${post.id}`}
{...post}
articleUrl={articleUrl}
showShareButtons={showShareButtons}
ad={ads[index]}
/>
))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const post2 = {
showShareButtons: true
}

const ads = {
1: (
<div className="o-ads" data-o-ads-name="mid1">
Ads
</div>
)
}

describe('x-live-blog-wrapper', () => {
it('has a displayName', () => {
expect(LiveBlogWrapper.displayName).toContain('BaseLiveBlogWrapper')
Expand All @@ -49,6 +57,15 @@ describe('x-live-blog-wrapper', () => {
expect(articles.at(0).html()).toContain('Post 2 Title')
expect(articles.at(1).html()).toContain('Post 1 Title')
})

it('renders an ad slot element at the given position', () => {
const posts = [post1, post2]
const liveBlogWrapper = mount(<LiveBlogWrapper posts={posts} ads={ads} />)

const articles = liveBlogWrapper.find('article')
expect(articles.at(0).html()).not.toContain('Ads')
expect(articles.at(1).html()).toContain('Ads')
})
})

describe('liveBlogWrapperActions', () => {
Expand Down
37 changes: 36 additions & 1 deletion components/x-live-blog-wrapper/storybook/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@ import React from 'react'
import { LiveBlogWrapper } from '../src/LiveBlogWrapper'
import '../../x-live-blog-post/dist/LiveBlogPost.css'

const Ad = (props) => {
const {
slotName,
targeting,
defaultFormat = 'false',
small = 'false',
medium = 'false',
large = 'false',
extra = 'false',
alignment = 'center'
} = props

const classes = `o-ads o-ads--${alignment} o-ads--transition`

return (
<div
data-o-ads-name={slotName}
data-o-ads-targeting={targeting}
data-o-ads-formats-default={defaultFormat}
data-o-ads-formats-small={small}
data-o-ads-formats-medium={medium}
data-o-ads-formats-large={large}
data-o-ads-formats-extra={extra}
data-o-ads-label="true"
aria-hidden="true"
tabIndex="-1"
className={classes}
/>
)
}

const defaultProps = {
message: 'Test',
posts: [
Expand Down Expand Up @@ -32,7 +63,11 @@ const defaultProps = {
articleUrl: 'https://www.ft.com/content/2b665ec7-a88f-3998-8f39-5371f9c791ed',
showShareButtons: true
}
]
],
ads: {
1: <Ad />,
2: <Ad />
}
}

export default {
Expand Down

0 comments on commit 0d6ab16

Please sign in to comment.