diff --git a/src/hooks/useAds.ts b/src/hooks/useAds.ts index 3144c08d2..d54aec8d5 100644 --- a/src/hooks/useAds.ts +++ b/src/hooks/useAds.ts @@ -3,44 +3,21 @@ import { useQuery } from 'react-query'; import { useConfigStore } from '#src/stores/ConfigStore'; import ApiService from '#src/services/api.service'; import { getModule } from '#src/modules/container'; +import { addQueryParams } from '#src/utils/formatting'; const CACHE_TIME = 60 * 1000 * 20; /** - * @deprecated Use {@link useAppBasedAds} instead. + * @deprecated Use adScheduleUrls.xml form the config instead. */ const useLegacyStandaloneAds = ({ adScheduleId, enabled }: { adScheduleId: string | null | undefined; enabled: boolean }) => { const apiService = getModule(ApiService); - const { isLoading, data } = useQuery( - ['ad-schedule', adScheduleId], - async () => { - const adSchedule = await apiService.getAdSchedule(adScheduleId); - - return adSchedule; - }, - { enabled: enabled && !!adScheduleId, cacheTime: CACHE_TIME, staleTime: CACHE_TIME }, - ); - - return { - isLoading, - data, - }; -}; - -const useAppBasedAds = ({ jsonUrl, mediaId, enabled }: { jsonUrl: string | null | undefined; mediaId: string; enabled: boolean }) => { - const apiService = getModule(ApiService); - - const { isLoading, data } = useQuery( - ['media-ads', mediaId], - async () => { - // Waiting for `prd` deploy to remove `replace` - const mediaAds = await apiService.getMediaAds(jsonUrl?.replace('advertising/site', 'sites') as string, mediaId); - - return mediaAds; - }, - { enabled: enabled && !!mediaId, cacheTime: CACHE_TIME, staleTime: CACHE_TIME }, - ); + const { isLoading, data } = useQuery(['ad-schedule', adScheduleId], async () => apiService.getAdSchedule(adScheduleId), { + enabled: enabled && !!adScheduleId, + cacheTime: CACHE_TIME, + staleTime: CACHE_TIME, + }); return { isLoading, @@ -51,14 +28,19 @@ const useAppBasedAds = ({ jsonUrl, mediaId, enabled }: { jsonUrl: string | null export const useAds = ({ mediaId }: { mediaId: string }) => { const { adSchedule: adScheduleId, adScheduleUrls } = useConfigStore((s) => s.config); - // adScheduleUrls.json prop exists when ad-config is attached to the App Config - const useAppBasedFlow = !!adScheduleUrls?.json; + // adScheduleUrls.xml prop exists when ad-config is attached to the App Config + const useAppBasedFlow = !!adScheduleUrls?.xml; - const { data: mediaAds, isLoading: isMediaAdsLoading } = useAppBasedAds({ jsonUrl: adScheduleUrls?.json, mediaId, enabled: useAppBasedFlow }); const { data: adSchedule, isLoading: isAdScheduleLoading } = useLegacyStandaloneAds({ adScheduleId, enabled: !useAppBasedFlow }); + const adConfig = { + client: 'vast', + schedule: addQueryParams(adScheduleUrls?.xml || '', { + media_id: mediaId, + }), + }; return { - isLoading: useAppBasedFlow ? isMediaAdsLoading : isAdScheduleLoading, - data: useAppBasedFlow ? mediaAds : adSchedule, + isLoading: useAppBasedFlow ? false : isAdScheduleLoading, + data: useAppBasedFlow ? adConfig : adSchedule, }; }; diff --git a/types/ad-schedule.d.ts b/types/ad-schedule.d.ts index f27519e3e..4ead8eded 100644 --- a/types/ad-schedule.d.ts +++ b/types/ad-schedule.d.ts @@ -1,5 +1,5 @@ export type AdSchedule = { - adscheduleid: string; + adscheduleid?: string; client: string; schedule: string; };