Skip to content

Commit 7ca42fb

Browse files
committed
dynamic ads
1 parent 04e66bb commit 7ca42fb

3 files changed

Lines changed: 48 additions & 22 deletions

File tree

src/features/ads/api/getAd.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
import { useQuery } from '@tanstack/react-query'
2+
import { axios } from 'src/lib/axios'
23
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query'
34
import { Ad } from '../types'
4-
import { axios } from 'src/lib/axios'
55

6-
const getAd = async (keywords: string[]): Promise<Ad | null> => {
7-
let url = new URL(window.location.href);
8-
let ref = url.searchParams.get("ref");
9-
return axios.get('/engine/ads/', {params: { ref, keywords: keywords.join(",") }})
6+
7+
8+
9+
const getAd = async (
10+
keywords: string[],
11+
aditionalAdQueries: { [key: string]: string } | undefined
12+
): Promise<Ad | null> => {
13+
let params = { keywords: keywords.join(',') }
14+
if (aditionalAdQueries) {
15+
params = { ...params, ...aditionalAdQueries }
16+
}
17+
return axios.get('/engine/ads/', { params })
1018
}
1119

1220
type QueryFnType = typeof getAd
1321

1422
type UseGetAdOptions = {
15-
keywords: string[];
23+
keywords: string[]
1624
config?: QueryConfig<QueryFnType>
25+
aditionalAdQueries: { [key: string]: string } | undefined
1726
}
18-
export const useGetAd = ({ keywords, config }: UseGetAdOptions) => {
27+
export const useGetAd = ({ keywords, config, aditionalAdQueries }: UseGetAdOptions) => {
1928
return useQuery<ExtractFnReturnType<QueryFnType>>({
2029
...config,
2130
queryKey: ['ad'],
22-
queryFn: () => getAd(keywords),
31+
queryFn: () => getAd(keywords, aditionalAdQueries),
2332
})
2433
}

src/features/ads/components/BannerAd.tsx

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
1-
import './BannerAd.css'
2-
import { useGetAd } from '../api/getAd'
3-
import { useUserPreferences } from 'src/stores/preferences'
1+
import { useState } from 'react'
42
import { AdPlaceholder } from 'src/components/placeholders'
3+
import { useUserPreferences } from 'src/stores/preferences'
4+
import { useGetAd } from '../api/getAd'
5+
import './BannerAd.css'
56

67
export const BannerAd = () => {
78
const { userSelectedTags } = useUserPreferences()
9+
10+
const [aditionalAdQueries, setAditionalAdQueries] = useState<
11+
{ [key: string]: string } | undefined
12+
>()
813
const {
914
data: ad,
1015
isLoading,
1116
isError,
1217
} = useGetAd({
13-
keywords: userSelectedTags.map(tag => tag.label.toLocaleLowerCase()),
18+
keywords: userSelectedTags.map((tag) => tag.label.toLocaleLowerCase()),
19+
aditionalAdQueries: aditionalAdQueries,
1420
config: {
1521
cacheTime: 0,
1622
staleTime: 0,
23+
refetchInterval(data, query) {
24+
if (data?.nextAd) {
25+
setAditionalAdQueries(data.nextAd.queries)
26+
return data.nextAd.interval
27+
}
28+
return false
29+
},
1730
},
1831
})
1932

@@ -59,9 +72,7 @@ export const BannerAd = () => {
5972
</a>
6073
</span>
6174
</div>
62-
{ad.viewUrl && (
63-
<img src={ad.viewUrl} key={ad.viewUrl} className="hidden" alt="" />
64-
)}
75+
{ad.viewUrl && <img src={ad.viewUrl} key={ad.viewUrl} className="hidden" alt="" />}
6576
</div>
6677
)
6778
}

src/features/ads/types/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ type AdProvider = {
44
link?: string,
55
}
66

7+
type NextAdType = {
8+
queries: { [key: string]: string }
9+
interval: number
10+
}
11+
712
export type Ad = {
8-
title?: string,
9-
description: string,
10-
imageUrl: string,
11-
viewUrl?: string,
12-
link: string,
13-
backgroundColor?: string,
14-
provider: AdProvider,
13+
title?: string
14+
description: string
15+
imageUrl: string
16+
viewUrl?: string
17+
link: string
18+
backgroundColor?: string
19+
provider: AdProvider
20+
nextAd?: NextAdType
1521
}

0 commit comments

Comments
 (0)