Skip to content

Commit

Permalink
feat(videodetail): add playnext for movie
Browse files Browse the repository at this point in the history
  • Loading branch information
royschut committed Jun 17, 2021
1 parent 467501c commit 7564a88
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
9 changes: 8 additions & 1 deletion src/components/Video/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Props = {
play: boolean;
startPlay: () => void;
goBack: () => void;
onComplete?: () => void;
isFavorited: boolean;
onFavoriteButtonClick: () => void;
poster: Poster;
Expand All @@ -49,6 +50,7 @@ const Video: React.FC<Props> = ({
play,
startPlay,
goBack,
onComplete,
poster,
enableSharing,
hasShared,
Expand Down Expand Up @@ -161,7 +163,12 @@ const Video: React.FC<Props> = ({
{play && (
<div className={styles.playerContainer} onMouseMove={mouseActivity} onClick={mouseActivity}>
<div className={styles.player}>
<Cinema item={item} onPlay={() => setIsPlaying(true)} onPause={() => setIsPlaying(false)} />
<Cinema
item={item}
onPlay={() => setIsPlaying(true)}
onPause={() => setIsPlaying(false)}
onComplete={onComplete}
/>
</div>
<div className={classNames(styles.playerContent, { [styles.hidden]: isPlaying && !mouseActive })}>
<IconButton aria-label={t('common:back')} onClick={goBack}>
Expand Down
14 changes: 9 additions & 5 deletions src/containers/Playlist/PlaylistContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect } from 'react';
import type { Playlist } from 'types/playlist';
import type { Playlist, PlaylistItem } from 'types/playlist';

import { PersonalShelf, PersonalShelves } from '../../enum/PersonalShelf';
import usePlaylist, { UsePlaylistResult } from '../../hooks/usePlaylist';
Expand All @@ -14,17 +14,17 @@ type ChildrenParams = {

type Props = {
playlistId: string;
relatedMediaId?: string;
relatedItem?: PlaylistItem;
onPlaylistUpdate?: (playlist: Playlist) => void;
children: (childrenParams: ChildrenParams) => JSX.Element;
};

const PlaylistContainer = ({ playlistId, relatedMediaId, onPlaylistUpdate, children }: Props): JSX.Element | null => {
const PlaylistContainer = ({ playlistId, relatedItem, onPlaylistUpdate, children }: Props): JSX.Element | null => {
const isAlternativeShelf = PersonalShelves.includes(playlistId as PersonalShelf);
const { isLoading, error, data: fetchedPlaylist = { title: '', playlist: [] } }: UsePlaylistResult = usePlaylist(
playlistId,
relatedMediaId,
!isAlternativeShelf,
relatedItem?.mediaid,
!isAlternativeShelf && !!playlistId,
);

let playlist = fetchedPlaylist;
Expand All @@ -44,6 +44,10 @@ const PlaylistContainer = ({ playlistId, relatedMediaId, onPlaylistUpdate, child
if (!playlistId) return <p>No playlist id</p>;
if (!playlist.playlist.length) return null;

if (relatedItem && !playlist.playlist.some(({ mediaid }) => mediaid === relatedItem.mediaid)) {
playlist.playlist.unshift(relatedItem);
}

return children({ playlist, isLoading, error });
};

Expand Down
58 changes: 34 additions & 24 deletions src/screens/Movie/Movie.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ const Movie = ({

const onCardClick = (item: PlaylistItem) => history.push(cardUrl(item));

const playNext = (playlist: PlaylistItem[] | null) => {
if (!item || !playlist) return;

const index = playlist.findIndex(({ mediaid }) => mediaid === item.mediaid);
const nextItem = playlist[index + 1];

return nextItem && history.push(videoUrl(nextItem, searchParams.get('r'), true));
};

const onShareClick = (): void => {
if (!item) return;

Expand Down Expand Up @@ -93,26 +102,27 @@ const Movie = ({
<meta property="og:video:tag" content={tag} key={tag} />
))}
</Helmet>
<VideoComponent
title={item.title}
item={item}
trailerItem={trailerItem}
play={play}
startPlay={startPlay}
goBack={goBack}
poster={posterFading ? 'fading' : 'normal'}
enableSharing={enableSharing}
hasShared={hasShared}
onShareClick={onShareClick}
playTrailer={playTrailer}
onTrailerClick={() => setPlayTrailer(true)}
onTrailerClose={() => setPlayTrailer(false)}
isFavorited={isFavorited}
onFavoriteButtonClick={() => (isFavorited ? removeItem(item) : saveItem(item))}
>
{config.recommendationsPlaylist ? (
<PlaylistContainer playlistId={config.recommendationsPlaylist} relatedMediaId={item.mediaid}>
{({ playlist, isLoading }) => (
<PlaylistContainer playlistId={config?.recommendationsPlaylist || ''} relatedItem={item}>
{({ playlist, isLoading }) => (
<VideoComponent
title={item.title}
item={item}
trailerItem={trailerItem}
play={play}
startPlay={startPlay}
goBack={goBack}
onComplete={() => playNext(playlist.playlist)}
poster={posterFading ? 'fading' : 'normal'}
enableSharing={enableSharing}
hasShared={hasShared}
onShareClick={onShareClick}
playTrailer={playTrailer}
onTrailerClick={() => setPlayTrailer(true)}
onTrailerClose={() => setPlayTrailer(false)}
isFavorited={isFavorited}
onFavoriteButtonClick={() => (isFavorited ? removeItem(item) : saveItem(item))}
>
{config.recommendationsPlaylist ? (
<>
<div className={styles.related}>
<h3>{playlist.title}</h3>
Expand All @@ -125,10 +135,10 @@ const Movie = ({
currentCardLabel={t('currently_playing')}
/>
</>
)}
</PlaylistContainer>
) : undefined}
</VideoComponent>
) : undefined}
</VideoComponent>
)}
</PlaylistContainer>
</React.Fragment>
);
};
Expand Down

0 comments on commit 7564a88

Please sign in to comment.