From f727f999f9eb61725a0fb54f5d46ab7570507b0a Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Fri, 21 Sep 2018 18:08:39 +0300 Subject: [PATCH 1/3] Add a test upstreams button Closes #321 --- client/src/actions/index.js | 33 ++++++++++++++++++++++ client/src/api/Api.js | 10 +++++++ client/src/components/Filters/UserRules.js | 2 +- client/src/components/Settings/Upstream.js | 19 +++++++++++++ client/src/components/Settings/index.js | 6 ++++ client/src/containers/Settings.js | 3 +- client/src/reducers/index.js | 7 ++++- 7 files changed, 77 insertions(+), 3 deletions(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index c4700198..862c36cf 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -420,3 +420,36 @@ export const setUpstream = url => async (dispatch) => { dispatch(setUpstreamFailure()); } }; + +export const testUpstreamRequest = createAction('TEST_UPSTREAM_REQUEST'); +export const testUpstreamFailure = createAction('TEST_UPSTREAM_FAILURE'); +export const testUpstreamSuccess = createAction('TEST_UPSTREAM_SUCCESS'); + +export const testUpstream = servers => async (dispatch) => { + dispatch(testUpstreamRequest()); + try { + if (servers.length > 0) { + const upstreamResponse = await apiClient.testUpstream(servers); + + const testMessages = Object.keys(upstreamResponse).map((key) => { + const message = upstreamResponse[key]; + if (message !== 'OK') { + dispatch(addErrorToast({ error: `Server "${key}": could not be used, please check that you've written it correctly` })); + } + return message; + }); + + if (testMessages.every(message => message === testMessages[0])) { + dispatch(addSuccessToast('All servers is OK')); + } + + dispatch(testUpstreamSuccess()); + } else { + dispatch(addErrorToast({ error: 'No servers specified' })); + dispatch(testUpstreamFailure()); + } + } catch (error) { + dispatch(addErrorToast({ error })); + dispatch(testUpstreamFailure()); + } +}; diff --git a/client/src/api/Api.js b/client/src/api/Api.js index 4408eb51..b56d4307 100644 --- a/client/src/api/Api.js +++ b/client/src/api/Api.js @@ -31,6 +31,7 @@ export default class Api { GLOBAL_QUERY_LOG_ENABLE = { path: 'querylog_enable', method: 'POST' }; GLOBAL_QUERY_LOG_DISABLE = { path: 'querylog_disable', method: 'POST' }; GLOBAL_SET_UPSTREAM_DNS = { path: 'set_upstream_dns', method: 'POST' }; + GLOBAL_TEST_UPSTREAM_DNS = { path: 'test_upstream_dns', method: 'POST' }; GLOBAL_VERSION = { path: 'version.json', method: 'GET' }; restartGlobalFiltering() { @@ -108,6 +109,15 @@ export default class Api { return this.makeRequest(path, method, config); } + testUpstream(servers) { + const { path, method } = this.GLOBAL_TEST_UPSTREAM_DNS; + const config = { + data: servers, + header: { 'Content-Type': 'text/plain' }, + }; + return this.makeRequest(path, method, config); + } + getGlobalVersion() { const { path, method } = this.GLOBAL_VERSION; return this.makeRequest(path, method); diff --git a/client/src/components/Filters/UserRules.js b/client/src/components/Filters/UserRules.js index 58cc059d..c748c3bc 100644 --- a/client/src/components/Filters/UserRules.js +++ b/client/src/components/Filters/UserRules.js @@ -27,7 +27,7 @@ export default class UserRules extends Component { type="submit" onClick={this.handleSubmit} > - Apply... + Apply diff --git a/client/src/components/Settings/Upstream.js b/client/src/components/Settings/Upstream.js index a2645854..db43943e 100644 --- a/client/src/components/Settings/Upstream.js +++ b/client/src/components/Settings/Upstream.js @@ -1,5 +1,6 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; +import classnames from 'classnames'; import Card from '../ui/Card'; export default class Upstream extends Component { @@ -13,7 +14,16 @@ export default class Upstream extends Component { this.props.handleUpstreamSubmit(); }; + handleTest = () => { + this.props.handleUpstreamTest(); + } + render() { + const testButtonClass = classnames({ + 'btn btn-primary btn-standart mr-2': true, + 'btn btn-primary btn-standart mr-2 btn-loading': this.props.processingTestUpstream, + }); + return (
+
diff --git a/client/src/containers/Settings.js b/client/src/containers/Settings.js index 3c1e5664..d53da73b 100644 --- a/client/src/containers/Settings.js +++ b/client/src/containers/Settings.js @@ -1,5 +1,5 @@ import { connect } from 'react-redux'; -import { initSettings, toggleSetting, handleUpstreamChange, setUpstream } from '../actions'; +import { initSettings, toggleSetting, handleUpstreamChange, setUpstream, testUpstream } from '../actions'; import Settings from '../components/Settings'; const mapStateToProps = (state) => { @@ -13,6 +13,7 @@ const mapDispatchToProps = { toggleSetting, handleUpstreamChange, setUpstream, + testUpstream, }; export default connect( diff --git a/client/src/reducers/index.js b/client/src/reducers/index.js index 86c4d49a..3372cf79 100644 --- a/client/src/reducers/index.js +++ b/client/src/reducers/index.js @@ -31,9 +31,14 @@ const settings = handleActions({ const { upstream } = payload; return { ...state, upstream }; }, + + [actions.testUpstreamRequest]: state => ({ ...state, processingTestUpstream: true }), + [actions.testUpstreamFailure]: state => ({ ...state, processingTestUpstream: false }), + [actions.testUpstreamSuccess]: state => ({ ...state, processingTestUpstream: false }), }, { processing: true, - processingUpstream: true, + processingTestUpstream: false, + processingSetUpstream: false, upstream: '', }); From c2a2b3ea6a90a953decc9fa8e6058b96b44225f6 Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Fri, 21 Sep 2018 18:50:06 +0300 Subject: [PATCH 2/3] Check upstream length in component --- client/src/actions/index.js | 27 ++++++++++--------------- client/src/components/Settings/index.js | 6 +++++- client/src/containers/Settings.js | 3 ++- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index 862c36cf..5c0341fc 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -428,26 +428,21 @@ export const testUpstreamSuccess = createAction('TEST_UPSTREAM_SUCCESS'); export const testUpstream = servers => async (dispatch) => { dispatch(testUpstreamRequest()); try { - if (servers.length > 0) { - const upstreamResponse = await apiClient.testUpstream(servers); + const upstreamResponse = await apiClient.testUpstream(servers); - const testMessages = Object.keys(upstreamResponse).map((key) => { - const message = upstreamResponse[key]; - if (message !== 'OK') { - dispatch(addErrorToast({ error: `Server "${key}": could not be used, please check that you've written it correctly` })); - } - return message; - }); - - if (testMessages.every(message => message === testMessages[0])) { - dispatch(addSuccessToast('All servers is OK')); + const testMessages = Object.keys(upstreamResponse).map((key) => { + const message = upstreamResponse[key]; + if (message !== 'OK') { + dispatch(addErrorToast({ error: `Server "${key}": could not be used, please check that you've written it correctly` })); } + return message; + }); - dispatch(testUpstreamSuccess()); - } else { - dispatch(addErrorToast({ error: 'No servers specified' })); - dispatch(testUpstreamFailure()); + if (testMessages.every(message => message === testMessages[0])) { + dispatch(addSuccessToast('All servers is OK')); } + + dispatch(testUpstreamSuccess()); } catch (error) { dispatch(addErrorToast({ error })); dispatch(testUpstreamFailure()); diff --git a/client/src/components/Settings/index.js b/client/src/components/Settings/index.js index ed3a9693..14140c4f 100644 --- a/client/src/components/Settings/index.js +++ b/client/src/components/Settings/index.js @@ -44,7 +44,11 @@ export default class Settings extends Component { }; handleUpstreamTest = () => { - this.props.testUpstream(this.props.settings.upstream); + if (this.props.settings.upstream.length > 0) { + this.props.testUpstream(this.props.settings.upstream); + } else { + this.props.addErrorToast({ error: 'No servers specified' }); + } }; renderSettings = (settings) => { diff --git a/client/src/containers/Settings.js b/client/src/containers/Settings.js index d53da73b..3a790799 100644 --- a/client/src/containers/Settings.js +++ b/client/src/containers/Settings.js @@ -1,5 +1,5 @@ import { connect } from 'react-redux'; -import { initSettings, toggleSetting, handleUpstreamChange, setUpstream, testUpstream } from '../actions'; +import { initSettings, toggleSetting, handleUpstreamChange, setUpstream, testUpstream, addErrorToast } from '../actions'; import Settings from '../components/Settings'; const mapStateToProps = (state) => { @@ -14,6 +14,7 @@ const mapDispatchToProps = { handleUpstreamChange, setUpstream, testUpstream, + addErrorToast, }; export default connect( From 1c1b952d485e572eb2320b641429e07757b2d65f Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Fri, 21 Sep 2018 18:57:27 +0300 Subject: [PATCH 3/3] Fix message checking --- client/src/actions/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index 5c0341fc..df7b5d1c 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -438,7 +438,7 @@ export const testUpstream = servers => async (dispatch) => { return message; }); - if (testMessages.every(message => message === testMessages[0])) { + if (testMessages.every(message => message === 'OK')) { dispatch(addSuccessToast('All servers is OK')); }