Add button to the dashboard page for enable/disable filtering

Closes #333
This commit is contained in:
Ildar Kamalov 2018-09-26 17:12:31 +03:00
parent 381b96a4b1
commit 2cf22898dd
3 changed files with 69 additions and 1 deletions

View File

@ -98,6 +98,46 @@ export const initSettings = settingsList => async (dispatch) => {
} }
}; };
export const getFilteringRequest = createAction('GET_FILTERING_REQUEST');
export const getFilteringFailure = createAction('GET_FILTERING_FAILURE');
export const getFilteringSuccess = createAction('GET_FILTERING_SUCCESS');
export const getFiltering = () => async (dispatch) => {
dispatch(getFilteringRequest());
try {
const filteringStatus = await apiClient.getFilteringStatus();
dispatch(getFilteringSuccess(filteringStatus.enabled));
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(getFilteringFailure());
}
};
export const toggleFilteringRequest = createAction('TOGGLE_FILTERING_REQUEST');
export const toggleFilteringFailure = createAction('TOGGLE_FILTERING_FAILURE');
export const toggleFilteringSuccess = createAction('TOGGLE_FILTERING_SUCCESS');
export const toggleFiltering = status => async (dispatch) => {
dispatch(toggleFilteringRequest());
let successMessage = '';
try {
if (status) {
successMessage = 'Disabled filtering';
await apiClient.disableFiltering();
} else {
successMessage = 'Enabled filtering';
await apiClient.enableFiltering();
}
dispatch(addSuccessToast(successMessage));
dispatch(toggleFilteringSuccess());
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(toggleFilteringFailure());
}
};
export const dnsStatusRequest = createAction('DNS_STATUS_REQUEST'); export const dnsStatusRequest = createAction('DNS_STATUS_REQUEST');
export const dnsStatusFailure = createAction('DNS_STATUS_FAILURE'); export const dnsStatusFailure = createAction('DNS_STATUS_FAILURE');
export const dnsStatusSuccess = createAction('DNS_STATUS_SUCCESS'); export const dnsStatusSuccess = createAction('DNS_STATUS_SUCCESS');

View File

@ -20,6 +20,19 @@ class Dashboard extends Component {
this.props.getStats(); this.props.getStats();
this.props.getStatsHistory(); this.props.getStatsHistory();
this.props.getTopStats(); this.props.getTopStats();
this.props.getFiltering();
}
getToggleFilteringButton = () => {
const { isFilteringEnabled } = this.props.dashboard;
const buttonText = isFilteringEnabled ? 'Disable' : 'Enable';
const buttonClass = isFilteringEnabled ? 'btn-outline-secondary' : 'btn-outline-success';
return (
<button type="button" className={`btn btn-sm mr-2 ${buttonClass}`} onClick={() => this.props.toggleFiltering(isFilteringEnabled)}>
{buttonText} protection
</button>
);
} }
render() { render() {
@ -37,6 +50,7 @@ class Dashboard extends Component {
<Fragment> <Fragment>
<PageTitle title="Dashboard"> <PageTitle title="Dashboard">
<div className="page-title__actions"> <div className="page-title__actions">
{this.getToggleFilteringButton()}
{refreshFullButton} {refreshFullButton}
</div> </div>
</PageTitle> </PageTitle>
@ -97,9 +111,10 @@ Dashboard.propTypes = {
getStats: PropTypes.func, getStats: PropTypes.func,
getStatsHistory: PropTypes.func, getStatsHistory: PropTypes.func,
getTopStats: PropTypes.func, getTopStats: PropTypes.func,
disableDns: PropTypes.func,
dashboard: PropTypes.object, dashboard: PropTypes.object,
isCoreRunning: PropTypes.bool, isCoreRunning: PropTypes.bool,
getFiltering: PropTypes.func,
toggleFiltering: PropTypes.func,
}; };
export default Dashboard; export default Dashboard;

View File

@ -129,6 +129,18 @@ const dashboard = handleActions({
return state; return state;
}, },
[actions.getFilteringRequest]: state => ({ ...state, processingFiltering: true }),
[actions.getFilteringFailure]: state => ({ ...state, processingFiltering: false }),
[actions.getFilteringSuccess]: (state, { payload }) => {
const newState = { ...state, isFilteringEnabled: payload, processingFiltering: false };
return newState;
},
[actions.toggleFilteringSuccess]: (state) => {
const newSetting = { ...state, isFilteringEnabled: !state.isFilteringEnabled };
return newSetting;
},
}, { }, {
processing: true, processing: true,
isCoreRunning: false, isCoreRunning: false,
@ -136,6 +148,7 @@ const dashboard = handleActions({
processingStats: true, processingStats: true,
logStatusProcessing: false, logStatusProcessing: false,
processingVersion: true, processingVersion: true,
processingFiltering: true,
}); });
const queryLogs = handleActions({ const queryLogs = handleActions({