Skip to content

Commit

Permalink
TV Seasons and Episodes support added
Browse files Browse the repository at this point in the history
  • Loading branch information
afzaalb committed Feb 3, 2023
1 parent 7203785 commit 07d55fc
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 6 deletions.
59 changes: 59 additions & 0 deletions src/components/season/EpisodesList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from "react";
import { Calendar } from "react-bytesize-icons";
import Img from "react-cool-img";
import { Link } from "react-router-dom";
import { COVER_URL, FALLBACK_IMAGE } from "../../constants";

const EpisodesList = ({ showName, tvId, seasonNumber, episodes }) =>
episodes && episodes.length > 0 ? (
<article>
<h6>Episodes</h6>
<div className="row">
{episodes.map((episode, i) => {
const { name, overview, episode_number, still_path, air_date } =
episode;
return (
<Link
key={i}
to={`/tv/${tvId}/${showName}/seasons/${seasonNumber}/episodes/${episode_number}`}
className="col-sm-12 col-xs-12"
>
<div className="d-flex rounded-card">
<div className="py-2 px-2">
<Img
src={`${COVER_URL + still_path}`}
error={FALLBACK_IMAGE}
className="rounded"
width={144}
alt={name}
/>
</div>
<div className="flex-1-1-a py-2 px-3">
<h6 className="d-flex align-items-center">
<span className="mr-2">{name}</span>
<span className="flex-shrink-0 pill px-3 py-1 mr-2 d-flex align-items-center">
<Calendar
className="align-top mr-2"
width="14"
height="14"
strokeWidth="2"
/>
<span>Aired {air_date}</span>
</span>
<small className="flex-shrink-0 ml-auto text-muted">
Episode {episode_number}
</small>
</h6>
<p className="m-0">{overview}</p>
</div>
</div>
</Link>
);
})}
</div>
</article>
) : (
""
);

export default EpisodesList;
4 changes: 2 additions & 2 deletions src/components/season/SeasonsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const SeasonsList = ({ showName, tvId, seasons }) =>
<div className="flex-1-1-a py-2 px-3">
<h6 className="d-flex align-items-center">
<span className="mr-2">{name}</span>
<span className="pill px-3 py-1 mr-2 d-flex align-items-center">
<span className="ml-auto pill px-3 py-1 mr-2 d-flex align-items-center">
<Calendar
className="align-top mr-2"
width="14"
Expand All @@ -42,7 +42,7 @@ const SeasonsList = ({ showName, tvId, seasons }) =>
<span>Aired {air_date}</span>
</span>
</h6>
<p>{overview}</p>
<p className="m-0">{overview}</p>
</div>
</div>
</Link>
Expand Down
171 changes: 171 additions & 0 deletions src/containers/episode-item/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import React, { Component } from "react";
import { getItemTrailer } from "../../utils";
import theMovieDb from "themoviedb-javascript-library";
import isEmpty from "lodash/isEmpty";
import NoDataFound from "../../components/shared/NoDataFound";
import Loader from "../../components/shared/Loader";
import Player from "../../components/item/Player";
import ItemDetails from "../../components/item/ItemDetails";
import GA from "react-ga";

class EpisodeItem extends Component {
constructor(props) {
super(props);
this.state = {
playing: false,
itemDetails: {},
loading: true,
};
}

componentDidMount() {
const { id, number, episode } = this.props.match.params;
this.getItemById(id, number, episode);
}

componentDidUpdate(prevProps) {
const { id, number, episode } = this.props.match.params;
if (id !== prevProps.match.params.id) {
this.getItemById(id, number, episode);
}
}

getItemById = (id, season_number, episode_number) => {
theMovieDb.tvEpisodes.getById(
{
id,
season_number,
episode_number,
append_to_response:
"videos,recommendations,images&include_image_language=en,null",
},
(data) => this.successCB(data, id, season_number, episode_number),
this.errorCB
);
};

successCB = (data, id, season_number, episode_number) => {
const fetchedData = JSON.parse(data);
this.setState(
{
loading: false,
itemDetails: fetchedData,
},
() => {
theMovieDb.tv.getCredits(
{ id, season_number, episode_number },
this.creditsSuccessCB,
this.errorCB
);
}
);
};

errorCB = (data) => {
if (data) {
this.setState({
loading: false,
tmdbResponse: JSON.parse(data).status_message,
});
} else {
this.setState({
loading: false,
itemDetails: {},
});
}
};

creditsSuccessCB = (data) => {
const { itemDetails } = this.state;
const fetchedData = JSON.parse(data);
this.setState({
itemDetails: { ...itemDetails, casts: fetchedData },
});
};

handlePlayerState = () => {
const { playing } = this.state;
this.setState(
{
playing: !playing,
},
() => {
this.state.playing &&
GA.event({
category: "Player",
action: "Play trailer for tv episode",
});
}
);
};

render() {
const { playing, itemDetails, tmdbResponse, loading } = this.state;
const {
name,
status,
overview,
vote_average,
first_air_date,
still_path,
backdrop_path,
genres,
homepage,
budget,
revenue,
runtime,
imdb_id,
videos,
images,
production_companies,
} = itemDetails;

if (!isEmpty(itemDetails)) {
var { stills } = images;
var { results } = videos;
}

if (!isEmpty(itemDetails.casts)) {
var { cast, crew } = itemDetails.casts;
}

return !isEmpty(itemDetails) ? (
<>
{results.length > 0 && (
<Player
videoId={getItemTrailer(results)}
poster={backdrop_path ? backdrop_path : still_path}
title={name}
playing={playing}
handlePlayerState={this.handlePlayerState}
/>
)}
<ItemDetails
id={this.props.match.params.id}
title={name}
release={first_air_date}
genres={genres}
runtime={runtime}
overview={overview}
rating={vote_average}
poster={still_path}
status={status}
link={homepage}
budget={budget}
revenue={revenue}
imdb={imdb_id}
backdrops={stills}
cast={cast}
crew={crew}
productionCompanies={production_companies}
/>
</>
) : tmdbResponse ? (
<NoDataFound alignCenter spaceTop message={tmdbResponse} />
) : loading ? (
<Loader spaceTop />
) : null;
}
}

export default EpisodeItem;
15 changes: 11 additions & 4 deletions src/containers/season-item/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { Component, lazy } from "react";
import React, { Component } from "react";
import { getItemTrailer } from "../../utils";
import theMovieDb from "themoviedb-javascript-library";
import isEmpty from "lodash/isEmpty";
import NoDataFound from "../../components/shared/NoDataFound";
import Loader from "../../components/shared/Loader";
import Player from "../../components/item/Player";
import ItemDetails from "../../components/item/ItemDetails";
import EpisodesList from "../../components/season/EpisodesList";
import GA from "react-ga";

class SeasonItem extends Component {
Expand Down Expand Up @@ -115,7 +116,7 @@ class SeasonItem extends Component {
imdb_id,
videos,
images,
seasons,
episodes,
production_companies,
} = itemDetails;

Expand Down Expand Up @@ -153,12 +154,18 @@ class SeasonItem extends Component {
budget={budget}
revenue={revenue}
imdb={imdb_id}
seasons={seasons}
backdrops={posters || backdrops}
cast={cast}
crew={crew}
productionCompanies={production_companies}
/>
>
<EpisodesList
showName={this.props.match.params.name}
tvId={this.props.match.params.id}
seasonNumber={this.props.match.params.number}
episodes={episodes}
/>
</ItemDetails>
</>
) : tmdbResponse ? (
<NoDataFound alignCenter spaceTop message={tmdbResponse} />
Expand Down
6 changes: 6 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MovieItem from "../containers/movie-item";
import TvShows from "../containers/tv-shows";
import TvItem from "../containers/tv-item";
import SeasonItem from "../containers/season-item";
import EpisodeItem from "../containers/episode-item";
import Person from "../containers/person";
import Search from "../containers/search";
import Settings from "../containers/settings";
Expand Down Expand Up @@ -40,6 +41,11 @@ const MyRouter = () => (
path="/tv/:id/:name/seasons/:number"
render={renderComponent(SeasonItem)}
/>
<Route
exact
path="/tv/:id/:name/seasons/:number/episodes/:episode"
render={renderComponent(EpisodeItem)}
/>

{/* Other Features */}
<Route exact path="/search" render={renderComponent(Search)} />
Expand Down

0 comments on commit 07d55fc

Please sign in to comment.