Split the store of the show
This commit is contained in:
parent
e53306686f
commit
33137e0035
@ -3,7 +3,7 @@ import { connect } from 'react-redux'
|
||||
import { bindActionCreators } from 'redux'
|
||||
import { addTorrent } from '../../actions/torrents'
|
||||
import { refreshSubtitles } from '../../actions/subtitles'
|
||||
import { addShowToWishlist, deleteFromWishlist, getEpisodeDetails,
|
||||
import { addShowToWishlist, deleteShowFromWishlist, getEpisodeDetails,
|
||||
updateEpisodeDetailsStore, updateShowDetails } from '../../actions/shows'
|
||||
|
||||
import Loader from '../loader/loader'
|
||||
@ -19,7 +19,7 @@ function mapStateToProps(state) {
|
||||
};
|
||||
}
|
||||
const mapDispatchToProps = (dispatch) =>
|
||||
bindActionCreators({addTorrent, addShowToWishlist, deleteFromWishlist,
|
||||
bindActionCreators({addTorrent, addShowToWishlist, deleteShowFromWishlist,
|
||||
updateShowDetails, updateEpisodeDetailsStore, getEpisodeDetails,
|
||||
refreshSubtitles }, dispatch)
|
||||
|
||||
|
@ -2,23 +2,23 @@ import React from 'react'
|
||||
import { connect } from 'react-redux'
|
||||
import { bindActionCreators } from 'redux'
|
||||
import { selectShow, addShowToWishlist,
|
||||
deleteFromWishlist, getShowDetails } from '../../actions/shows'
|
||||
deleteShowFromWishlist, getShowDetails } from '../../actions/shows'
|
||||
|
||||
import ListDetails from '../list/details'
|
||||
import ListPosters from '../list/posters'
|
||||
import ShowButtons from './listButtons'
|
||||
|
||||
function mapStateToProps(state) {
|
||||
return { showStore: state.showStore };
|
||||
return { showsStore: state.showsStore };
|
||||
}
|
||||
const mapDispatchToProps = (dispatch) =>
|
||||
bindActionCreators({ selectShow, addShowToWishlist,
|
||||
deleteFromWishlist, getShowDetails }, dispatch)
|
||||
deleteShowFromWishlist, getShowDetails }, dispatch)
|
||||
|
||||
class ShowList extends React.Component {
|
||||
render() {
|
||||
const shows = this.props.showStore.shows;
|
||||
const selectedShowId = this.props.showStore.selectedImdbId;
|
||||
const shows = this.props.showsStore.shows;
|
||||
const selectedShowId = this.props.showsStore.selectedImdbId;
|
||||
let index = shows.map((el) => el.imdb_id).indexOf(selectedShowId);
|
||||
if (index === -1) {
|
||||
index = 0;
|
||||
@ -30,17 +30,17 @@ class ShowList extends React.Component {
|
||||
<ListPosters
|
||||
data={shows}
|
||||
type="shows"
|
||||
formModel="showStore"
|
||||
filterControlModel="showStore.filter"
|
||||
formModel="showsStore"
|
||||
filterControlModel="showsStore.filter"
|
||||
filterControlPlaceHolder="Filter shows..."
|
||||
exploreOptions={this.props.showStore.exploreOptions}
|
||||
exploreOptions={this.props.showsStore.exploreOptions}
|
||||
selectedImdbId={selectedShowId}
|
||||
filter={this.props.showStore.filter}
|
||||
perPage={this.props.showStore.perPage}
|
||||
filter={this.props.showsStore.filter}
|
||||
perPage={this.props.showsStore.perPage}
|
||||
onClick={this.props.selectShow}
|
||||
router={this.props.router}
|
||||
params={this.props.params}
|
||||
loading={this.props.showStore.loading}
|
||||
loading={this.props.showsStore.loading}
|
||||
/>
|
||||
{selectedShow &&
|
||||
<ListDetails data={selectedShow} >
|
||||
@ -49,7 +49,7 @@ class ShowList extends React.Component {
|
||||
deleteFromWishlist={this.props.deleteShowFromWishlist}
|
||||
addToWishlist={this.props.addShowToWishlist}
|
||||
getDetails={this.props.getShowDetails}
|
||||
fetching={this.props.showStore.getDetails}
|
||||
fetching={this.props.showsStore.getDetails}
|
||||
/>
|
||||
</ListDetails>
|
||||
}
|
||||
|
@ -2,7 +2,8 @@ import { combineForms } from 'react-redux-form'
|
||||
import { routerReducer } from 'react-router-redux'
|
||||
|
||||
import movieStore from './movies'
|
||||
import showStore from './shows'
|
||||
import showsStore from './shows'
|
||||
import showStore from './show'
|
||||
import userStore from './users'
|
||||
import alerts from './alerts'
|
||||
import torrentStore from './torrents'
|
||||
@ -13,6 +14,7 @@ import torrentStore from './torrents'
|
||||
const rootReducer = combineForms({
|
||||
routing: routerReducer,
|
||||
movieStore,
|
||||
showsStore,
|
||||
showStore,
|
||||
userStore,
|
||||
alerts,
|
||||
|
139
src/public/js/reducers/show.js
Normal file
139
src/public/js/reducers/show.js
Normal file
@ -0,0 +1,139 @@
|
||||
const defaultState = {
|
||||
loading: false,
|
||||
show: {
|
||||
seasons: [],
|
||||
},
|
||||
};
|
||||
|
||||
export default function showStore(state = defaultState, action) {
|
||||
switch (action.type) {
|
||||
case 'SHOW_FETCH_DETAILS_PENDING':
|
||||
return Object.assign({}, state, {
|
||||
loading: true,
|
||||
})
|
||||
case 'SHOW_FETCH_DETAILS_FULFILLED':
|
||||
return Object.assign({}, state, {
|
||||
show: sortEpisodes(action.payload.response.data),
|
||||
loading: false,
|
||||
})
|
||||
case 'SHOW_UPDATE_STORE_WISHLIST':
|
||||
return Object.assign({}, state, {
|
||||
show: updateShowStoreWishlist(Object.assign({}, state.show), action.payload),
|
||||
})
|
||||
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.response.data),
|
||||
})
|
||||
case 'EPISODE_SUBTITLES_UPDATE_PENDING':
|
||||
return Object.assign({}, state, {
|
||||
show: updateEpisodeSubtitles(Object.assign({}, state.show), action.payload.main.season, action.payload.main.episode, true),
|
||||
})
|
||||
case 'EPISODE_SUBTITLES_UPDATE_FULFILLED':
|
||||
console.log("payload :", action.payload);
|
||||
return Object.assign({}, state, {
|
||||
show: updateEpisodeSubtitles(Object.assign({}, state.show), action.payload.main.season, action.payload.main.episode, false, action.payload.response.data),
|
||||
})
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
function updateEpisode(show, fetching, data = null) {
|
||||
// Error handling for PouuleT
|
||||
if (data === null) {
|
||||
for (let seasonIndex of Object.keys(show.seasons)) {
|
||||
for (let episodeIndex of Object.keys(show.seasons[seasonIndex].episodes)) {
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].fetching = false;
|
||||
}
|
||||
}
|
||||
return show
|
||||
}
|
||||
|
||||
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 ('imdb_id' in data) {
|
||||
show.seasons[seasonIndex].episodes[episodeIndex] = data;
|
||||
}
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].fetching = fetching;
|
||||
return show
|
||||
}
|
||||
|
||||
function updateEpisodeSubtitles(show, season, episode, fetching, data = null) {
|
||||
let seasonIndex = show.seasons.map((el) => el.season).indexOf(season.toString());
|
||||
let episodeIndex = show.seasons[seasonIndex].episodes.map((el) => el.episode).indexOf(episode);
|
||||
|
||||
if (data) {
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].subtitles = data;
|
||||
}
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].fetchingSubtitles = fetching;
|
||||
return show
|
||||
}
|
||||
|
||||
function sortEpisodes(show) {
|
||||
let episodes = show.episodes;
|
||||
delete show["episodes"];
|
||||
|
||||
if (episodes.length == 0) {
|
||||
return show;
|
||||
}
|
||||
|
||||
// Extract the seasons
|
||||
let seasons = {};
|
||||
for (let ep of episodes) {
|
||||
ep.fetching = false;
|
||||
if (!seasons[ep.season]) {
|
||||
seasons[ep.season] = { episodes: [] };
|
||||
}
|
||||
seasons[ep.season].episodes.push(ep);
|
||||
}
|
||||
|
||||
if (seasons.length === 0) {
|
||||
return show;
|
||||
}
|
||||
|
||||
// Put all the season in an array
|
||||
let sortedSeasons = [];
|
||||
for (let season of Object.keys(seasons)) {
|
||||
let seasonEpisodes = seasons[season].episodes;
|
||||
// Order the episodes in each season
|
||||
seasonEpisodes.sort((a,b) => (a.episode - b.episode))
|
||||
// Add the season in the list
|
||||
sortedSeasons.push({
|
||||
season: season,
|
||||
episodes: seasonEpisodes,
|
||||
})
|
||||
}
|
||||
|
||||
// Order the seasons
|
||||
for (let i=0; i<sortedSeasons.length; i++) {
|
||||
sortedSeasons.sort((a,b) => (a.season - b.season))
|
||||
}
|
||||
|
||||
show.seasons = sortedSeasons;
|
||||
|
||||
return show;
|
||||
}
|
||||
|
||||
// Update the store containing the current detailed show
|
||||
function updateShowStoreWishlist(show, payload) {
|
||||
if (show.seasons.length === 0) {
|
||||
return show;
|
||||
}
|
||||
let season = payload.season;
|
||||
let episode = payload.episode;
|
||||
if (payload.wishlisted) {
|
||||
if (season === null) {
|
||||
season = 0;
|
||||
}
|
||||
if (episode === null) {
|
||||
episode = 0;
|
||||
}
|
||||
}
|
||||
show.tracked_season = season;
|
||||
show.tracked_episode = episode;
|
||||
return show
|
||||
}
|
@ -4,15 +4,12 @@ const defaultState = {
|
||||
filter: "",
|
||||
perPage: 30,
|
||||
selectedImdbId: "",
|
||||
show: {
|
||||
seasons: [],
|
||||
},
|
||||
getDetails: false,
|
||||
lastShowsFetchUrl: "",
|
||||
exploreOptions: {},
|
||||
};
|
||||
|
||||
export default function showStore(state = defaultState, action) {
|
||||
export default function showsStore(state = defaultState, action) {
|
||||
switch (action.type) {
|
||||
case 'SHOW_LIST_FETCH_PENDING':
|
||||
return Object.assign({}, state, {
|
||||
@ -29,7 +26,6 @@ export default function showStore(state = defaultState, action) {
|
||||
shows: action.payload.response.data,
|
||||
selectedImdbId: selectedImdbId,
|
||||
filter: defaultState.filter,
|
||||
perPage: defaultState.perPage,
|
||||
loading: false,
|
||||
})
|
||||
case 'SHOW_GET_DETAILS_PENDING':
|
||||
@ -41,23 +37,6 @@ export default function showStore(state = defaultState, action) {
|
||||
shows: updateShowDetails(state.shows.slice(), action.payload.response.data),
|
||||
getDetails: false,
|
||||
})
|
||||
case 'SHOW_FETCH_DETAILS_PENDING':
|
||||
return Object.assign({}, state, {
|
||||
loading: true,
|
||||
})
|
||||
case 'SHOW_FETCH_DETAILS_FULFILLED':
|
||||
return Object.assign({}, state, {
|
||||
show: sortEpisodes(action.payload.response.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.response.data),
|
||||
})
|
||||
case 'EXPLORE_SHOWS_PENDING':
|
||||
return Object.assign({}, state, {
|
||||
loading: true,
|
||||
@ -74,21 +53,11 @@ export default function showStore(state = defaultState, action) {
|
||||
case 'SHOW_UPDATE_STORE_WISHLIST':
|
||||
return Object.assign({}, state, {
|
||||
shows: updateShowsStoreWishlist(state.shows.slice(), action.payload),
|
||||
show: updateShowStoreWishlist(Object.assign({}, state.show), action.payload),
|
||||
})
|
||||
case 'UPDATE_LAST_SHOWS_FETCH_URL':
|
||||
return Object.assign({}, state, {
|
||||
lastShowsFetchUrl: action.payload.url,
|
||||
})
|
||||
case 'EPISODE_SUBTITLES_UPDATE_PENDING':
|
||||
return Object.assign({}, state, {
|
||||
show: updateEpisodeSubtitles(Object.assign({}, state.show), action.payload.main.season, action.payload.main.episode, true),
|
||||
})
|
||||
case 'EPISODE_SUBTITLES_UPDATE_FULFILLED':
|
||||
console.log("payload :", action.payload);
|
||||
return Object.assign({}, state, {
|
||||
show: updateEpisodeSubtitles(Object.assign({}, state.show), action.payload.main.season, action.payload.main.episode, false, action.payload.response.data),
|
||||
})
|
||||
case 'SELECT_SHOW':
|
||||
// Don't select the show if we're fetching another show's details
|
||||
if (state.fetchingDetails) {
|
||||
@ -103,82 +72,6 @@ export default function showStore(state = defaultState, action) {
|
||||
}
|
||||
}
|
||||
|
||||
function updateEpisode(show, fetching, data = null) {
|
||||
// Error handling for PouuleT
|
||||
if (data === null) {
|
||||
for (let seasonIndex of Object.keys(show.seasons)) {
|
||||
for (let episodeIndex of Object.keys(show.seasons[seasonIndex].episodes)) {
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].fetching = false;
|
||||
}
|
||||
}
|
||||
return show
|
||||
}
|
||||
|
||||
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 ('imdb_id' in data) {
|
||||
show.seasons[seasonIndex].episodes[episodeIndex] = data;
|
||||
}
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].fetching = fetching;
|
||||
return show
|
||||
}
|
||||
|
||||
function updateEpisodeSubtitles(show, season, episode, fetching, data = null) {
|
||||
let seasonIndex = show.seasons.map((el) => el.season).indexOf(season.toString());
|
||||
let episodeIndex = show.seasons[seasonIndex].episodes.map((el) => el.episode).indexOf(episode);
|
||||
|
||||
if (data) {
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].subtitles = data;
|
||||
}
|
||||
show.seasons[seasonIndex].episodes[episodeIndex].fetchingSubtitles = fetching;
|
||||
return show
|
||||
}
|
||||
|
||||
function sortEpisodes(show) {
|
||||
let episodes = show.episodes;
|
||||
delete show["episodes"];
|
||||
|
||||
if (episodes.length == 0) {
|
||||
return show;
|
||||
}
|
||||
|
||||
// Extract the seasons
|
||||
let seasons = {};
|
||||
for (let ep of episodes) {
|
||||
ep.fetching = false;
|
||||
if (!seasons[ep.season]) {
|
||||
seasons[ep.season] = { episodes: [] };
|
||||
}
|
||||
seasons[ep.season].episodes.push(ep);
|
||||
}
|
||||
|
||||
if (seasons.length === 0) {
|
||||
return show;
|
||||
}
|
||||
|
||||
// Put all the season in an array
|
||||
let sortedSeasons = [];
|
||||
for (let season of Object.keys(seasons)) {
|
||||
let seasonEpisodes = seasons[season].episodes;
|
||||
// Order the episodes in each season
|
||||
seasonEpisodes.sort((a,b) => (a.episode - b.episode))
|
||||
// Add the season in the list
|
||||
sortedSeasons.push({
|
||||
season: season,
|
||||
episodes: seasonEpisodes,
|
||||
})
|
||||
}
|
||||
|
||||
// Order the seasons
|
||||
for (let i=0; i<sortedSeasons.length; i++) {
|
||||
sortedSeasons.sort((a,b) => (a.season - b.season))
|
||||
}
|
||||
|
||||
show.seasons = sortedSeasons;
|
||||
|
||||
return show;
|
||||
}
|
||||
|
||||
// Update the store containing all the shows
|
||||
function updateShowsStoreWishlist(shows, payload) {
|
||||
if (shows.length === 0) {
|
||||
@ -201,26 +94,6 @@ function updateShowsStoreWishlist(shows, payload) {
|
||||
return shows
|
||||
}
|
||||
|
||||
// Update the store containing the current detailed show
|
||||
function updateShowStoreWishlist(show, payload) {
|
||||
if (show.seasons.length === 0) {
|
||||
return show;
|
||||
}
|
||||
let season = payload.season;
|
||||
let episode = payload.episode;
|
||||
if (payload.wishlisted) {
|
||||
if (season === null) {
|
||||
season = 0;
|
||||
}
|
||||
if (episode === null) {
|
||||
episode = 0;
|
||||
}
|
||||
}
|
||||
show.tracked_season = season;
|
||||
show.tracked_episode = episode;
|
||||
return show
|
||||
}
|
||||
|
||||
function updateShowDetails(shows, data) {
|
||||
let index = shows.map((el) => el.imdb_id).indexOf(data.imdb_id);
|
||||
shows[index] = data;
|
||||
|
Loading…
x
Reference in New Issue
Block a user