Skip to content

Commit

Permalink
feat(project): use xml for ad config (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLantukh authored Jan 9, 2024
1 parent 527ca02 commit 2aec0dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
52 changes: 17 additions & 35 deletions src/hooks/useAds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
};
};
2 changes: 1 addition & 1 deletion types/ad-schedule.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type AdSchedule = {
adscheduleid: string;
adscheduleid?: string;
client: string;
schedule: string;
};
Expand Down

0 comments on commit 2aec0dd

Please sign in to comment.