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

144 lines
3.6 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, {
isFileListPending: true,
2017-06-06 23:19:12 +02:00
});
2017-06-06 06:21:55 +02:00
};
reducers[types.FILE_LIST_COMPLETED] = function(state, action) {
2017-06-06 23:19:12 +02:00
const { fileInfos } = action.data;
2017-06-06 23:19:12 +02:00
const newFileInfos = Object.assign({}, state.fileInfos);
fileInfos.forEach(fileInfo => {
const { outpoint } = fileInfo;
2017-06-06 23:19:12 +02:00
if (outpoint) newFileInfos[fileInfo.outpoint] = fileInfo;
});
return Object.assign({}, state, {
isFileListPending: false,
2017-06-06 23:19:12 +02:00
fileInfos: newFileInfos,
});
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
2017-06-06 23:19:12 +02:00
const newFileInfos = Object.assign({}, state.fileInfos);
const newFetching = Object.assign({}, state.fetching);
2017-04-28 17:14:44 +02:00
2017-06-06 23:19:12 +02:00
newFileInfos[outpoint] = fileInfo;
delete newFetching[outpoint];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
fileInfos: newFileInfos,
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;
2017-06-06 23:19:12 +02:00
const newFileInfos = Object.assign({}, state.fileInfos);
const newDownloading = Object.assign({}, state.urisDownloading);
const newLoading = Object.assign({}, state.urisLoading);
2017-04-28 17:14:44 +02:00
2017-06-06 23:19:12 +02:00
newDownloading[uri] = true;
newFileInfos[outpoint] = fileInfo;
delete newLoading[uri];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
urisDownloading: newDownloading,
urisLoading: newLoading,
fileInfos: newFileInfos,
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;
2017-06-06 23:19:12 +02:00
const newFileInfos = Object.assign({}, state.fileInfos);
const newDownloading = Object.assign({}, state.urisDownloading);
2017-04-28 17:14:44 +02:00
2017-06-06 23:19:12 +02:00
newFileInfos[outpoint] = fileInfo;
newDownloading[uri] = true;
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
fileInfos: newFileInfos,
2017-06-06 23:19:12 +02:00
urisDownloading: newDownloading,
});
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;
2017-06-06 23:19:12 +02:00
const newFileInfos = Object.assign({}, state.fileInfos);
const newDownloading = Object.assign({}, state.urisDownloading);
2017-04-28 17:14:44 +02:00
2017-06-06 23:19:12 +02:00
newFileInfos[outpoint] = fileInfo;
delete newDownloading[uri];
2017-04-28 17:14:44 +02:00
return Object.assign({}, state, {
fileInfos: newFileInfos,
urisDownloading: 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;
2017-06-06 23:19:12 +02:00
const newFileInfos = Object.assign({}, state.fileInfos);
2017-06-06 23:19:12 +02:00
delete newFileInfos[outpoint];
return Object.assign({}, state, {
fileInfos: newFileInfos,
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;
}