Updates #951. Squashed commit of the following: commit 6b840fd516f5a87fde0420e3aceb9c239b22c974 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 29 19:53:03 2023 +0300 client: imp docs more commit 7fc8f0363fbe4c4266cb0f67428fe4d18c351d2d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 29 19:40:00 2023 +0300 client: imp docs commit 00bc14d5760614f2797714cdc2c4c19b1a94b86e Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 18:43:49 2023 +0300 try to fix lock file commit d749df74b576091e0b58928d86ea8b3b49f919da Merge: c69f9230be1f6229e5Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Aug 28 18:14:02 2023 +0300 Merge branch 'master' into 951-blocked-services-schedule-api commit c69f9230b12f7c983db06b74324b3df77d74b32b Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 17:16:20 2023 +0300 revert eslintrc commit b37916c2dff0ddea5293d87570bb58e3443d2d21 Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 12:02:39 2023 +0300 fix translations commit f5bb67d81506c687d0abd580049a3eee0af808e0 Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 11:43:57 2023 +0300 fix helpers commit 13ec6a8b3a0acfb62762ae7e46c6e98eb7c82212 Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 11:24:57 2023 +0300 remove todo commit 23724ec2fd683ed17b9f1cee841ad9aaf4c9d04f Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 09:56:56 2023 +0300 add clients schedule form commit 84d29e558a329068e64e7a95ee183946aa4515b5 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 25 17:44:40 2023 +0300 fix schedule form commit 83e4017688082e9eb670091d5a24d98157050502 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 18 12:58:16 2023 +0300 remove unused commit ef2b68e138da382e3cf42586ae604e12d9493504 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 18 12:57:37 2023 +0300 client: fix translation string commit 32ea80c968f52f18adbc811b2f06874644cdfe20 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 18 12:26:26 2023 +0300 wip schedule commit 9b770873859186c9424c8d108812e32ddff33bad Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 21 14:29:50 2023 +0300 all: imp naming commit ea4e9514ea3b264bcce7f2a301db817de4e87059 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jul 19 18:09:27 2023 +0300 all: imp code commit 98a705bdaa5c1e79394c73e5d75af2416fe9f297 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 18 18:23:26 2023 +0300 all: imp naming commit 4f84b55c7bfc9f7b680feac0ec45f5ea9189299a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 14 15:01:17 2023 +0300 all: add global schedule api commit 87cf1646869ee9138964b47a27b7493674c8854a Merge: cabb80ac12adc8624cAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 14 12:09:29 2023 +0300 Merge branch 'master' into 951-blocked-services-schedule-api commit cabb80ac16de437a8118bb0166479574379c97a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 13 13:37:23 2023 +0300 openapi: fix typo commit 2279b03acbcfc3d76216f8aaf30ae1c7894127bc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 13 12:26:19 2023 +0300 all: imp docs ... and 3 more commits
458 lines
16 KiB
JavaScript
458 lines
16 KiB
JavaScript
import React, { useState } from 'react';
|
|
import { connect, useSelector } from 'react-redux';
|
|
import PropTypes from 'prop-types';
|
|
import {
|
|
Field, FieldArray, reduxForm, formValueSelector,
|
|
} from 'redux-form';
|
|
import { Trans, withTranslation } from 'react-i18next';
|
|
import flow from 'lodash/flow';
|
|
import Select from 'react-select';
|
|
|
|
import i18n from '../../../i18n';
|
|
import Tabs from '../../ui/Tabs';
|
|
import Examples from '../Dns/Upstream/Examples';
|
|
import { ScheduleForm } from '../../Filters/Services/ScheduleForm';
|
|
import { toggleAllServices, trimLinesAndRemoveEmpty, captitalizeWords } from '../../../helpers/helpers';
|
|
import {
|
|
renderInputField,
|
|
renderGroupField,
|
|
CheckboxField,
|
|
renderServiceField,
|
|
renderTextareaField,
|
|
} from '../../../helpers/form';
|
|
import { validateClientId, validateRequiredValue } from '../../../helpers/validators';
|
|
import { CLIENT_ID_LINK, FORM_NAME } from '../../../helpers/constants';
|
|
import './Service.css';
|
|
|
|
const settingsCheckboxes = [
|
|
{
|
|
name: 'use_global_settings',
|
|
placeholder: 'client_global_settings',
|
|
},
|
|
{
|
|
name: 'filtering_enabled',
|
|
placeholder: 'block_domain_use_filters_and_hosts',
|
|
},
|
|
{
|
|
name: 'safebrowsing_enabled',
|
|
placeholder: 'use_adguard_browsing_sec',
|
|
},
|
|
{
|
|
name: 'parental_enabled',
|
|
placeholder: 'use_adguard_parental',
|
|
},
|
|
];
|
|
|
|
const logAndStatsCheckboxes = [
|
|
{
|
|
name: 'ignore_querylog',
|
|
placeholder: 'ignore_query_log',
|
|
},
|
|
{
|
|
name: 'ignore_statistics',
|
|
placeholder: 'ignore_statistics',
|
|
},
|
|
];
|
|
const validate = (values) => {
|
|
const errors = {};
|
|
const { name, ids } = values;
|
|
errors.name = validateRequiredValue(name);
|
|
|
|
if (ids && ids.length) {
|
|
const idArrayErrors = [];
|
|
ids.forEach((id, idx) => {
|
|
idArrayErrors[idx] = validateRequiredValue(id) || validateClientId(id);
|
|
});
|
|
|
|
if (idArrayErrors.length) {
|
|
errors.ids = idArrayErrors;
|
|
}
|
|
}
|
|
return errors;
|
|
};
|
|
|
|
const renderFieldsWrapper = (placeholder, buttonTitle) => function cell(row) {
|
|
const {
|
|
fields,
|
|
} = row;
|
|
return (
|
|
<div className="form__group">
|
|
{fields.map((ip, index) => (
|
|
<div key={index} className="mb-1">
|
|
<Field
|
|
name={ip}
|
|
component={renderGroupField}
|
|
type="text"
|
|
className="form-control"
|
|
placeholder={placeholder}
|
|
isActionAvailable={index !== 0}
|
|
removeField={() => fields.remove(index)}
|
|
normalizeOnBlur={(data) => data.trim()}
|
|
/>
|
|
</div>
|
|
))}
|
|
<button
|
|
type="button"
|
|
className="btn btn-link btn-block btn-sm"
|
|
onClick={() => fields.push()}
|
|
title={buttonTitle}
|
|
>
|
|
<svg className="icon icon--24">
|
|
<use xlinkHref="#plus" />
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
// Should create function outside of component to prevent component re-renders
|
|
const renderFields = renderFieldsWrapper(i18n.t('form_enter_id'), i18n.t('form_add_id'));
|
|
|
|
const renderMultiselect = (props) => {
|
|
const { input, placeholder, options } = props;
|
|
|
|
return (
|
|
<Select
|
|
{...input}
|
|
options={options}
|
|
className="basic-multi-select"
|
|
classNamePrefix="select"
|
|
onChange={(value) => input.onChange(value)}
|
|
onBlur={() => input.onBlur(input.value)}
|
|
placeholder={placeholder}
|
|
blurInputOnSelect={false}
|
|
isMulti
|
|
/>
|
|
);
|
|
};
|
|
|
|
renderMultiselect.propTypes = {
|
|
input: PropTypes.object.isRequired,
|
|
placeholder: PropTypes.string,
|
|
options: PropTypes.array,
|
|
};
|
|
|
|
let Form = (props) => {
|
|
const {
|
|
t,
|
|
handleSubmit,
|
|
reset,
|
|
change,
|
|
submitting,
|
|
useGlobalSettings,
|
|
useGlobalServices,
|
|
blockedServicesSchedule,
|
|
toggleClientModal,
|
|
processingAdding,
|
|
processingUpdating,
|
|
invalid,
|
|
tagsOptions,
|
|
initialValues,
|
|
} = props;
|
|
const services = useSelector((store) => store?.services);
|
|
const { safe_search } = initialValues;
|
|
const safeSearchServices = { ...safe_search };
|
|
delete safeSearchServices.enabled;
|
|
|
|
const [activeTabLabel, setActiveTabLabel] = useState('settings');
|
|
|
|
const handleScheduleSubmit = (values) => {
|
|
change('blocked_services_schedule', values);
|
|
};
|
|
|
|
const tabs = {
|
|
settings: {
|
|
title: 'settings',
|
|
component: <div label="settings" title={props.t('main_settings')}>
|
|
<div className="form__label--bot form__label--bold">
|
|
{t('protection_section_label')}
|
|
</div>
|
|
{settingsCheckboxes.map((setting) => (
|
|
<div className="form__group" key={setting.name}>
|
|
<Field
|
|
name={setting.name}
|
|
type="checkbox"
|
|
component={CheckboxField}
|
|
placeholder={t(setting.placeholder)}
|
|
disabled={
|
|
setting.name !== 'use_global_settings'
|
|
? useGlobalSettings
|
|
: false
|
|
}
|
|
/>
|
|
</div>
|
|
))}
|
|
<div className="form__group">
|
|
<Field
|
|
name="safe_search.enabled"
|
|
type="checkbox"
|
|
component={CheckboxField}
|
|
placeholder={t('enforce_safe_search')}
|
|
disabled={useGlobalSettings}
|
|
/>
|
|
</div>
|
|
<div className='form__group--inner'>
|
|
{Object.keys(safeSearchServices).map((searchKey) => (
|
|
<div key={searchKey}>
|
|
<Field
|
|
name={`safe_search.${searchKey}`}
|
|
type="checkbox"
|
|
component={CheckboxField}
|
|
placeholder={captitalizeWords(searchKey)}
|
|
disabled={useGlobalSettings}
|
|
/>
|
|
</div>
|
|
))}
|
|
</div>
|
|
<div className="form__label--bold form__label--top form__label--bot">
|
|
{t('log_and_stats_section_label')}
|
|
</div>
|
|
{logAndStatsCheckboxes.map((setting) => (
|
|
<div className="form__group" key={setting.name}>
|
|
<Field
|
|
name={setting.name}
|
|
type="checkbox"
|
|
component={CheckboxField}
|
|
placeholder={t(setting.placeholder)}
|
|
/>
|
|
</div>
|
|
))}
|
|
</div>,
|
|
},
|
|
block_services: {
|
|
title: 'block_services',
|
|
component: <div label="services" title={props.t('block_services')}>
|
|
<div className="form__group">
|
|
<Field
|
|
name="use_global_blocked_services"
|
|
type="checkbox"
|
|
component={renderServiceField}
|
|
placeholder={t('blocked_services_global')}
|
|
modifier="service--global"
|
|
/>
|
|
<div className="row mb-4">
|
|
<div className="col-6">
|
|
<button
|
|
type="button"
|
|
className="btn btn-secondary btn-block"
|
|
disabled={useGlobalServices}
|
|
onClick={() => (
|
|
toggleAllServices(services.allServices, change, true)
|
|
)}
|
|
>
|
|
<Trans>block_all</Trans>
|
|
</button>
|
|
</div>
|
|
<div className="col-6">
|
|
<button
|
|
type="button"
|
|
className="btn btn-secondary btn-block"
|
|
disabled={useGlobalServices}
|
|
onClick={() => (
|
|
toggleAllServices(services.allServices, change, false)
|
|
)}
|
|
>
|
|
<Trans>unblock_all</Trans>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
{services.allServices.length > 0 && (
|
|
<div className="services">
|
|
{services.allServices.map((service) => (
|
|
<Field
|
|
key={service.id}
|
|
icon={service.icon_svg}
|
|
name={`blocked_services.${service.id}`}
|
|
type="checkbox"
|
|
component={renderServiceField}
|
|
placeholder={service.name}
|
|
disabled={useGlobalServices}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>,
|
|
},
|
|
schedule_services: {
|
|
title: 'schedule_services',
|
|
component: (
|
|
<>
|
|
<div className="form__desc mb-4">
|
|
<Trans>schedule_services_desc_client</Trans>
|
|
</div>
|
|
<ScheduleForm
|
|
schedule={blockedServicesSchedule}
|
|
onScheduleSubmit={handleScheduleSubmit}
|
|
clientForm
|
|
/>
|
|
</>
|
|
),
|
|
},
|
|
upstream_dns: {
|
|
title: 'upstream_dns',
|
|
component: <div label="upstream" title={props.t('upstream_dns')}>
|
|
<div className="form__desc mb-3">
|
|
<Trans components={[<a href="#dns" key="0">link</a>]}>
|
|
upstream_dns_client_desc
|
|
</Trans>
|
|
</div>
|
|
<Field
|
|
id="upstreams"
|
|
name="upstreams"
|
|
component={renderTextareaField}
|
|
type="text"
|
|
className="form-control form-control--textarea mb-5"
|
|
placeholder={t('upstream_dns')}
|
|
normalizeOnBlur={trimLinesAndRemoveEmpty}
|
|
/>
|
|
<Examples />
|
|
</div>,
|
|
},
|
|
};
|
|
|
|
const activeTab = tabs[activeTabLabel].component;
|
|
|
|
return (
|
|
<form onSubmit={handleSubmit}>
|
|
<div className="modal-body">
|
|
<div className="form__group mb-0">
|
|
<div className="form__group">
|
|
<Field
|
|
id="name"
|
|
name="name"
|
|
component={renderInputField}
|
|
type="text"
|
|
className="form-control"
|
|
placeholder={t('form_client_name')}
|
|
normalizeOnBlur={(data) => data.trim()}
|
|
/>
|
|
</div>
|
|
|
|
<div className="form__group mb-4">
|
|
<div className="form__label">
|
|
<strong className="mr-3">
|
|
<Trans>tags_title</Trans>
|
|
</strong>
|
|
</div>
|
|
<div className="form__desc mt-0 mb-2">
|
|
<Trans components={[
|
|
<a target="_blank" rel="noopener noreferrer" href="https://github.com/AdguardTeam/AdGuardHome/wiki/Hosts-Blocklists#ctag"
|
|
key="0">link</a>,
|
|
]}>
|
|
tags_desc
|
|
</Trans>
|
|
</div>
|
|
<Field
|
|
name="tags"
|
|
component={renderMultiselect}
|
|
placeholder={t('form_select_tags')}
|
|
options={tagsOptions}
|
|
/>
|
|
</div>
|
|
|
|
<div className="form__group">
|
|
<div className="form__label">
|
|
<strong className="mr-3">
|
|
<Trans>client_identifier</Trans>
|
|
</strong>
|
|
</div>
|
|
<div className="form__desc mt-0">
|
|
<Trans components={[
|
|
<a href={CLIENT_ID_LINK} target="_blank" rel="noopener noreferrer"
|
|
key="0">text</a>,
|
|
]}>
|
|
client_identifier_desc
|
|
</Trans>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="form__group">
|
|
<FieldArray
|
|
name="ids"
|
|
component={renderFields}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<Tabs
|
|
controlClass="form"
|
|
tabs={tabs}
|
|
activeTabLabel={activeTabLabel}
|
|
setActiveTabLabel={setActiveTabLabel}
|
|
>
|
|
{activeTab}
|
|
</Tabs>
|
|
</div>
|
|
|
|
<div className="modal-footer">
|
|
<div className="btn-list">
|
|
<button
|
|
type="button"
|
|
className="btn btn-secondary btn-standard"
|
|
disabled={submitting}
|
|
onClick={() => {
|
|
reset();
|
|
toggleClientModal();
|
|
}}
|
|
>
|
|
<Trans>cancel_btn</Trans>
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
className="btn btn-success btn-standard"
|
|
disabled={
|
|
submitting
|
|
|| invalid
|
|
|| processingAdding
|
|
|| processingUpdating
|
|
}
|
|
>
|
|
<Trans>save_btn</Trans>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
);
|
|
};
|
|
|
|
Form.propTypes = {
|
|
pristine: PropTypes.bool.isRequired,
|
|
handleSubmit: PropTypes.func.isRequired,
|
|
reset: PropTypes.func.isRequired,
|
|
change: PropTypes.func.isRequired,
|
|
submitting: PropTypes.bool.isRequired,
|
|
toggleClientModal: PropTypes.func.isRequired,
|
|
useGlobalSettings: PropTypes.bool,
|
|
useGlobalServices: PropTypes.bool,
|
|
blockedServicesSchedule: PropTypes.object,
|
|
t: PropTypes.func.isRequired,
|
|
processingAdding: PropTypes.bool.isRequired,
|
|
processingUpdating: PropTypes.bool.isRequired,
|
|
invalid: PropTypes.bool.isRequired,
|
|
tagsOptions: PropTypes.array.isRequired,
|
|
initialValues: PropTypes.object,
|
|
};
|
|
|
|
const selector = formValueSelector(FORM_NAME.CLIENT);
|
|
|
|
Form = connect((state) => {
|
|
const useGlobalSettings = selector(state, 'use_global_settings');
|
|
const useGlobalServices = selector(state, 'use_global_blocked_services');
|
|
const blockedServicesSchedule = selector(state, 'blocked_services_schedule');
|
|
return {
|
|
useGlobalSettings,
|
|
useGlobalServices,
|
|
blockedServicesSchedule,
|
|
};
|
|
})(Form);
|
|
|
|
export default flow([
|
|
withTranslation(),
|
|
reduxForm({
|
|
form: FORM_NAME.CLIENT,
|
|
enableReinitialize: true,
|
|
validate,
|
|
}),
|
|
])(Form);
|