mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-07-29 03:59:23 +00:00
Closes #896 Squashed commit of the following: commit 66b781438aa668a16b19455c3e0dc5100417d869 Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Mon Feb 3 12:59:25 2020 +0300 - client: block/unblock client without requesting stats commit e70f62738d549e32339bae3d5e996a912b99c21d Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Mon Feb 3 12:22:13 2020 +0300 - client: get current access settings before set commit 65c59d1d55f3255f33f917b61b6ab2c6c2e54271 Author: Ildar Kamalov <i.kamalov@adguard.com> Date: Mon Feb 3 12:21:12 2020 +0300 * client: mode svg-url-loader to devDependencies
85 lines
3.2 KiB
JavaScript
85 lines
3.2 KiB
JavaScript
import { createAction } from 'redux-actions';
|
|
import { t } from 'i18next';
|
|
|
|
import apiClient from '../api/Api';
|
|
import { addErrorToast, addSuccessToast } from './index';
|
|
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');
|
|
export const getAccessListSuccess = createAction('GET_ACCESS_LIST_SUCCESS');
|
|
|
|
export const getAccessList = () => async (dispatch) => {
|
|
dispatch(getAccessListRequest());
|
|
try {
|
|
const data = await apiClient.getAccessList();
|
|
dispatch(getAccessListSuccess(data));
|
|
} catch (error) {
|
|
dispatch(addErrorToast({ error }));
|
|
dispatch(getAccessListFailure());
|
|
}
|
|
};
|
|
|
|
export const setAccessListRequest = createAction('SET_ACCESS_LIST_REQUEST');
|
|
export const setAccessListFailure = createAction('SET_ACCESS_LIST_FAILURE');
|
|
export const setAccessListSuccess = createAction('SET_ACCESS_LIST_SUCCESS');
|
|
|
|
export const setAccessList = config => async (dispatch) => {
|
|
dispatch(setAccessListRequest());
|
|
try {
|
|
const { allowed_clients, disallowed_clients, blocked_hosts } = config;
|
|
|
|
const values = {
|
|
allowed_clients: normalizeTextarea(allowed_clients),
|
|
disallowed_clients: normalizeTextarea(disallowed_clients),
|
|
blocked_hosts: normalizeTextarea(blocked_hosts),
|
|
};
|
|
|
|
await apiClient.setAccessList(values);
|
|
dispatch(setAccessListSuccess());
|
|
dispatch(addSuccessToast('access_settings_saved'));
|
|
} catch (error) {
|
|
dispatch(addErrorToast({ error }));
|
|
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) => {
|
|
dispatch(toggleClientBlockRequest());
|
|
try {
|
|
const {
|
|
allowed_clients, disallowed_clients, blocked_hosts,
|
|
} = await apiClient.getAccessList();
|
|
let updatedDisallowedClients = 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,
|
|
blocked_hosts,
|
|
disallowed_clients: updatedDisallowedClients,
|
|
};
|
|
|
|
await apiClient.setAccessList(values);
|
|
dispatch(toggleClientBlockSuccess(values));
|
|
|
|
if (type === ACTION.unblock) {
|
|
dispatch(addSuccessToast(t('client_unblocked', { ip })));
|
|
} else if (type === ACTION.block) {
|
|
dispatch(addSuccessToast(t('client_blocked', { ip })));
|
|
}
|
|
} catch (error) {
|
|
dispatch(addErrorToast({ error }));
|
|
dispatch(toggleClientBlockFailure());
|
|
}
|
|
};
|