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

101 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 { createThunk, handleThunks } from 'Store/thunks';
import createAjaxRequest from 'Utilities/createAjaxRequest';
2018-11-23 07:04:42 +00:00
import { set, update } from './baseActions';
import createHandleActions from './Creators/createHandleActions';
2018-11-23 07:04:42 +00:00
//
// Variables
export const section = 'movieHistory';
//
// State
export const defaultState = {
isFetching: false,
isPopulated: false,
error: null,
items: []
};
//
// Actions Types
export const FETCH_MOVIE_HISTORY = 'movieHistory/fetchMovieHistory';
export const CLEAR_MOVIE_HISTORY = 'movieHistory/clearMovieHistory';
export const MOVIE_HISTORY_MARK_AS_FAILED = 'movieHistory/movieHistoryMarkAsFailed';
2018-11-23 07:04:42 +00:00
//
// Action Creators
export const fetchMovieHistory = createThunk(FETCH_MOVIE_HISTORY);
export const clearMovieHistory = createAction(CLEAR_MOVIE_HISTORY);
export const movieHistoryMarkAsFailed = createThunk(MOVIE_HISTORY_MARK_AS_FAILED);
2018-11-23 07:04:42 +00:00
//
// Action Handlers
export const actionHandlers = handleThunks({
[FETCH_MOVIE_HISTORY]: function(getState, payload, dispatch) {
2018-11-23 07:04:42 +00:00
dispatch(set({ section, isFetching: true }));
const promise = createAjaxRequest({
url: '/history/movie',
2018-11-23 07:04:42 +00:00
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
}));
});
},
[MOVIE_HISTORY_MARK_AS_FAILED]: function(getState, payload, dispatch) {
2018-11-23 07:04:42 +00:00
const {
historyId,
movieId
2018-11-23 07:04:42 +00:00
} = payload;
const promise = createAjaxRequest({
url: `/history/failed/${historyId}`,
2021-10-21 20:04:19 +00:00
method: 'POST',
dataType: 'json'
}).request;
2018-11-23 07:04:42 +00:00
promise.done(() => {
dispatch(fetchMovieHistory({ movieId }));
2018-11-23 07:04:42 +00:00
});
}
});
//
// Reducers
export const reducers = createHandleActions({
[CLEAR_MOVIE_HISTORY]: (state) => {
2018-11-23 07:04:42 +00:00
return Object.assign({}, state, defaultState);
}
}, defaultState, section);