Redux changes and refactoring
This commit is contained in:
52
src/redux/actions/download_actions.js
Normal file
52
src/redux/actions/download_actions.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import * as Constants from '../../constants';
|
||||
import {createAction} from 'redux-starter-kit';
|
||||
|
||||
export const setAllowDownload = createAction('download/setAllowDownload');
|
||||
|
||||
export const SET_DOWNLOAD_BEGIN = 'download/setDownloadBegin';
|
||||
export const setDownloadBegin = (name, type, url) => {
|
||||
return {
|
||||
type: SET_DOWNLOAD_BEGIN,
|
||||
payload: {
|
||||
name,
|
||||
type,
|
||||
url
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const setDownloadEnd = createAction('download/setDownloadEnd');
|
||||
export const setDownloadProgress = createAction('download/setDownloadProgress');
|
||||
|
||||
export const downloadItem = (name, type, url, completedCallback) => {
|
||||
return (dispatch, getState) => {
|
||||
const state = getState();
|
||||
if (!state.download.DownloadActive && state.download.AllowDownload) {
|
||||
if (!process.versions.hasOwnProperty('electron')) {
|
||||
const ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null);
|
||||
if (ipcRenderer) {
|
||||
dispatch(setDownloadBegin(name, type, url));
|
||||
|
||||
const downloadFileProgress = (_, arg) => {
|
||||
dispatch(setDownloadProgress(arg.data.Progress));
|
||||
};
|
||||
|
||||
const downloadFileComplete = (_, arg) => {
|
||||
this.ipcRenderer.removeListener(Constants.IPC_Download_File_Progress, downloadFileProgress);
|
||||
this.ipcRenderer.removeListener(Constants.IPC_Download_File_Complete, downloadFileComplete);
|
||||
completedCallback(name, type, url, arg.data);
|
||||
dispatch(setDownloadEnd(arg.data));
|
||||
};
|
||||
|
||||
ipcRenderer.on(Constants.IPC_Download_File_Progress, downloadFileProgress);
|
||||
ipcRenderer.on(Constants.IPC_Download_File_Complete, downloadFileComplete);
|
||||
|
||||
ipcRenderer.send(Constants.IPC_Download_File, {
|
||||
Filename: name,
|
||||
URL: url,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
49
src/redux/reducers/download_reducer.js
Normal file
49
src/redux/reducers/download_reducer.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import {createReducer} from 'redux-starter-kit';
|
||||
import {setAllowDownload, SET_DOWNLOAD_BEGIN, setDownloadEnd, setDownloadProgress} from '../actions/download_actions';
|
||||
|
||||
const defaultDownloadState = {
|
||||
DownloadActive: false,
|
||||
DownloadProgress: 0.0,
|
||||
DownloadingDependency: false,
|
||||
DownloadName: '',
|
||||
DownloadType: null,
|
||||
DownloadingRelease: false,
|
||||
DownloadResult: null,
|
||||
DownloadingUpgrade: false
|
||||
};
|
||||
|
||||
export const downloadReducer = createReducer({
|
||||
...defaultDownloadState,
|
||||
AllowDownload: false,
|
||||
}, {
|
||||
[setAllowDownload]: (state, action) => {
|
||||
return {
|
||||
...state,
|
||||
AllowDownload: action.payload,
|
||||
};
|
||||
},
|
||||
[SET_DOWNLOAD_BEGIN]: (state, action) => {
|
||||
return {
|
||||
...state,
|
||||
DownloadActive: true,
|
||||
DownloadName: action.payload.name,
|
||||
DownloadProgress: 0.0,
|
||||
DownloadResult: null,
|
||||
DownloadType: action.payload.type,
|
||||
DownloadURL: action.payload.url,
|
||||
}
|
||||
},
|
||||
[setDownloadEnd]: (state, action) => {
|
||||
return {
|
||||
...state,
|
||||
...defaultDownloadState,
|
||||
DownloadResult: action.payload,
|
||||
};
|
||||
},
|
||||
[setDownloadProgress]: (state, action) => {
|
||||
return {
|
||||
...state,
|
||||
DownloadProgress: action.payload,
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -2,10 +2,12 @@ import {configureStore, getDefaultMiddleware} from 'redux-starter-kit';
|
||||
import {createCommonReducer} from '../reducers/common_reducer';
|
||||
import {mountReducer} from '../reducers/mount_reducer';
|
||||
import {releaseVersionReducer} from '../reducers/release_version_reducer';
|
||||
import {downloadReducer} from '../reducers/download_reducer';
|
||||
|
||||
export default function createAppStore(platform, appPlatform, version) {
|
||||
const reducer = {
|
||||
common: createCommonReducer(platform, appPlatform, version),
|
||||
download: downloadReducer,
|
||||
mounts: mountReducer,
|
||||
relver: releaseVersionReducer,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user