mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
New: Project Aphrodite
This commit is contained in:
280
frontend/src/Store/Actions/releaseActions.js
Normal file
280
frontend/src/Store/Actions/releaseActions.js
Normal file
@@ -0,0 +1,280 @@
|
||||
import $ from 'jquery';
|
||||
import { createAction } from 'redux-actions';
|
||||
import { filterBuilderTypes, filterBuilderValueTypes, filterTypes, sortDirections } from 'Helpers/Props';
|
||||
import { createThunk, handleThunks } from 'Store/thunks';
|
||||
import createSetClientSideCollectionSortReducer from './Creators/Reducers/createSetClientSideCollectionSortReducer';
|
||||
import createSetClientSideCollectionFilterReducer from './Creators/Reducers/createSetClientSideCollectionFilterReducer';
|
||||
import createFetchHandler from './Creators/createFetchHandler';
|
||||
import createHandleActions from './Creators/createHandleActions';
|
||||
|
||||
//
|
||||
// Variables
|
||||
|
||||
export const section = 'releases';
|
||||
export const episodeSection = 'releases.episode';
|
||||
export const seasonSection = 'releases.season';
|
||||
|
||||
let abortCurrentRequest = null;
|
||||
|
||||
//
|
||||
// State
|
||||
|
||||
export const defaultState = {
|
||||
isFetching: false,
|
||||
isPopulated: false,
|
||||
error: null,
|
||||
items: [],
|
||||
sortKey: 'releaseWeight',
|
||||
sortDirection: sortDirections.ASCENDING,
|
||||
sortPredicates: {
|
||||
peers: function(item, direction) {
|
||||
const seeders = item.seeders || 0;
|
||||
const leechers = item.leechers || 0;
|
||||
|
||||
return seeders * 1000000 + leechers;
|
||||
},
|
||||
|
||||
rejections: function(item, direction) {
|
||||
const rejections = item.rejections;
|
||||
const releaseWeight = item.releaseWeight;
|
||||
|
||||
if (rejections.length !== 0) {
|
||||
return releaseWeight + 1000000;
|
||||
}
|
||||
|
||||
return releaseWeight;
|
||||
}
|
||||
},
|
||||
|
||||
filters: [
|
||||
{
|
||||
key: 'all',
|
||||
label: 'All',
|
||||
filters: []
|
||||
},
|
||||
{
|
||||
key: 'season-pack',
|
||||
label: 'Season Pack',
|
||||
filters: [
|
||||
{
|
||||
key: 'fullSeason',
|
||||
value: true,
|
||||
type: filterTypes.EQUAL
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
key: 'not-season-pack',
|
||||
label: 'Not Season Pack',
|
||||
filters: [
|
||||
{
|
||||
key: 'fullSeason',
|
||||
value: false,
|
||||
type: filterTypes.EQUAL
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
filterPredicates: {
|
||||
quality: function(item, value, type) {
|
||||
const qualityId = item.quality.quality.id;
|
||||
|
||||
if (type === filterTypes.EQUAL) {
|
||||
return qualityId === value;
|
||||
}
|
||||
|
||||
if (type === filterTypes.NOT_EQUAL) {
|
||||
return qualityId !== value;
|
||||
}
|
||||
|
||||
// Default to false
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
filterBuilderProps: [
|
||||
{
|
||||
name: 'title',
|
||||
label: 'Title',
|
||||
type: filterBuilderTypes.STRING
|
||||
},
|
||||
{
|
||||
name: 'age',
|
||||
label: 'Age',
|
||||
type: filterBuilderTypes.NUMBER
|
||||
},
|
||||
{
|
||||
name: 'protocol',
|
||||
label: 'Protocol',
|
||||
type: filterBuilderTypes.EXACT,
|
||||
valueType: filterBuilderValueTypes.PROTOCOL
|
||||
},
|
||||
{
|
||||
name: 'indexerId',
|
||||
label: 'Indexer',
|
||||
type: filterBuilderTypes.EXACT,
|
||||
valueType: filterBuilderValueTypes.INDEXER
|
||||
},
|
||||
{
|
||||
name: 'size',
|
||||
label: 'Size',
|
||||
type: filterBuilderTypes.NUMBER
|
||||
},
|
||||
{
|
||||
name: 'seeders',
|
||||
label: 'Seeders',
|
||||
type: filterBuilderTypes.NUMBER
|
||||
},
|
||||
{
|
||||
name: 'peers',
|
||||
label: 'Peers',
|
||||
type: filterBuilderTypes.NUMBER
|
||||
},
|
||||
{
|
||||
name: 'quality',
|
||||
label: 'Quality',
|
||||
type: filterBuilderTypes.EXACT,
|
||||
valueType: filterBuilderValueTypes.QUALITY
|
||||
},
|
||||
{
|
||||
name: 'rejections',
|
||||
label: 'Rejections',
|
||||
type: filterBuilderTypes.NUMBER
|
||||
}
|
||||
],
|
||||
|
||||
episode: {
|
||||
selectedFilterKey: 'all'
|
||||
},
|
||||
|
||||
season: {
|
||||
selectedFilterKey: 'season-pack'
|
||||
}
|
||||
};
|
||||
|
||||
export const persistState = [
|
||||
'releases.selectedFilterKey',
|
||||
'releases.episode.customFilters',
|
||||
'releases.season.customFilters'
|
||||
];
|
||||
|
||||
//
|
||||
// Actions Types
|
||||
|
||||
export const FETCH_RELEASES = 'releases/fetchReleases';
|
||||
export const CANCEL_FETCH_RELEASES = 'releases/cancelFetchReleases';
|
||||
export const SET_RELEASES_SORT = 'releases/setReleasesSort';
|
||||
export const CLEAR_RELEASES = 'releases/clearReleases';
|
||||
export const GRAB_RELEASE = 'releases/grabRelease';
|
||||
export const UPDATE_RELEASE = 'releases/updateRelease';
|
||||
export const SET_EPISODE_RELEASES_FILTER = 'releases/setEpisodeReleasesFilter';
|
||||
export const SET_SEASON_RELEASES_FILTER = 'releases/setSeasonReleasesFilter';
|
||||
|
||||
//
|
||||
// Action Creators
|
||||
|
||||
export const fetchReleases = createThunk(FETCH_RELEASES);
|
||||
export const cancelFetchReleases = createThunk(CANCEL_FETCH_RELEASES);
|
||||
export const setReleasesSort = createAction(SET_RELEASES_SORT);
|
||||
export const clearReleases = createAction(CLEAR_RELEASES);
|
||||
export const grabRelease = createThunk(GRAB_RELEASE);
|
||||
export const updateRelease = createAction(UPDATE_RELEASE);
|
||||
export const setEpisodeReleasesFilter = createAction(SET_EPISODE_RELEASES_FILTER);
|
||||
export const setSeasonReleasesFilter = createAction(SET_SEASON_RELEASES_FILTER);
|
||||
|
||||
//
|
||||
// Helpers
|
||||
|
||||
const fetchReleasesHelper = createFetchHandler(section, '/release');
|
||||
|
||||
//
|
||||
// Action Handlers
|
||||
|
||||
export const actionHandlers = handleThunks({
|
||||
|
||||
[FETCH_RELEASES]: function(getState, payload, dispatch) {
|
||||
const abortRequest = fetchReleasesHelper(getState, payload, dispatch);
|
||||
|
||||
abortCurrentRequest = abortRequest;
|
||||
},
|
||||
|
||||
[CANCEL_FETCH_RELEASES]: function(getState, payload, dispatch) {
|
||||
if (abortCurrentRequest) {
|
||||
abortCurrentRequest = abortCurrentRequest();
|
||||
}
|
||||
},
|
||||
|
||||
[GRAB_RELEASE]: function(getState, payload, dispatch) {
|
||||
const guid = payload.guid;
|
||||
|
||||
dispatch(updateRelease({ guid, isGrabbing: true }));
|
||||
|
||||
const promise = $.ajax({
|
||||
url: '/release',
|
||||
method: 'POST',
|
||||
contentType: 'application/json',
|
||||
data: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
promise.done((data) => {
|
||||
dispatch(updateRelease({
|
||||
guid,
|
||||
isGrabbing: false,
|
||||
isGrabbed: true,
|
||||
grabError: null
|
||||
}));
|
||||
});
|
||||
|
||||
promise.fail((xhr) => {
|
||||
const grabError = xhr.responseJSON && xhr.responseJSON.message || 'Failed to add to download queue';
|
||||
|
||||
dispatch(updateRelease({
|
||||
guid,
|
||||
isGrabbing: false,
|
||||
isGrabbed: false,
|
||||
grabError
|
||||
}));
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
// Reducers
|
||||
|
||||
export const reducers = createHandleActions({
|
||||
|
||||
[CLEAR_RELEASES]: (state) => {
|
||||
const {
|
||||
episode,
|
||||
season,
|
||||
...otherDefaultState
|
||||
} = defaultState;
|
||||
|
||||
return Object.assign({}, state, otherDefaultState);
|
||||
},
|
||||
|
||||
[UPDATE_RELEASE]: (state, { payload }) => {
|
||||
const guid = payload.guid;
|
||||
const newState = Object.assign({}, state);
|
||||
const items = newState.items;
|
||||
|
||||
// Return early if there aren't any items (the user closed the modal)
|
||||
if (!items.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const index = items.findIndex((item) => item.guid === guid);
|
||||
const item = Object.assign({}, items[index], payload);
|
||||
|
||||
newState.items = [...items];
|
||||
newState.items.splice(index, 1, item);
|
||||
|
||||
return newState;
|
||||
},
|
||||
|
||||
[SET_RELEASES_SORT]: createSetClientSideCollectionSortReducer(section),
|
||||
[SET_EPISODE_RELEASES_FILTER]: createSetClientSideCollectionFilterReducer(episodeSection),
|
||||
[SET_SEASON_RELEASES_FILTER]: createSetClientSideCollectionFilterReducer(seasonSection)
|
||||
|
||||
}, defaultState, section);
|
Reference in New Issue
Block a user