lbry-desktop/ui/js/reducers/file_info.js

149 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
import lbryuri from "lbryuri";
2017-04-28 17:14:44 +02:00
2017-06-06 06:21:55 +02:00
const reducers = {};
const defaultState = {};
2017-04-28 17:14:44 +02:00
reducers[types.FILE_LIST_STARTED] = function(state, action) {
return Object.assign({}, state, {
isFetchingFileList: true,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
reducers[types.FILE_LIST_SUCCEEDED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { fileInfos } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
2017-06-17 19:59:18 +02:00
const pendingByOutpoint = Object.assign({}, state.pendingByOutpoint);
2017-06-06 23:19:12 +02:00
fileInfos.forEach(fileInfo => {
const { outpoint } = fileInfo;
if (outpoint) newByOutpoint[fileInfo.outpoint] = fileInfo;
2017-06-06 23:19:12 +02:00
});
return Object.assign({}, state, {
isFetchingFileList: false,
byOutpoint: newByOutpoint,
2017-06-17 19:59:18 +02:00
pendingByOutpoint,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-28 17:14:44 +02:00
reducers[types.FETCH_FILE_INFO_STARTED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { outpoint } = action.data;
const newFetching = Object.assign({}, state.fetching);
2017-04-28 17:14:44 +02:00
2017-06-06 23:19:12 +02:00
newFetching[outpoint] = true;
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
fetching: newFetching,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-28 17:14:44 +02:00
reducers[types.FETCH_FILE_INFO_COMPLETED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { fileInfo, outpoint } = action.data;
2017-05-15 05:50:59 +02:00
const newByOutpoint = Object.assign({}, state.byOutpoint);
2017-06-06 23:19:12 +02:00
const newFetching = Object.assign({}, state.fetching);
2017-04-28 17:14:44 +02:00
newByOutpoint[outpoint] = fileInfo;
2017-06-06 23:19:12 +02:00
delete newFetching[outpoint];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
2017-04-28 17:14:44 +02:00
fetching: newFetching,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-28 17:14:44 +02:00
reducers[types.DOWNLOADING_STARTED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
2017-07-21 10:02:29 +02:00
const newDownloading = Object.assign({}, state.downloadingByOutpoin);
2017-06-06 23:19:12 +02:00
const newLoading = Object.assign({}, state.urisLoading);
2017-04-28 17:14:44 +02:00
2017-07-21 10:02:29 +02:00
newDownloading[outpoint] = true;
newByOutpoint[outpoint] = fileInfo;
2017-06-06 23:19:12 +02:00
delete newLoading[uri];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
2017-07-21 10:02:29 +02:00
downloadingByOutpoint: newDownloading,
urisLoading: newLoading,
byOutpoint: newByOutpoint,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-28 17:14:44 +02:00
reducers[types.DOWNLOADING_PROGRESSED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
2017-07-21 10:02:29 +02:00
const newDownloading = Object.assign({}, state.downloadingByOutpoint);
2017-04-28 17:14:44 +02:00
2017-06-27 10:23:03 +02:00
newByOutpoint[outpoint] = fileInfo;
2017-07-21 10:02:29 +02:00
newDownloading[outpoint] = true;
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
2017-07-21 10:02:29 +02:00
downloadingByOutpoint: newDownloading,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-28 17:14:44 +02:00
reducers[types.DOWNLOADING_COMPLETED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
2017-07-21 10:02:29 +02:00
const newDownloading = Object.assign({}, state.downloadingByOutpoint);
2017-04-28 17:14:44 +02:00
newByOutpoint[outpoint] = fileInfo;
2017-07-21 10:02:29 +02:00
delete newDownloading[outpoint];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
2017-07-21 10:02:29 +02:00
downloadingByOutpoint: newDownloading,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
reducers[types.FILE_DELETE] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { outpoint } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const downloadingByOutpoint = Object.assign({}, state.downloadingByOutpoint);
delete newByOutpoint[outpoint];
delete downloadingByOutpoint[outpoint];
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
downloadingByOutpoint,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
reducers[types.LOADING_VIDEO_STARTED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { uri } = action.data;
2017-06-06 23:19:12 +02:00
const newLoading = Object.assign({}, state.urisLoading);
2017-06-06 23:19:12 +02:00
newLoading[uri] = true;
return Object.assign({}, state, {
urisLoading: newLoading,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
reducers[types.LOADING_VIDEO_FAILED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { uri } = action.data;
2017-06-06 23:19:12 +02:00
const newLoading = Object.assign({}, state.urisLoading);
2017-06-06 23:19:12 +02:00
delete newLoading[uri];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
urisLoading: newLoading,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
2017-04-28 17:14:44 +02:00
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}