From 3a74dfdfa4b41b57d19a40c116abf1f85713ed27 Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Thu, 11 Oct 2018 10:57:36 +0300 Subject: [PATCH] Add client requests for toggle protection Closes #333 --- client/src/actions/index.js | 22 +++++++++++----------- client/src/api/Api.js | 12 ++++++++++++ client/src/components/Dashboard/index.js | 11 +++++------ client/src/components/Header/index.js | 6 +++--- client/src/reducers/index.js | 9 ++++++--- 5 files changed, 37 insertions(+), 23 deletions(-) diff --git a/client/src/actions/index.js b/client/src/actions/index.js index cb6cd1ed..7a28a600 100644 --- a/client/src/actions/index.js +++ b/client/src/actions/index.js @@ -113,28 +113,28 @@ export const getFiltering = () => async (dispatch) => { } }; -export const toggleFilteringRequest = createAction('TOGGLE_FILTERING_REQUEST'); -export const toggleFilteringFailure = createAction('TOGGLE_FILTERING_FAILURE'); -export const toggleFilteringSuccess = createAction('TOGGLE_FILTERING_SUCCESS'); +export const toggleProtectionRequest = createAction('TOGGLE_PROTECTION_REQUEST'); +export const toggleProtectionFailure = createAction('TOGGLE_PROTECTION_FAILURE'); +export const toggleProtectionSuccess = createAction('TOGGLE_PROTECTION_SUCCESS'); -export const toggleFiltering = status => async (dispatch) => { - dispatch(toggleFilteringRequest()); +export const toggleProtection = status => async (dispatch) => { + dispatch(toggleProtectionRequest()); let successMessage = ''; try { if (status) { - successMessage = 'Disabled filtering'; - await apiClient.disableFiltering(); + successMessage = 'Disabled protection'; + await apiClient.disableGlobalProtection(); } else { - successMessage = 'Enabled filtering'; - await apiClient.enableFiltering(); + successMessage = 'Enabled protection'; + await apiClient.enableGlobalProtection(); } dispatch(addSuccessToast(successMessage)); - dispatch(toggleFilteringSuccess()); + dispatch(toggleProtectionSuccess()); } catch (error) { dispatch(addErrorToast({ error })); - dispatch(toggleFilteringFailure()); + dispatch(toggleProtectionFailure()); } }; diff --git a/client/src/api/Api.js b/client/src/api/Api.js index b56d4307..636b0ddd 100644 --- a/client/src/api/Api.js +++ b/client/src/api/Api.js @@ -33,6 +33,8 @@ export default class Api { 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' }; + GLOBAL_ENABLE_PROTECTION = { path: 'enable_protection', method: 'POST' }; + GLOBAL_DISABLE_PROTECTION = { path: 'disable_protection', method: 'POST' }; restartGlobalFiltering() { const { path, method } = this.GLOBAL_RESTART; @@ -123,6 +125,16 @@ export default class Api { return this.makeRequest(path, method); } + enableGlobalProtection() { + const { path, method } = this.GLOBAL_ENABLE_PROTECTION; + return this.makeRequest(path, method); + } + + disableGlobalProtection() { + const { path, method } = this.GLOBAL_DISABLE_PROTECTION; + return this.makeRequest(path, method); + } + // Filtering FILTERING_STATUS = { path: 'filtering/status', method: 'GET' }; FILTERING_ENABLE = { path: 'filtering/enable', method: 'POST' }; diff --git a/client/src/components/Dashboard/index.js b/client/src/components/Dashboard/index.js index d847fb40..28d52f11 100644 --- a/client/src/components/Dashboard/index.js +++ b/client/src/components/Dashboard/index.js @@ -20,16 +20,15 @@ class Dashboard extends Component { this.props.getStats(); this.props.getStatsHistory(); this.props.getTopStats(); - this.props.getFiltering(); } getToggleFilteringButton = () => { - const { isFilteringEnabled } = this.props.dashboard; - const buttonText = isFilteringEnabled ? 'Disable' : 'Enable'; - const buttonClass = isFilteringEnabled ? 'btn-gray' : 'btn-success'; + const { protectionEnabled } = this.props.dashboard; + const buttonText = protectionEnabled ? 'Disable' : 'Enable'; + const buttonClass = protectionEnabled ? 'btn-gray' : 'btn-success'; return ( - ); @@ -114,7 +113,7 @@ Dashboard.propTypes = { dashboard: PropTypes.object, isCoreRunning: PropTypes.bool, getFiltering: PropTypes.func, - toggleFiltering: PropTypes.func, + toggleProtection: PropTypes.func, }; export default Dashboard; diff --git a/client/src/components/Header/index.js b/client/src/components/Header/index.js index b360563b..845cdd92 100644 --- a/client/src/components/Header/index.js +++ b/client/src/components/Header/index.js @@ -26,8 +26,8 @@ class Header extends Component { const { dashboard } = this.props; const badgeClass = classnames({ 'badge dns-status': true, - 'badge-success': dashboard.isCoreRunning, - 'badge-danger': !dashboard.isCoreRunning, + 'badge-success': dashboard.protectionEnabled, + 'badge-danger': !dashboard.protectionEnabled, }); return ( @@ -44,7 +44,7 @@ class Header extends Component { {!dashboard.proccessing && - {dashboard.isCoreRunning ? 'ON' : 'OFF'} + {dashboard.protectionEnabled ? 'ON' : 'OFF'} } diff --git a/client/src/reducers/index.js b/client/src/reducers/index.js index 8fc2cb3c..9fbd8f73 100644 --- a/client/src/reducers/index.js +++ b/client/src/reducers/index.js @@ -48,6 +48,7 @@ const dashboard = handleActions({ dns_address: dnsAddress, querylog_enabled: queryLogEnabled, upstream_dns: upstreamDns, + protection_enabled: protectionEnabled, } = payload; const newState = { ...state, @@ -58,6 +59,7 @@ const dashboard = handleActions({ dnsAddress, queryLogEnabled, upstreamDns: upstreamDns.join('\n'), + protectionEnabled, }; return newState; }, @@ -134,9 +136,9 @@ const dashboard = handleActions({ return newState; }, - [actions.toggleFilteringSuccess]: (state) => { - const newSetting = { ...state, isFilteringEnabled: !state.isFilteringEnabled }; - return newSetting; + [actions.toggleProtectionSuccess]: (state) => { + const newState = { ...state, protectionEnabled: !state.protectionEnabled }; + return newState; }, [actions.handleUpstreamChange]: (state, { payload }) => { @@ -152,6 +154,7 @@ const dashboard = handleActions({ processingVersion: true, processingFiltering: true, upstreamDns: [], + protectionEnabled: false, }); const queryLogs = handleActions({