You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
264 lines
5.3 KiB
264 lines
5.3 KiB
import _ from 'lodash';
|
|
import { createAction } from 'redux-actions';
|
|
// import { batchActions } from 'redux-batched-actions';
|
|
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
|
import dateFilterPredicate from 'Utilities/Date/dateFilterPredicate';
|
|
import { filterTypePredicates, filterTypes, sortDirections } from 'Helpers/Props';
|
|
import { createThunk, handleThunks } from 'Store/thunks';
|
|
import createSetSettingValueReducer from './Creators/Reducers/createSetSettingValueReducer';
|
|
import createFetchHandler from './Creators/createFetchHandler';
|
|
import createSaveProviderHandler from './Creators/createSaveProviderHandler';
|
|
import createRemoveItemHandler from './Creators/createRemoveItemHandler';
|
|
import createHandleActions from './Creators/createHandleActions';
|
|
import { updateItem } from './baseActions';
|
|
|
|
//
|
|
// Variables
|
|
|
|
export const section = 'movies';
|
|
|
|
export const filters = [
|
|
{
|
|
key: 'all',
|
|
label: 'All',
|
|
filters: []
|
|
},
|
|
{
|
|
key: 'monitored',
|
|
label: 'Monitored Only',
|
|
filters: [
|
|
{
|
|
key: 'monitored',
|
|
value: true,
|
|
type: filterTypes.EQUAL
|
|
}
|
|
]
|
|
},
|
|
{
|
|
key: 'unmonitored',
|
|
label: 'Unmonitored Only',
|
|
filters: [
|
|
{
|
|
key: 'monitored',
|
|
value: false,
|
|
type: filterTypes.EQUAL
|
|
}
|
|
]
|
|
},
|
|
{
|
|
key: 'wanted',
|
|
label: 'Wanted Missing',
|
|
filters: [
|
|
{
|
|
key: 'monitored',
|
|
value: true,
|
|
type: filterTypes.EQUAL
|
|
},
|
|
{
|
|
key: 'hasFile',
|
|
value: false,
|
|
type: filterTypes.EQUAL
|
|
}
|
|
]
|
|
},
|
|
{
|
|
key: 'cutoffunmet',
|
|
label: 'Cut-off Unmet',
|
|
filters: [
|
|
{
|
|
key: 'monitored',
|
|
value: true,
|
|
type: filterTypes.EQUAL
|
|
},
|
|
{
|
|
key: 'hasFile',
|
|
value: true,
|
|
type: filterTypes.EQUAL
|
|
},
|
|
{
|
|
key: 'movieFile.qualityCutoffNotMet',
|
|
value: true,
|
|
type: filterTypes.EQUAL
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
export const filterPredicates = {
|
|
missing: function(item) {
|
|
const { statistics = {} } = item;
|
|
|
|
return statistics.episodeCount - statistics.episodeFileCount > 0;
|
|
},
|
|
|
|
added: function(item, filterValue, type) {
|
|
return dateFilterPredicate(item.added, filterValue, type);
|
|
},
|
|
|
|
ratings: function(item, filterValue, type) {
|
|
const predicate = filterTypePredicates[type];
|
|
|
|
return predicate(item.ratings.value * 10, filterValue);
|
|
}
|
|
};
|
|
|
|
export const sortPredicates = {
|
|
status: function(item) {
|
|
let result = 0;
|
|
|
|
if (item.monitored) {
|
|
result += 4;
|
|
}
|
|
|
|
if (item.status === 'announced') {
|
|
result++;
|
|
}
|
|
|
|
if (item.status === 'inCinemas') {
|
|
result += 2;
|
|
}
|
|
|
|
if (item.status === 'released') {
|
|
result += 3;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
};
|
|
|
|
//
|
|
// State
|
|
|
|
export const defaultState = {
|
|
isFetching: false,
|
|
isPopulated: false,
|
|
error: null,
|
|
isSaving: false,
|
|
saveError: null,
|
|
items: [],
|
|
sortKey: 'sortTitle',
|
|
sortDirection: sortDirections.ASCENDING,
|
|
pendingChanges: {}
|
|
};
|
|
|
|
//
|
|
// Actions Types
|
|
|
|
export const FETCH_MOVIES = 'movies/fetchMovies';
|
|
export const SET_MOVIE_VALUE = 'movies/setMovieValue';
|
|
export const SAVE_MOVIE = 'movies/saveMovie';
|
|
export const DELETE_MOVIE = 'movies/deleteMovie';
|
|
|
|
export const TOGGLE_MOVIE_MONITORED = 'movies/toggleMovieMonitored';
|
|
|
|
//
|
|
// Action Creators
|
|
|
|
export const fetchMovies = createThunk(FETCH_MOVIES);
|
|
export const saveMovie = createThunk(SAVE_MOVIE, (payload) => {
|
|
const newPayload = {
|
|
...payload
|
|
};
|
|
|
|
if (payload.moveFiles) {
|
|
newPayload.queryParams = {
|
|
moveFiles: true
|
|
};
|
|
}
|
|
|
|
delete newPayload.moveFiles;
|
|
|
|
return newPayload;
|
|
});
|
|
|
|
export const deleteMovie = createThunk(DELETE_MOVIE, (payload) => {
|
|
return {
|
|
...payload,
|
|
queryParams: {
|
|
deleteFiles: payload.deleteFiles
|
|
}
|
|
};
|
|
});
|
|
|
|
export const toggleMovieMonitored = createThunk(TOGGLE_MOVIE_MONITORED);
|
|
|
|
export const setMovieValue = createAction(SET_MOVIE_VALUE, (payload) => {
|
|
return {
|
|
section,
|
|
...payload
|
|
};
|
|
});
|
|
|
|
//
|
|
// Helpers
|
|
|
|
function getSaveAjaxOptions({ ajaxOptions, payload }) {
|
|
if (payload.moveFolder) {
|
|
ajaxOptions.url = `${ajaxOptions.url}?moveFolder=true`;
|
|
}
|
|
|
|
return ajaxOptions;
|
|
}
|
|
|
|
//
|
|
// Action Handlers
|
|
|
|
export const actionHandlers = handleThunks({
|
|
|
|
[FETCH_MOVIES]: createFetchHandler(section, '/movie'),
|
|
[SAVE_MOVIE]: createSaveProviderHandler(section, '/movie', { getAjaxOptions: getSaveAjaxOptions }),
|
|
[DELETE_MOVIE]: createRemoveItemHandler(section, '/movie'),
|
|
|
|
[TOGGLE_MOVIE_MONITORED]: (getState, payload, dispatch) => {
|
|
const {
|
|
movieId: id,
|
|
monitored
|
|
} = payload;
|
|
|
|
const movie = _.find(getState().movies.items, { id });
|
|
|
|
dispatch(updateItem({
|
|
id,
|
|
section,
|
|
isSaving: true
|
|
}));
|
|
|
|
const promise = createAjaxRequest({
|
|
url: `/movie/${id}`,
|
|
method: 'PUT',
|
|
data: JSON.stringify({
|
|
...movie,
|
|
monitored
|
|
}),
|
|
dataType: 'json'
|
|
}).request;
|
|
|
|
promise.done((data) => {
|
|
dispatch(updateItem({
|
|
id,
|
|
section,
|
|
isSaving: false,
|
|
monitored
|
|
}));
|
|
});
|
|
|
|
promise.fail((xhr) => {
|
|
dispatch(updateItem({
|
|
id,
|
|
section,
|
|
isSaving: false
|
|
}));
|
|
});
|
|
}
|
|
|
|
});
|
|
|
|
//
|
|
// Reducers
|
|
|
|
export const reducers = createHandleActions({
|
|
|
|
[SET_MOVIE_VALUE]: createSetSettingValueReducer(section)
|
|
|
|
}, defaultState, section);
|