UI Action Handler Changes, Misc Fixes

This commit is contained in:
Qstick 2017-11-26 15:09:45 -05:00
parent 7825319d89
commit cd5b658196
193 changed files with 6992 additions and 6341 deletions

View file

@ -1,10 +1,152 @@
import $ from 'jquery';
import { createAction } from 'redux-actions';
import * as types from './actionTypes';
import releaseActionHandlers from './releaseActionHandlers';
import { sortDirections } from 'Helpers/Props';
import { createThunk, handleThunks } from 'Store/thunks';
import createSetClientSideCollectionSortReducer from './Creators/Reducers/createSetClientSideCollectionSortReducer';
import createFetchHandler from './Creators/createFetchHandler';
import createHandleActions from './Creators/createHandleActions';
export const fetchReleases = releaseActionHandlers[types.FETCH_RELEASES];
export const cancelFetchReleases = releaseActionHandlers[types.CANCEL_FETCH_RELEASES];
export const setReleasesSort = createAction(types.SET_RELEASES_SORT);
export const clearReleases = createAction(types.CLEAR_RELEASES);
export const grabRelease = releaseActionHandlers[types.GRAB_RELEASE];
export const updateRelease = createAction(types.UPDATE_RELEASE);
//
// Variables
export const section = 'releases';
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;
}
}
};
//
// 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';
//
// 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);
//
// 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) => {
return Object.assign({}, state, defaultState);
},
[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)
}, defaultState, section);