From 92f4c34224ab7927b02edde829f2d9653a00a854 Mon Sep 17 00:00:00 2001 From: ArtemBaskal Date: Wed, 27 May 2020 18:35:05 +0300 Subject: [PATCH] Make variable names more expressive --- client/src/components/Dashboard/Clients.js | 59 ++++++++-------------- client/src/helpers/constants.js | 7 +-- client/src/helpers/helpers.js | 44 +++++++++------- 3 files changed, 51 insertions(+), 59 deletions(-) diff --git a/client/src/components/Dashboard/Clients.js b/client/src/components/Dashboard/Clients.js index 660d3140472..8d35eae183b 100644 --- a/client/src/components/Dashboard/Clients.js +++ b/client/src/components/Dashboard/Clients.js @@ -6,14 +6,15 @@ import { Trans, withTranslation } from 'react-i18next'; import Card from '../ui/Card'; import Cell from '../ui/Cell'; -import { getPercent, isClientInIpsOrCidrs } from '../../helpers/helpers'; -import { BLOCKED_CLIENT, STATUS_COLORS } from '../../helpers/constants'; +import { getPercent, getIpMatchListStatus } from '../../helpers/helpers'; +import { IP_MATCH_LIST_STATUS, STATUS_COLORS } from '../../helpers/constants'; import { formatClientCell } from '../../helpers/formatClientCell'; const getClientsPercentColor = (percent) => { if (percent > 50) { return STATUS_COLORS.green; - } if (percent > 10) { + } + if (percent > 10) { return STATUS_COLORS.yellow; } return STATUS_COLORS.red; @@ -27,20 +28,18 @@ const countCell = (dnsQueries) => function cell(row) { return ; }; -const renderBlockingButton = (blocked, ip, handleClick, processing) => { - let buttonProps = { - className: 'btn-outline-danger', - text: 'block_btn', - type: 'block', - }; - - if (blocked) { - buttonProps = { +const renderBlockingButton = (ipMatchListStatus, ip, handleClick, processing) => { + const buttonProps = ipMatchListStatus === IP_MATCH_LIST_STATUS.NOT_FOUND + ? { + className: 'btn-outline-danger', + text: 'block_btn', + type: 'block', + } + : { className: 'btn-outline-secondary', text: 'unblock_btn', type: 'unblock', }; - } return (
@@ -56,25 +55,18 @@ const renderBlockingButton = (blocked, ip, handleClick, processing) => { ); }; -const isBlockedClient = (rawClients, client) => { - if (!rawClients || !client) { - return false; - } - return isClientInIpsOrCidrs(rawClients, client); -}; - const clientCell = (t, toggleClientStatus, processing, disallowedClients) => function cell(row) { const { value } = row; - const blocked = isBlockedClient(disallowedClients, value); + const ipMatchListStatus = getIpMatchListStatus(value, disallowedClients); return ( - -
- {formatClientCell(row, t)} -
- {blocked !== BLOCKED_CLIENT.CIDR - && renderBlockingButton(blocked, value, toggleClientStatus, processing)} -
+ +
+ {formatClientCell(row, t)} +
+ {ipMatchListStatus !== IP_MATCH_LIST_STATUS.CIDR + && renderBlockingButton(ipMatchListStatus, value, toggleClientStatus, processing)} +
); }; @@ -130,15 +122,8 @@ const Clients = ({ const { ip } = rowInfo.original; - if (isBlockedClient(disallowedClients, ip)) { - return { - className: 'red', - }; - } - - return { - className: '', - }; + return getIpMatchListStatus(ip, disallowedClients) + === IP_MATCH_LIST_STATUS.NOT_FOUND ? {} : { className: 'red' }; }} /> diff --git a/client/src/helpers/constants.js b/client/src/helpers/constants.js index 84f6898a38c..7da7d054d12 100644 --- a/client/src/helpers/constants.js +++ b/client/src/helpers/constants.js @@ -380,7 +380,8 @@ export const DNS_REQUEST_OPTIONS = { FASTEST_ADDR: 'fastest_addr', }; -export const BLOCKED_CLIENT = { - IP: 'IP', - CIDR: 'CIDR', +export const IP_MATCH_LIST_STATUS = { + NOT_FOUND: 'NOT_FOUND', // not found in the list + EXACT: 'EXACT', // found exact match (ip === ip) + CIDR: 'CIDR', // the ip is in the specified CIDR range }; diff --git a/client/src/helpers/helpers.js b/client/src/helpers/helpers.js index 0fdaf9ba78b..c03d9a00548 100644 --- a/client/src/helpers/helpers.js +++ b/client/src/helpers/helpers.js @@ -26,7 +26,7 @@ import { DEFAULT_LANGUAGE, FILTERED_STATUS, FILTERED, - BLOCKED_CLIENT, + IP_MATCH_LIST_STATUS, } from './constants'; /** @@ -501,7 +501,7 @@ export const normalizeMultiline = (multiline) => `${normalizeTextarea(multiline) * @param cidr {string} * @returns {boolean} */ -export const isIpInCidr = (ip, cidr) => { +export const isIpMatchCidr = (ip, cidr) => { try { const [cidrIp] = cidr.split('/'); const cidrIpVersion = ipaddr.parse(cidrIp) @@ -519,24 +519,30 @@ export const isIpInCidr = (ip, cidr) => { }; /** - * @param rawClients {string} - * @param currentClient {string} - * @returns {boolean | 'CIDR' | 'IP'} + * @param ip {string} + * @param list {string} + * @returns {'EXACT' | 'CIDR' | 'NOT_FOND'} */ -export const isClientInIpsOrCidrs = (rawClients, currentClient) => rawClients.split('\n') - .reduce((isClientInList, rawClient) => { - if (isClientInList) { - return isClientInList; - } +export const getIpMatchListStatus = (ip, list) => { + if (!ip || !list) { + return IP_MATCH_LIST_STATUS.NOT_FOUND; + } - if (rawClient === currentClient) { - return BLOCKED_CLIENT.IP; - } + return list.split('\n') + .reduce((ipMatchList, listItem) => { + if (ipMatchList !== IP_MATCH_LIST_STATUS.NOT_FOUND) { + return ipMatchList; + } - if (rawClient.includes('/') && isIpInCidr(currentClient, rawClient)) { - return BLOCKED_CLIENT.CIDR; - } + if (listItem === ip) { + return IP_MATCH_LIST_STATUS.EXACT; + } - return false; - }, - false); + if (listItem.includes('/') && isIpMatchCidr(ip, listItem)) { + return IP_MATCH_LIST_STATUS.CIDR; + } + + return IP_MATCH_LIST_STATUS.NOT_FOUND; + }, + IP_MATCH_LIST_STATUS.NOT_FOUND); +};