Skip to content

Commit

Permalink
feat(project): fetch ads in PlayerContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonLantukh committed Nov 8, 2023
1 parent 71dc1b8 commit b0eb27a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/components/Player/Player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 { useAds } from '#src/hooks/useAds';
import type { AdSchedule } from '#types/ad-schedule';

type Props = {
playerId: string;
Expand All @@ -18,6 +18,7 @@ type Props = {
item: PlaylistItem;
startTime?: number;
autostart?: boolean;
adsData?: AdSchedule;
onReady?: (player?: JWPlayer) => void;
onPlay?: () => void;
onPause?: () => void;
Expand All @@ -36,6 +37,7 @@ const Player: React.FC<Props> = ({
playerId,
playerLicenseKey,
item,
adsData,
onReady,
onPlay,
onPause,
Expand All @@ -59,8 +61,6 @@ const Player: React.FC<Props> = ({
const startTimeRef = useRef(startTime);
const setPlayer = useOttAnalytics(item, feedId);

const { data: adsData = {}, isLoading: isAdsLoading } = useAds({ mediaId: item?.mediaid });

const handleBeforePlay = useEventCallback(onBeforePlay);
const handlePlay = useEventCallback(onPlay);
const handlePause = useEventCallback(onPause);
Expand Down Expand Up @@ -208,10 +208,10 @@ const Player: React.FC<Props> = ({
return loadPlaylist();
}

if (libLoaded && !isAdsLoading) {
if (libLoaded) {
initializePlayer();
}
}, [libLoaded, item, detachEvents, attachEvents, playerId, setPlayer, autostart, adsData, playerLicenseKey, feedId, isAdsLoading]);
}, [libLoaded, item, detachEvents, attachEvents, playerId, setPlayer, autostart, adsData, playerLicenseKey, feedId]);

useEffect(() => {
return () => {
Expand Down
5 changes: 4 additions & 1 deletion src/containers/PlayerContainer/PlayerContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import useContentProtection from '#src/hooks/useContentProtection';
import { getMediaById } from '#src/services/api.service';
import LoadingOverlay from '#components/LoadingOverlay/LoadingOverlay';
import { useSettingsStore } from '#src/stores/SettingsStore';
import { useAds } from '#src/hooks/useAds';

type Props = {
item: PlaylistItem;
Expand Down Expand Up @@ -45,6 +46,7 @@ const PlayerContainer: React.FC<Props> = ({
// data
const { data: playableItem, isLoading } = useContentProtection('media', item.mediaid, (token, drmPolicyId) => getMediaById(item.mediaid, token, drmPolicyId));
const { playerId, playerLicenseKey } = useSettingsStore((s) => s);
const { data: adsData, isLoading: isAdsLoading } = useAds({ mediaId: item?.mediaid });

// state
const [playerInstance, setPlayerInstance] = useState<JWPlayer>();
Expand All @@ -68,7 +70,7 @@ const PlayerContainer: React.FC<Props> = ({

const handlePlaylistItemCallback = usePlaylistItemCallback(liveStartDateTime, liveEndDateTime);

if (!playableItem || isLoading) {
if (!playableItem || isLoading || isAdsLoading) {
return <LoadingOverlay inline />;
}

Expand All @@ -78,6 +80,7 @@ const PlayerContainer: React.FC<Props> = ({
playerLicenseKey={playerLicenseKey}
feedId={feedId}
item={playableItem}
adsData={adsData}
onReady={handleReady}
onFirstFrame={handleFirstFrame}
onPlay={onPlay}
Expand Down

0 comments on commit b0eb27a

Please sign in to comment.