Skip to content

Commit

Permalink
feat(project): use ternanry operator to get ads
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLantukh committed Nov 6, 2023
1 parent 97d43e7 commit 53c3e00
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { PlaylistItem } from '#types/playlist';
import useEventCallback from '#src/hooks/useEventCallback';
import useOttAnalytics from '#src/hooks/useOttAnalytics';
import { logDev, testId } from '#src/utils/common';
import { useConfigStore } from '#src/stores/ConfigStore';
import { useAds } from '#src/hooks/useAds';

type Props = {
Expand Down Expand Up @@ -60,8 +59,7 @@ const Player: React.FC<Props> = ({
const startTimeRef = useRef(startTime);
const setPlayer = useOttAnalytics(item, feedId);

const { adSchedule, adScheduleUrls } = useConfigStore((s) => s.config);
const { data: adsData, isLoading: isAdsLoading } = useAds(adSchedule, adScheduleUrls, item?.mediaid);
const { data: adsData, isLoading: isAdsLoading } = useAds({ mediaId: item?.mediaid });

const handleBeforePlay = useEventCallback(onBeforePlay);
const handlePlay = useEventCallback(onPlay);
Expand Down
25 changes: 13 additions & 12 deletions src/hooks/useAds.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { useQuery } from 'react-query';

import { getMediaAds, getAdSchedule } from '#src/services/api.service';
import type { AdScheduleUrls } from '#types/ad-schedule';
import { useConfigStore } from '#src/stores/ConfigStore';

const CACHE_TIME = 60 * 1000 * 20;

const useAdSchedule = (adScheduleId: string | null | undefined, enabled: boolean) => {
const useAdSchedule = ({ adScheduleId, enabled }: { adScheduleId: string | null | undefined; enabled: boolean }) => {
const { isLoading, data } = useQuery(
['ad-schedule', adScheduleId],
async () => {
const adSchedule = await getAdSchedule(adScheduleId);

return adSchedule;
},
{ enabled, cacheTime: CACHE_TIME, staleTime: CACHE_TIME },
{ enabled: enabled && !!adScheduleId, cacheTime: CACHE_TIME, staleTime: CACHE_TIME },
);

return {
Expand All @@ -22,7 +22,7 @@ const useAdSchedule = (adScheduleId: string | null | undefined, enabled: boolean
};
};

const useMediaAds = (jsonUrl: string | null | undefined, mediaId: string, enabled: boolean) => {
const useMediaAds = ({ jsonUrl, mediaId, enabled }: { jsonUrl: string | null | undefined; mediaId: string; enabled: boolean }) => {
const { isLoading, data } = useQuery(
['media-ads', mediaId],
async () => {
Expand All @@ -31,7 +31,7 @@ const useMediaAds = (jsonUrl: string | null | undefined, mediaId: string, enable

return mediaAds;
},
{ enabled, cacheTime: CACHE_TIME, staleTime: CACHE_TIME },
{ enabled: enabled && !!mediaId, cacheTime: CACHE_TIME, staleTime: CACHE_TIME },
);

return {
Expand All @@ -40,16 +40,17 @@ const useMediaAds = (jsonUrl: string | null | undefined, mediaId: string, enable
};
};

export const useAds = (adScheduleId: string | null | undefined, urls: AdScheduleUrls | undefined, mediaId: string) => {
const hasAdConfig = !!urls?.json;
export const useAds = ({ mediaId }: { mediaId: string }) => {
const { adSchedule: adScheduleId, adScheduleUrls } = useConfigStore((s) => s.config);

// Fetch ad-schedule only when ad-config is not set (`adScheduleUrls.json` prop is not set) + adScheduleId is present
const { data: adSchedule, isLoading: isAdScheduleLoading } = useAdSchedule(adScheduleId, !!(adScheduleId && hasAdConfig));
// adScheduleUrls.json prop exists when ad-config is attached to the App Config
const { isLoading: isMediaAdsLoading, data: mediaAds } = useMediaAds(urls?.json, mediaId, hasAdConfig);
const usePerMediaAds = !!adScheduleUrls?.json;

const { data: mediaAds, isLoading: isMediaAdsLoading } = useMediaAds({ jsonUrl: adScheduleUrls?.json, mediaId, enabled: usePerMediaAds });
const { data: adSchedule, isLoading: isAdScheduleLoading } = useAdSchedule({ adScheduleId, enabled: !usePerMediaAds });

return {
isLoading: isAdScheduleLoading || isMediaAdsLoading,
data: mediaAds || adSchedule,
isLoading: usePerMediaAds ? isMediaAdsLoading : isAdScheduleLoading,
data: usePerMediaAds ? mediaAds : adSchedule,
};
};
1 change: 1 addition & 0 deletions test-e2e/tests/live_channel_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Scenario('I can navigate to live channels from the header', ({ I }) => {
I.see('On Channel 1', locate('div').inside(videoDetailsLocator));
});

// TODO: add BCL SaaS stream
// eslint-disable-next-line codeceptjs/no-skipped-tests
Scenario.skip('I can watch the current live program on the live channel screen', async ({ I }) => {
await I.openVideoCard('Channel 1');
Expand Down

0 comments on commit 53c3e00

Please sign in to comment.