lbry-desktop/ui/js/reducers/file_info.js
2017-07-13 13:52:47 +07:00

200 lines
5.2 KiB
JavaScript

import * as types from "constants/action_types";
import lbryuri from "lbryuri";
const reducers = {};
const defaultState = {};
reducers[types.FILE_LIST_STARTED] = function(state, action) {
return Object.assign({}, state, {
isFileListPending: true,
});
};
reducers[types.FILE_LIST_COMPLETED] = function(state, action) {
const { fileInfos } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const pendingByOutpoint = Object.assign({}, state.pendingByOutpoint);
fileInfos.forEach(fileInfo => {
const { outpoint } = fileInfo;
if (outpoint) newByOutpoint[fileInfo.outpoint] = fileInfo;
});
return Object.assign({}, state, {
isFileListPending: false,
byOutpoint: newByOutpoint,
pendingByOutpoint,
});
};
reducers[types.FETCH_FILE_INFO_STARTED] = function(state, action) {
const { outpoint } = action.data;
const newFetching = Object.assign({}, state.fetching);
newFetching[outpoint] = true;
return Object.assign({}, state, {
fetching: newFetching,
});
};
reducers[types.FETCH_FILE_INFO_COMPLETED] = function(state, action) {
const { fileInfo, outpoint } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const newFetching = Object.assign({}, state.fetching);
newByOutpoint[outpoint] = fileInfo;
delete newFetching[outpoint];
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
fetching: newFetching,
});
};
reducers[types.DOWNLOADING_STARTED] = function(state, action) {
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const newDownloading = Object.assign({}, state.urisDownloading);
const newLoading = Object.assign({}, state.urisLoading);
newDownloading[uri] = true;
newByOutpoint[outpoint] = fileInfo;
delete newLoading[uri];
return Object.assign({}, state, {
urisDownloading: newDownloading,
urisLoading: newLoading,
byOutpoint: newByOutpoint,
});
};
reducers[types.DOWNLOADING_PROGRESSED] = function(state, action) {
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const newDownloading = Object.assign({}, state.urisDownloading);
newByOutpoint[outpoint] = fileInfo;
newDownloading[uri] = true;
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
urisDownloading: newDownloading,
});
};
reducers[types.DOWNLOADING_COMPLETED] = function(state, action) {
const { uri, outpoint, fileInfo } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
const newDownloading = Object.assign({}, state.urisDownloading);
newByOutpoint[outpoint] = fileInfo;
delete newDownloading[uri];
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
urisDownloading: newDownloading,
});
};
reducers[types.FILE_DELETE] = function(state, action) {
const { outpoint } = action.data;
const newByOutpoint = Object.assign({}, state.byOutpoint);
delete newByOutpoint[outpoint];
return Object.assign({}, state, {
byOutpoint: newByOutpoint,
});
};
reducers[types.LOADING_VIDEO_STARTED] = function(state, action) {
const { uri } = action.data;
const newLoading = Object.assign({}, state.urisLoading);
newLoading[uri] = true;
return Object.assign({}, state, {
urisLoading: newLoading,
});
};
reducers[types.LOADING_VIDEO_FAILED] = function(state, action) {
const { uri } = action.data;
const newLoading = Object.assign({}, state.urisLoading);
delete newLoading[uri];
return Object.assign({}, state, {
urisLoading: newLoading,
});
};
reducers[types.PUBLISH_STARTED] = function(state, action) {
const { pendingPublish } = action.data;
const pendingByOutpoint = Object.assign({}, state.pendingByOutpoint);
pendingByOutpoint[pendingPublish.outpoint] = pendingPublish;
return Object.assign({}, state, {
pendingByOutpoint,
});
};
reducers[types.PUBLISH_COMPLETED] = function(state, action) {
const { pendingPublish } = action.data;
const pendingByOutpoint = Object.assign({}, state.pendingByOutpoint);
delete pendingByOutpoint[pendingPublish.outpoint];
return Object.assign({}, state, {
pendingByOutpoint,
});
};
reducers[types.PUBLISH_FAILED] = function(state, action) {
const { pendingPublish } = action.data;
const pendingByOutpoint = Object.assign({}, state.pendingByOutpoint);
delete pendingByOutpoint[pendingPublish.outpoint];
return Object.assign({}, state, {
pendingByOutpoint,
});
};
// reducers[types.PUBLISH_COMPLETED] = function(state, action) {
// const { claim } = action.data;
// const uri = lbryuri.build({
// txid: claim.txId
// })
// const newPendingPublish = {
// name,
// channel_name,
// claim_id: "pending_claim_" + uri,
// txid: "pending_" + uri,
// nout: 0,
// outpoint: "pending_" + uri + ":0",
// time: Date.now(),
// };
// const fileInfos = Object.assign({}, state.fileInfos)
// fileInfos[newPendingPublish.outpoint] = newPendingPublish
// return Object.assign({}, state, {
// fileInfos,
// })
// }
export default function reducer(state = defaultState, action) {
const handler = reducers[action.type];
if (handler) return handler(state, action);
return state;
}