Fixed page reload on settings change

This commit is contained in:
Ildar Kamalov 2019-01-23 17:22:04 +03:00 committed by Eugene Bujak
parent 6e41897323
commit 24be7ce4ed
5 changed files with 62 additions and 35 deletions

4
client/.eslintrc vendored
View File

@ -45,9 +45,7 @@
}], }],
"class-methods-use-this": "off", "class-methods-use-this": "off",
"no-shadow": "off", "no-shadow": "off",
"camelcase": ["error", { "camelcase": "off",
"properties": "never"
}],
"no-console": ["warn", { "allow": ["warn", "error"] }], "no-console": ["warn", { "allow": ["warn", "error"] }],
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }], "import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
"import/prefer-default-export": "off" "import/prefer-default-export": "off"

View File

@ -572,34 +572,36 @@ export const setDhcpConfigSuccess = createAction('SET_DHCP_CONFIG_SUCCESS');
export const setDhcpConfigFailure = createAction('SET_DHCP_CONFIG_FAILURE'); export const setDhcpConfigFailure = createAction('SET_DHCP_CONFIG_FAILURE');
// TODO rewrite findActiveDhcp part // TODO rewrite findActiveDhcp part
export const setDhcpConfig = config => async (dispatch) => { export const setDhcpConfig = values => async (dispatch, getState) => {
const { config } = getState().dhcp;
const updatedConfig = { ...config, ...values };
dispatch(setDhcpConfigRequest()); dispatch(setDhcpConfigRequest());
if (values.interface_name) {
if (config.interface_name) {
dispatch(findActiveDhcpRequest()); dispatch(findActiveDhcpRequest());
try { try {
const activeDhcp = await apiClient.findActiveDhcp(config.interface_name); const activeDhcp = await apiClient.findActiveDhcp(values.interface_name);
dispatch(findActiveDhcpSuccess(activeDhcp)); dispatch(findActiveDhcpSuccess(activeDhcp));
if (!activeDhcp.found) { if (!activeDhcp.found) {
await apiClient.setDhcpConfig(config); try {
await apiClient.setDhcpConfig(updatedConfig);
dispatch(setDhcpConfigSuccess(updatedConfig));
dispatch(addSuccessToast('dhcp_config_saved')); dispatch(addSuccessToast('dhcp_config_saved'));
dispatch(setDhcpConfigSuccess()); } catch (error) {
dispatch(getDhcpStatus()); dispatch(addErrorToast({ error }));
dispatch(setDhcpConfigFailure());
}
} else { } else {
dispatch(addErrorToast({ error: 'dhcp_found' })); dispatch(addErrorToast({ error: 'dhcp_found' }));
} }
} catch (error) { } catch (error) {
dispatch(addErrorToast({ error })); dispatch(addErrorToast({ error }));
dispatch(findActiveDhcpFailure()); dispatch(findActiveDhcpFailure());
dispatch(setDhcpConfigFailure());
} }
} else { } else {
try { try {
await apiClient.setDhcpConfig(config); await apiClient.setDhcpConfig(updatedConfig);
dispatch(setDhcpConfigSuccess(updatedConfig));
dispatch(addSuccessToast('dhcp_config_saved')); dispatch(addSuccessToast('dhcp_config_saved'));
dispatch(setDhcpConfigSuccess());
dispatch(getDhcpStatus());
} catch (error) { } catch (error) {
dispatch(addErrorToast({ error })); dispatch(addErrorToast({ error }));
dispatch(setDhcpConfigFailure()); dispatch(setDhcpConfigFailure());
@ -616,11 +618,10 @@ export const toggleDhcp = config => async (dispatch) => {
dispatch(toggleDhcpRequest()); dispatch(toggleDhcpRequest());
if (config.enabled) { if (config.enabled) {
dispatch(addSuccessToast('disabled_dhcp'));
try { try {
await apiClient.setDhcpConfig({ ...config, enabled: false }); await apiClient.setDhcpConfig({ ...config, enabled: false });
dispatch(toggleDhcpSuccess()); dispatch(toggleDhcpSuccess());
dispatch(getDhcpStatus()); dispatch(addSuccessToast('disabled_dhcp'));
} catch (error) { } catch (error) {
dispatch(addErrorToast({ error })); dispatch(addErrorToast({ error }));
dispatch(toggleDhcpFailure()); dispatch(toggleDhcpFailure());
@ -635,12 +636,11 @@ export const toggleDhcp = config => async (dispatch) => {
try { try {
await apiClient.setDhcpConfig({ ...config, enabled: true }); await apiClient.setDhcpConfig({ ...config, enabled: true });
dispatch(toggleDhcpSuccess()); dispatch(toggleDhcpSuccess());
dispatch(getDhcpStatus()); dispatch(addSuccessToast('enabled_dhcp'));
} catch (error) { } catch (error) {
dispatch(addErrorToast({ error })); dispatch(addErrorToast({ error }));
dispatch(toggleDhcpFailure()); dispatch(toggleDhcpFailure());
} }
dispatch(addSuccessToast('enabled_dhcp'));
} else { } else {
dispatch(addErrorToast({ error: 'dhcp_found' })); dispatch(addErrorToast({ error: 'dhcp_found' }));
} }

View File

@ -50,6 +50,7 @@ const Form = (props) => {
handleSubmit, handleSubmit,
submitting, submitting,
invalid, invalid,
processingConfig,
} = props; } = props;
return ( return (
@ -125,7 +126,7 @@ const Form = (props) => {
<button <button
type="submit" type="submit"
className="btn btn-success btn-standard" className="btn btn-success btn-standard"
disabled={submitting || invalid} disabled={submitting || invalid || processingConfig}
> >
{t('save_config')} {t('save_config')}
</button> </button>
@ -138,8 +139,8 @@ Form.propTypes = {
submitting: PropTypes.bool, submitting: PropTypes.bool,
invalid: PropTypes.bool, invalid: PropTypes.bool,
interfaces: PropTypes.object, interfaces: PropTypes.object,
processing: PropTypes.bool,
initialValues: PropTypes.object, initialValues: PropTypes.object,
processingConfig: PropTypes.bool,
t: PropTypes.func, t: PropTypes.func,
}; };

View File

@ -13,17 +13,14 @@ class Dhcp extends Component {
this.props.setDhcpConfig(values); this.props.setDhcpConfig(values);
}; };
handleFormChange = (value) => {
this.props.setDhcpConfig(value);
}
handleToggle = (config) => { handleToggle = (config) => {
this.props.toggleDhcp(config); this.props.toggleDhcp(config);
this.props.findActiveDhcp(config.interface_name);
} }
getToggleDhcpButton = () => { getToggleDhcpButton = () => {
const { config, active, processingDhcp } = this.props.dhcp; const {
config, active, processingDhcp, processingConfig,
} = this.props.dhcp;
const activeDhcpFound = active && active.found; const activeDhcpFound = active && active.found;
const filledConfig = Object.keys(config).every((key) => { const filledConfig = Object.keys(config).every((key) => {
if (key === 'enabled') { if (key === 'enabled') {
@ -39,7 +36,7 @@ class Dhcp extends Component {
type="button" type="button"
className="btn btn-standard mr-2 btn-gray" className="btn btn-standard mr-2 btn-gray"
onClick={() => this.props.toggleDhcp(config)} onClick={() => this.props.toggleDhcp(config)}
disabled={processingDhcp} disabled={processingDhcp || processingConfig}
> >
<Trans>dhcp_disable</Trans> <Trans>dhcp_disable</Trans>
</button> </button>
@ -51,7 +48,12 @@ class Dhcp extends Component {
type="button" type="button"
className="btn btn-standard mr-2 btn-success" className="btn btn-standard mr-2 btn-success"
onClick={() => this.handleToggle(config)} onClick={() => this.handleToggle(config)}
disabled={!filledConfig || activeDhcpFound || processingDhcp} disabled={
!filledConfig
|| activeDhcpFound
|| processingDhcp
|| processingConfig
}
> >
<Trans>dhcp_enable</Trans> <Trans>dhcp_enable</Trans>
</button> </button>
@ -94,6 +96,22 @@ class Dhcp extends Component {
'btn btn-primary btn-standard': true, 'btn btn-primary btn-standard': true,
'btn btn-primary btn-standard btn-loading': dhcp.processingStatus, 'btn btn-primary btn-standard btn-loading': dhcp.processingStatus,
}); });
const {
gateway_ip,
interface_name,
lease_duration,
range_end,
range_start,
subnet_mask,
} = dhcp.config;
const initialForm = {
gateway_ip,
lease_duration,
range_end,
range_start,
subnet_mask,
};
const initialSelect = { interface_name };
return ( return (
<Fragment> <Fragment>
@ -102,17 +120,17 @@ class Dhcp extends Component {
{!dhcp.processing && {!dhcp.processing &&
<Fragment> <Fragment>
<Interface <Interface
onChange={this.handleFormChange} onChange={this.handleFormSubmit}
initialValues={dhcp.config} initialValues={initialSelect}
interfaces={dhcp.interfaces} interfaces={dhcp.interfaces}
processing={dhcp.processingInterfaces} processing={dhcp.processingInterfaces}
enabled={dhcp.config.enabled} enabled={dhcp.config.enabled}
/> />
<Form <Form
onSubmit={this.handleFormSubmit} onSubmit={this.handleFormSubmit}
initialValues={dhcp.config} initialValues={initialForm}
interfaces={dhcp.interfaces} interfaces={dhcp.interfaces}
processing={dhcp.processingInterfaces} processingConfig={dhcp.processingConfig}
/> />
<hr/> <hr/>
<div className="card-actions mb-3"> <div className="card-actions mb-3">
@ -123,7 +141,7 @@ class Dhcp extends Component {
onClick={() => onClick={() =>
this.props.findActiveDhcp(dhcp.config.interface_name) this.props.findActiveDhcp(dhcp.config.interface_name)
} }
disabled={!dhcp.config.interface_name} disabled={!dhcp.config.interface_name || dhcp.processingConfig}
> >
<Trans>check_dhcp_servers</Trans> <Trans>check_dhcp_servers</Trans>
</button> </button>

View File

@ -308,11 +308,21 @@ const dhcp = handleActions({
const newState = { ...state, config: newConfig, processingDhcp: false }; const newState = { ...state, config: newConfig, processingDhcp: false };
return newState; return newState;
}, },
[actions.setDhcpConfigRequest]: state => ({ ...state, processingConfig: true }),
[actions.setDhcpConfigFailure]: state => ({ ...state, processingConfig: false }),
[actions.setDhcpConfigSuccess]: (state, { payload }) => {
const { config } = state;
const newConfig = { ...config, ...payload };
const newState = { ...state, config: newConfig, processingConfig: false };
return newState;
},
}, { }, {
processing: true, processing: true,
processingStatus: false, processingStatus: false,
processingInterfaces: false, processingInterfaces: false,
processingDhcp: false, processingDhcp: false,
processingConfig: false,
config: { config: {
enabled: false, enabled: false,
}, },