Skip to content

Commit f0dad6a

Browse files
committed
feat: add banner ads in postViewer
1 parent 317a87e commit f0dad6a

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

src/components/post/PostBanner.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import HorizontalBanner from '../../containers/post/HorizontalBanner';
3+
import PostCustomBanner from './PostCustomBanner';
4+
5+
interface PostBannerProps {
6+
isDisplayAd?: boolean;
7+
customAd: { image: string; url: string } | null;
8+
}
9+
10+
const PostBanner: React.FC<PostBannerProps> = ({ isDisplayAd, customAd }) => {
11+
if (customAd) {
12+
return <PostCustomBanner image={customAd.image} url={customAd.url} />;
13+
}
14+
return <HorizontalBanner isDisplayAd={isDisplayAd} />;
15+
};
16+
17+
export default PostBanner;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
import VelogResponsive from '../velog/VelogResponsive';
4+
import gtag from '../../lib/gtag';
5+
import media from '../../lib/styles/media';
6+
7+
interface PostCustomBannerProps {
8+
image: string;
9+
url: string;
10+
}
11+
12+
const onClick = () => {
13+
gtag('event', 'ads_banner_click');
14+
};
15+
16+
const PostCustomBanner: React.FC<PostCustomBannerProps> = ({ image, url }) => {
17+
return (
18+
<PostCustomBannerBlock onClick={onClick}>
19+
<a href={url}>
20+
<img src={image} alt="post-custom-banner" />
21+
</a>
22+
</PostCustomBannerBlock>
23+
);
24+
};
25+
26+
const PostCustomBannerBlock = styled(VelogResponsive)`
27+
max-width: 100%;
28+
height: auto;
29+
margin-top: 1rem;
30+
31+
${media.small} {
32+
margin-top: 0.5rem;
33+
}
34+
35+
img {
36+
display: block;
37+
width: 100%;
38+
object-fit: contain;
39+
}
40+
`;
41+
42+
export default PostCustomBanner;

src/containers/post/PostViewer.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ import { useSetShowFooter } from '../../components/velog/VelogPageTemplate';
4545
import HorizontalBanner from './HorizontalBanner';
4646
import gtag from '../../lib/gtag';
4747
import FollowButton from '../../components/common/FollowButton';
48+
import { BANNER_ADS } from '../../lib/graphql/ad';
49+
import PostBanner from '../../components/post/PostBanner';
4850

4951
const UserProfileWrapper = styled(VelogResponsive)`
5052
margin-top: 16rem;
@@ -105,6 +107,20 @@ const PostViewer: React.FC<PostViewerProps> = ({
105107
const [likePost, { loading: loadingLike }] = useMutation(LIKE_POST);
106108
const [unlikePost, { loading: loadingUnlike }] = useMutation(UNLIKE_POST);
107109
const { showNotFound } = useNotFound();
110+
const { data: ads } = useQuery(BANNER_ADS, {
111+
variables: {
112+
writerUsername: username,
113+
},
114+
});
115+
116+
const customAd = useMemo(() => {
117+
if (!ads) return null;
118+
if (ads.bannerAds.length === 0) return null;
119+
return {
120+
image: ads.bannerAds[0].image,
121+
url: ads.bannerAds[0].url,
122+
};
123+
}, [ads]);
108124

109125
// const userLogo = useSelector((state: RootState) => state.header.userLogo);
110126
// const velogTitle = useMemo(() => {
@@ -433,7 +449,7 @@ const PostViewer: React.FC<PostViewerProps> = ({
433449
/>
434450
}
435451
/>
436-
{shouldShowBanner ? <HorizontalBanner /> : null}
452+
{shouldShowBanner ? <PostBanner customAd={customAd} /> : null}
437453
<PostContent isMarkdown={post.is_markdown} body={post.body} />
438454
<UserProfileWrapper>
439455
<UserProfile
@@ -453,8 +469,12 @@ const PostViewer: React.FC<PostViewerProps> = ({
453469
/>
454470
</UserProfileWrapper>
455471
<LinkedPostList linkedPosts={post.linked_posts} />
456-
{shouldShowBanner && isContentLongEnough ? <HorizontalBanner /> : null}
457-
{shouldShowFooterBanner ? <HorizontalBanner isDisplayAd /> : null}
472+
{shouldShowBanner && isContentLongEnough ? (
473+
<PostBanner customAd={customAd} />
474+
) : null}
475+
{shouldShowFooterBanner ? (
476+
<PostBanner isDisplayAd={true} customAd={customAd} />
477+
) : null}
458478
<PostComments
459479
count={post.comments_count}
460480
comments={post.comments}

src/lib/graphql/ad.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { gql } from 'apollo-boost';
2+
3+
export type Ad = {
4+
id: string;
5+
title: string;
6+
body: string;
7+
image: string;
8+
url: string;
9+
};
10+
11+
export const BANNER_ADS = gql`
12+
query BannerAds($writerUsername: String!) {
13+
bannerAds(writer_username: $writerUsername) {
14+
id
15+
title
16+
body
17+
url
18+
image
19+
}
20+
}
21+
`;

0 commit comments

Comments
 (0)