Refactor the SubtitlesButton component

This commit is contained in:
Grégoire Delattre 2017-06-03 15:40:58 +02:00
parent f54ff38039
commit 86f0377d92
3 changed files with 30 additions and 74 deletions

View File

@ -17,71 +17,25 @@ export default class SubtitlesButton extends React.PureComponent {
this.props.refreshSubtitles(this.props.type, this.props.resourceID, this.props.season, this.props.episode); this.props.refreshSubtitles(this.props.type, this.props.resourceID, this.props.season, this.props.episode);
} }
render() { render() {
// If there is no URL, the resource is not in polochon, we won't be able to download subtitles const btnSize = this.props.xs ? "xsmall" : "small";
if (this.props.url === "") { const subtitles = this.props.subtitles;
return null; const hasSubtitles = (subtitles !== undefined && subtitles !== null);
}
// Build the button
const entries = buildMenuItems(this.props.subtitles);
let btnSize = "small";
if (this.props.xs) {
btnSize = "xsmall";
}
return ( return (
<DropdownButton bsStyle="success" bsSize={btnSize} title="Subtitles" id="download-subtitles-button" dropup> <DropdownButton bsStyle="success" bsSize={btnSize} title="Subtitles" id="download-subtitles-button" dropup>
{entries.map(function(e, index) { <MenuItem onClick={(ev) => this.handleClick(ev)}>
switch (e.type) {
case "action":
return (
<MenuItem key={index} onClick={(event) => this.handleClick(event, e.url)}>
<RefreshIndicator refresh={this.props.fetching} /> <RefreshIndicator refresh={this.props.fetching} />
</MenuItem> </MenuItem>
); {hasSubtitles &&
case "divider": <MenuItem divider></MenuItem>
}
{hasSubtitles && subtitles.toIndexedSeq().map(function(subtitle, index) {
return ( return (
<MenuItem key={index} divider></MenuItem> <MenuItem key={index} href={subtitle.get("url")}>
); <i className="fa fa-download"></i> &nbsp;{subtitle.get("language").split("_")[1]}
case "entry":
return (
<MenuItem key={index} href={e.url}>
<i className="fa fa-download"></i> {e.lang}
</MenuItem> </MenuItem>
); );
} })}
}, this)}
</DropdownButton> </DropdownButton>
); );
} }
} }
function buildMenuItems(subtitles) {
// Build the array of entries
let entries = [];
// Push the refresh button
entries.push({
type: "action",
value: "Refresh",
});
// If there is no subtitles, stop here
if (subtitles == undefined) {
return entries;
}
// Push the divider
entries.push({ type: "divider" });
// Push the subtitles
for (let sub of subtitles) {
entries.push({
type: "entry",
// Take only the last part of fr_FR
lang: sub.get("language").split("_")[1],
url: sub.get("url"),
});
}
return entries;
}

View File

@ -57,14 +57,15 @@ function MovieButtons(props) {
subtitles={props.movie.get("subtitles")} subtitles={props.movie.get("subtitles")}
/> />
{props.movie.get("polochon_url") !== null &&
<SubtitlesButton <SubtitlesButton
fetching={props.movie.get("fetchingSubtitles")} fetching={props.movie.get("fetchingSubtitles")}
url={props.movie.get("polochon_url")}
subtitles={props.movie.get("subtitles")} subtitles={props.movie.get("subtitles")}
refreshSubtitles={props.refreshSubtitles} refreshSubtitles={props.refreshSubtitles}
resourceID={props.movie.get("imdb_id")} resourceID={props.movie.get("imdb_id")}
type="movie" type="movie"
/> />
}
<ImdbButton imdbId={props.movie.get("imdb_id")} size="sm"/> <ImdbButton imdbId={props.movie.get("imdb_id")} size="sm"/>
</div> </div>

View File

@ -187,8 +187,8 @@ function Episode(props) {
{props.data.get("title")} {props.data.get("title")}
<span className="pull-right episode-buttons"> <span className="pull-right episode-buttons">
{props.data.get("polochon_url") !== "" &&
<SubtitlesButton <SubtitlesButton
url={props.data.get("polochon_url")}
subtitles={props.data.get("subtitles")} subtitles={props.data.get("subtitles")}
refreshSubtitles={props.refreshSubtitles} refreshSubtitles={props.refreshSubtitles}
resourceID={props.data.get("show_imdb_id")} resourceID={props.data.get("show_imdb_id")}
@ -197,6 +197,7 @@ function Episode(props) {
type="episode" type="episode"
xs xs
/> />
}
{props.data.get("torrents") && props.data.get("torrents").toList().map(function(torrent) { {props.data.get("torrents") && props.data.get("torrents").toList().map(function(torrent) {
let key = `${props.data.get("season")}-${props.data.get("episode")}-${torrent.get("source")}-${torrent.get("quality")}`; let key = `${props.data.get("season")}-${props.data.get("episode")}-${torrent.get("source")}-${torrent.get("quality")}`;
return ( return (