Merge: + client: handle client block and unblock from the top clients table

Closes #896

Squashed commit of the following:

commit 776de2ae0a62823b8968cff79a9fa7ba350d7f1c
Author: Ildar Kamalov <i.kamalov@adguard.com>
Date:   Thu Jan 30 11:13:41 2020 +0300

    - client: fix normalizeTextarea and blocking button

commit 399e6bc3893093632b09247eaf6493521a668c84
Author: Ildar Kamalov <i.kamalov@adguard.com>
Date:   Wed Jan 29 17:19:50 2020 +0300

    + client: handle client block and unblock from the top clients table
This commit is contained in:
Ildar Kamalov
2020-01-30 13:58:54 +03:00
parent 76be272787
commit 5c814b29e1
14 changed files with 195 additions and 39 deletions

View File

@@ -1,7 +1,11 @@
import { createAction } from 'redux-actions';
import { t } from 'i18next';
import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index';
import { getStats, getStatsConfig } from './stats';
import { normalizeTextarea } from '../helpers/helpers';
import { ACTION } from '../helpers/constants';
export const getAccessListRequest = createAction('GET_ACCESS_LIST_REQUEST');
export const getAccessListFailure = createAction('GET_ACCESS_LIST_FAILURE');
@@ -28,9 +32,9 @@ export const setAccessList = config => async (dispatch) => {
const { allowed_clients, disallowed_clients, blocked_hosts } = config;
const values = {
allowed_clients: (allowed_clients && normalizeTextarea(allowed_clients)) || [],
disallowed_clients: (disallowed_clients && normalizeTextarea(disallowed_clients)) || [],
blocked_hosts: (blocked_hosts && normalizeTextarea(blocked_hosts)) || [],
allowed_clients: normalizeTextarea(allowed_clients),
disallowed_clients: normalizeTextarea(disallowed_clients),
blocked_hosts: normalizeTextarea(blocked_hosts),
};
await apiClient.setAccessList(values);
@@ -41,3 +45,43 @@ export const setAccessList = config => async (dispatch) => {
dispatch(setAccessListFailure());
}
};
export const toggleClientBlockRequest = createAction('TOGGLE_CLIENT_BLOCK_REQUEST');
export const toggleClientBlockFailure = createAction('TOGGLE_CLIENT_BLOCK_FAILURE');
export const toggleClientBlockSuccess = createAction('TOGGLE_CLIENT_BLOCK_SUCCESS');
export const toggleClientBlock = (type, ip) => async (dispatch, getState) => {
dispatch(toggleClientBlockRequest());
try {
const { allowed_clients, disallowed_clients, blocked_hosts } = getState().access;
let updatedDisallowedClients = normalizeTextarea(disallowed_clients);
if (type === ACTION.unblock && updatedDisallowedClients.includes(ip)) {
updatedDisallowedClients = updatedDisallowedClients.filter(client => client !== ip);
} else if (type === ACTION.block && !updatedDisallowedClients.includes(ip)) {
updatedDisallowedClients.push(ip);
}
const values = {
allowed_clients: normalizeTextarea(allowed_clients),
blocked_hosts: normalizeTextarea(blocked_hosts),
disallowed_clients: updatedDisallowedClients,
};
await apiClient.setAccessList(values);
dispatch(toggleClientBlockSuccess());
if (type === ACTION.unblock) {
dispatch(addSuccessToast(t('client_unblocked', { ip })));
} else if (type === ACTION.block) {
dispatch(addSuccessToast(t('client_blocked', { ip })));
}
dispatch(getStats());
dispatch(getStatsConfig());
dispatch(getAccessList());
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(toggleClientBlockFailure());
}
};