Add button to get episode details / torrents

This commit is contained in:
Grégoire Delattre 2017-02-01 13:29:15 +01:00
parent dcde20f170
commit 425bfdc8d3
3 changed files with 82 additions and 0 deletions

View File

@ -188,6 +188,25 @@ export function getShowDetails(imdbId) {
)
}
export function getEpisodeDetails(imdbId, season, episode) {
return request(
'EPISODE_GET_DETAILS',
configureAxios().post(`/shows/${imdbId}/seasons/${season}/episodes/${episode}`),
)
}
export function updateEpisodeDetailsStore(imdbId, season, episode) {
return {
type: 'EPISODE_GET_DETAILS',
payload: {
imdbId,
season,
episode,
},
}
}
export function fetchShowDetails(imdbId) {
return request(
'SHOW_FETCH_DETAILS',

View File

@ -23,6 +23,8 @@ export default class ShowDetails extends React.Component {
data={this.props.showStore.show}
addTorrent={this.props.addTorrent}
addToWishlist={this.props.addShowToWishlist}
getEpisodeDetails={this.props.getEpisodeDetails}
updateEpisodeDetailsStore={this.props.updateEpisodeDetailsStore}
/>
</div>
);
@ -94,6 +96,8 @@ function SeasonsList(props){
data={season}
addTorrent={props.addTorrent}
addToWishlist={props.addToWishlist}
getEpisodeDetails={props.getEpisodeDetails}
updateEpisodeDetailsStore={props.updateEpisodeDetailsStore}
/>
</div>
)
@ -138,6 +142,8 @@ class Season extends React.Component {
data={episode}
addTorrent={this.props.addTorrent}
addToWishlist={this.props.addToWishlist}
getEpisodeDetails={this.props.getEpisodeDetails}
updateEpisodeDetailsStore={this.props.updateEpisodeDetailsStore}
/>
)
}, this)}
@ -173,6 +179,11 @@ function Episode(props) {
)
})}
<DownloadButton data={props.data}/>
<GetDetailsButton
data={props.data}
getEpisodeDetails={props.getEpisodeDetails}
updateEpisodeDetailsStore={props.updateEpisodeDetailsStore}
/>
</span>
</td>
</tr>
@ -297,3 +308,36 @@ function DownloadButton(props) {
</span>
);
}
class GetDetailsButton extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(e, url) {
e.preventDefault();
if (this.props.data.fetching) {
return
}
this.props.updateEpisodeDetailsStore(this.props.data.show_imdb_id, this.props.data.season, this.props.data.episode);
this.props.getEpisodeDetails(this.props.data.show_imdb_id, this.props.data.season, this.props.data.episode);
}
render() {
return (
<span className="episode-button">
<a type="button" className="btn btn-xs btn-info" onClick={(e) => this.handleClick(e)}>
{this.props.data.fetching ||
<span>
<i className="fa fa-refresh"></i> Refresh
</span>
}
{this.props.data.fetching &&
<span>
<i className="fa fa-spin fa-refresh"></i> Refreshing
</span>
}
</a>
</span>
);
}
}

View File

@ -46,6 +46,14 @@ export default function showStore(state = defaultState, action) {
show: sortEpisodes(action.payload.data),
loading: false,
})
case 'EPISODE_GET_DETAILS':
return Object.assign({}, state, {
show: updateEpisode(Object.assign({}, state.show), true, action.payload),
})
case 'EPISODE_GET_DETAILS_FULFILLED':
return Object.assign({}, state, {
show: updateEpisode(Object.assign({}, state.show), false, action.payload.data),
})
case 'SEARCH_SHOWS_PENDING':
return Object.assign({}, state, {
loading: true,
@ -74,6 +82,16 @@ export default function showStore(state = defaultState, action) {
}
}
function updateEpisode(show, fetching, data) {
let seasonIndex = show.seasons.map((el) => el.season).indexOf(data.season.toString());
let episodeIndex = show.seasons[seasonIndex].episodes.map((el) => el.episode).indexOf(data.episode);
if ('id' in data) {
show.seasons[seasonIndex].episodes[episodeIndex] = data;
}
show.seasons[seasonIndex].episodes[episodeIndex].fetching = fetching;
return show
}
function sortEpisodes(show) {
let episodes = show.episodes;
delete show["episodes"];
@ -85,6 +103,7 @@ function sortEpisodes(show) {
// Extract the seasons
let seasons = {};
for (let ep of episodes) {
ep.fetching = false;
if (!seasons[ep.season]) {
seasons[ep.season] = { episodes: [] };
}