Radarr/frontend/src/Store/Actions/movieHistoryActions.js

105 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-11-23 07:04:42 +00:00
import { createAction } from 'redux-actions';
import { batchActions } from 'redux-batched-actions';
import createAjaxRequest from 'Utilities/createAjaxRequest';
2018-11-23 07:04:42 +00:00
import { createThunk, handleThunks } from 'Store/thunks';
import createHandleActions from './Creators/createHandleActions';
import { set, update } from './baseActions';
//
// Variables
export const section = 'movieHistory';
//
// State
export const defaultState = {
isFetching: false,
isPopulated: false,
error: null,
items: []
};
//
// Actions Types
export const FETCH_SERIES_HISTORY = 'seriesHistory/fetchMovieHistory';
export const CLEAR_SERIES_HISTORY = 'seriesHistory/clearMovieHistory';
export const SERIES_HISTORY_MARK_AS_FAILED = 'seriesHistory/seriesHistoryMarkAsFailed';
//
// Action Creators
export const fetchMovieHistory = createThunk(FETCH_SERIES_HISTORY);
export const clearMovieHistory = createAction(CLEAR_SERIES_HISTORY);
export const seriesHistoryMarkAsFailed = createThunk(SERIES_HISTORY_MARK_AS_FAILED);
//
// Action Handlers
export const actionHandlers = handleThunks({
[FETCH_SERIES_HISTORY]: function(getState, payload, dispatch) {
dispatch(set({ section, isFetching: true }));
const promise = createAjaxRequest({
2018-11-23 07:04:42 +00:00
url: '/history/series',
data: payload
}).request;
2018-11-23 07:04:42 +00:00
promise.done((data) => {
dispatch(batchActions([
update({ section, data }),
set({
section,
isFetching: false,
isPopulated: true,
error: null
})
]));
});
promise.fail((xhr) => {
dispatch(set({
section,
isFetching: false,
isPopulated: false,
error: xhr
}));
});
},
[SERIES_HISTORY_MARK_AS_FAILED]: function(getState, payload, dispatch) {
const {
historyId,
seriesId,
seasonNumber
} = payload;
const promise = createAjaxRequest({
2018-11-23 07:04:42 +00:00
url: '/history/failed',
method: 'POST',
data: {
id: historyId
}
}).request;
2018-11-23 07:04:42 +00:00
promise.done(() => {
dispatch(fetchMovieHistory({ seriesId, seasonNumber }));
});
}
});
//
// Reducers
export const reducers = createHandleActions({
[CLEAR_SERIES_HISTORY]: (state) => {
return Object.assign({}, state, defaultState);
}
}, defaultState, section);