Redux changes and refactoring

This commit is contained in:
Scott E. Graves
2019-06-07 00:30:59 -05:00
parent 1dc99567f3
commit 31585fc912
6 changed files with 179 additions and 140 deletions

View 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,
});
}
}
}
};
};

View 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,
}
}
});

View File

@@ -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,
};