Refactor dialogs
This commit is contained in:
@@ -1,30 +1,12 @@
|
||||
import { createAction } from '@reduxjs/toolkit';
|
||||
import { createResponseDialogAction } from '../utils';
|
||||
|
||||
export const confirmYesNoAction = createResponseDialogAction('common', 'confirmYesNo');
|
||||
|
||||
import * as Constants from '../../constants';
|
||||
import { getIPCRenderer } from '../../utils.jsx';
|
||||
|
||||
const ipcRenderer = getIPCRenderer();
|
||||
let yesNoResolvers = [];
|
||||
|
||||
export const confirmYesNo = (title, cb) => {
|
||||
return (dispatch) => {
|
||||
if (cb) {
|
||||
dispatch(confirmYesNo(title)).then((confirmed) => cb(confirmed));
|
||||
} else {
|
||||
return new Promise((resolve) => {
|
||||
dispatch(handleConfirmYesNo(true, title, resolve));
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const DISPLAY_CONFIRM_YES_NO = 'common/displayConfirmYesNo';
|
||||
const displayConfirmYesNo = (show, title) => {
|
||||
return {
|
||||
type: DISPLAY_CONFIRM_YES_NO,
|
||||
payload: { show, title },
|
||||
};
|
||||
};
|
||||
|
||||
export const displaySelectAppPlatform = (display) => {
|
||||
return (dispatch) => {
|
||||
@@ -36,25 +18,6 @@ export const displaySelectAppPlatform = (display) => {
|
||||
};
|
||||
};
|
||||
|
||||
export const hideConfirmYesNo = (confirmed) => {
|
||||
return (dispatch) => {
|
||||
dispatch(handleConfirmYesNo(false, confirmed));
|
||||
};
|
||||
};
|
||||
|
||||
const handleConfirmYesNo = (show, titleOrConfirmed, resolve) => {
|
||||
return (dispatch) => {
|
||||
if (show) {
|
||||
yesNoResolvers.push(resolve);
|
||||
dispatch(displayConfirmYesNo(show, titleOrConfirmed));
|
||||
} else {
|
||||
yesNoResolvers[0](titleOrConfirmed);
|
||||
yesNoResolvers.splice(0, 1);
|
||||
dispatch(displayConfirmYesNo(false));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const NOTIFY_APPLICATION_BUSY = 'common/notifyApplicationBusy';
|
||||
export const notifyApplicationBusy = (busy, transparent) => {
|
||||
return {
|
||||
|
||||
@@ -1,41 +1,3 @@
|
||||
let addEditHostResolvers = [];
|
||||
import { createResponseDialogAction } from '../utils';
|
||||
|
||||
export const addEditHost = (display, host_list, host_data, cb) => {
|
||||
return (dispatch) => {
|
||||
if (cb) {
|
||||
dispatch(addEditHost(display, host_list, host_data)).then(({ changed, host_data }) =>
|
||||
cb(changed, host_data)
|
||||
);
|
||||
} else {
|
||||
return new Promise((resolve) => {
|
||||
dispatch(handleDisplayAddEditHost(display, host_list, host_data, resolve));
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const handleDisplayAddEditHost = (display, host_list, host_data, resolve) => {
|
||||
return (dispatch) => {
|
||||
if (display) {
|
||||
addEditHostResolvers.push(resolve);
|
||||
dispatch(displayAddEditHost(display, host_list, host_data));
|
||||
} else {
|
||||
dispatch(completeAddEditHost(false));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const completeAddEditHost = (changed, host_data) => {
|
||||
return (dispatch) => {
|
||||
if (changed) {
|
||||
addEditHostResolvers[0]({ changed, host_data });
|
||||
}
|
||||
addEditHostResolvers.splice(0, 1);
|
||||
dispatch(displayAddEditHost(false));
|
||||
};
|
||||
};
|
||||
|
||||
export const DISPLAY_ADD_EDIT_HOST = 'host/displayAddEditHost';
|
||||
export const displayAddEditHost = (display, host_list, host_data) => {
|
||||
return { type: DISPLAY_ADD_EDIT_HOST, payload: { display, host_data, host_list } };
|
||||
};
|
||||
export const addEditHostAction = createResponseDialogAction('host', 'displayAddEditHost');
|
||||
|
||||
@@ -4,7 +4,7 @@ import * as Constants from '../../constants';
|
||||
import { getIPCRenderer, getSelectedVersionFromState } from '../../utils.jsx';
|
||||
|
||||
import {
|
||||
confirmYesNo,
|
||||
confirmYesNoAction,
|
||||
displaySelectAppPlatform,
|
||||
setAllowMount,
|
||||
setApplicationReady,
|
||||
@@ -206,9 +206,9 @@ export const installReleaseByVersion = (release, version) => {
|
||||
};
|
||||
|
||||
if (getState().mounts.MountsBusy) {
|
||||
dispatch(confirmYesNo('Unmount all drives?'))
|
||||
.then((confirmed) => {
|
||||
if (confirmed) {
|
||||
dispatch(confirmYesNoAction.display(true, null, { title: 'Unmount all drives?' }))
|
||||
.then(({ changed }) => {
|
||||
if (changed) {
|
||||
dispatch(unmountAll(install));
|
||||
}
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ import { createAction } from '@reduxjs/toolkit';
|
||||
import * as Constants from '../../constants';
|
||||
import { getIPCRenderer } from '../../utils.jsx';
|
||||
|
||||
import { confirmYesNo, saveState } from './common_actions';
|
||||
import { confirmYesNoAction, saveState } from './common_actions';
|
||||
import { notifyError } from './error_actions';
|
||||
|
||||
export const addRemoteMount = (hostNameOrIp, port, token) => {
|
||||
@@ -23,7 +23,7 @@ export const addRemoteMount = (hostNameOrIp, port, token) => {
|
||||
Version: getState().relver.InstalledVersion,
|
||||
});
|
||||
} else {
|
||||
dispatch(notifyError('Failed to create S3 instance: ' + arg.data.Error));
|
||||
dispatch(notifyError('Failed to create remote mount: ' + arg.data.Error));
|
||||
dispatch(setBusy(false));
|
||||
}
|
||||
});
|
||||
@@ -32,7 +32,7 @@ export const addRemoteMount = (hostNameOrIp, port, token) => {
|
||||
Items: [
|
||||
{ Name: 'RemoteMount.RemoteHostNameOrIp', Value: hostNameOrIp },
|
||||
{ Name: 'RemoteMount.RemoteToken', Value: token },
|
||||
{ Name: 'RemoteMount.RemotePort', Value: port },
|
||||
{ Name: 'RemoteMount.RemotePort', Value: port.toString() },
|
||||
{ Name: 'RemoteMount.IsRemoteMount', Value: 'true' },
|
||||
],
|
||||
Provider: provider,
|
||||
@@ -96,13 +96,15 @@ export const displayConfiguration = (provider, remote, s3) => {
|
||||
export const removeMount = (provider) => {
|
||||
return (dispatch) => {
|
||||
const isRemote = provider.startsWith('Remote');
|
||||
dispatch(confirmYesNo('Delete [' + provider.substr(isRemote ? 6 : 2) + ']?')).then(
|
||||
(confirmed) => {
|
||||
if (confirmed) {
|
||||
dispatch(removeMount2(provider));
|
||||
}
|
||||
dispatch(
|
||||
confirmYesNoAction.display(true, null, {
|
||||
title: 'Delete [' + provider.substr(isRemote ? 6 : 2) + ']?',
|
||||
})
|
||||
).then(({ changed }) => {
|
||||
if (changed) {
|
||||
dispatch(removeMount2(provider));
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
|
||||
import {
|
||||
DISPLAY_CONFIRM_YES_NO,
|
||||
confirmYesNoAction,
|
||||
NOTIFY_APPLICATION_BUSY,
|
||||
notifyRebootRequired,
|
||||
SET_DISPLAY_SELECT_APPPLATFORM,
|
||||
@@ -26,11 +25,12 @@ export const createCommonReducer = (platformInfo, version) => {
|
||||
Version: version,
|
||||
},
|
||||
{
|
||||
[DISPLAY_CONFIRM_YES_NO]: (state, action) => {
|
||||
[confirmYesNoAction.action_type]: (state, action) => {
|
||||
return {
|
||||
...state,
|
||||
DisplayConfirmYesNo: action.payload.show,
|
||||
ConfirmTitle: action.payload.show ? action.payload.title : null,
|
||||
DisplayConfirmYesNo: action.payload.display,
|
||||
ConfirmTitle:
|
||||
action.payload.display && action.payload.data ? action.payload.data.title : null,
|
||||
};
|
||||
},
|
||||
[SET_DISPLAY_SELECT_APPPLATFORM]: (state, action) => {
|
||||
|
||||
@@ -1,21 +1,19 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import * as Actions from '../actions/host_actions';
|
||||
import { addEditHostAction } from '../actions/host_actions';
|
||||
|
||||
export const hostReducer = createReducer(
|
||||
{
|
||||
DisplayAddEditHost: false,
|
||||
Edit: false,
|
||||
HostData: {},
|
||||
HostList: [],
|
||||
},
|
||||
{
|
||||
[Actions.DISPLAY_ADD_EDIT_HOST]: (state, action) => {
|
||||
[addEditHostAction.action_type]: (state, action) => {
|
||||
return {
|
||||
...state,
|
||||
DisplayAddEditHost: action.payload.display,
|
||||
Edit: !!action.payload.host_data,
|
||||
HostData: action.payload.host_data || {},
|
||||
HostList: action.payload.host_list || [],
|
||||
HostData: action.payload.data ? action.payload.data.host_data || {} : {},
|
||||
HostList: action.payload.data ? action.payload.data.host_list || [] : [],
|
||||
};
|
||||
},
|
||||
}
|
||||
|
||||
47
src/redux/utils.js
Normal file
47
src/redux/utils.js
Normal file
@@ -0,0 +1,47 @@
|
||||
export const createResponseDialogAction = (type, name) => {
|
||||
let resolverList = [];
|
||||
|
||||
const display = (show, cb, data) => {
|
||||
return (dispatch) => {
|
||||
if (cb) {
|
||||
dispatch(display(show, null, data)).then(({ changed, data }) => cb(changed, data));
|
||||
} else {
|
||||
return new Promise((resolve) => {
|
||||
dispatch(handleDisplay(show, data, resolve));
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const handleDisplay = (show, data, resolve) => {
|
||||
return (dispatch) => {
|
||||
if (show) {
|
||||
resolverList.push(resolve);
|
||||
dispatch(displayAction(show, data));
|
||||
} else {
|
||||
dispatch(complete(false));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const complete = (changed, data) => {
|
||||
return (dispatch) => {
|
||||
if (changed) {
|
||||
resolverList[0]({ changed, data });
|
||||
}
|
||||
resolverList.splice(0, 1);
|
||||
dispatch(displayAction(false));
|
||||
};
|
||||
};
|
||||
|
||||
const DISPLAY_ACTION = type + '/' + name;
|
||||
const displayAction = (display, data) => {
|
||||
return { type: DISPLAY_ACTION, payload: { display, data } };
|
||||
};
|
||||
|
||||
return {
|
||||
action_type: DISPLAY_ACTION,
|
||||
complete,
|
||||
display,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user