Skip to content

Commit 9fdfe77

Browse files
author
oatkiller
committed
fixed up stuff, but reducer is still having a hard time
1 parent c12e150 commit 9fdfe77

5 files changed

Lines changed: 78 additions & 106 deletions

File tree

x-pack/plugins/endpoint/public/applications/endpoint/models/policy_details_config.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,31 @@ export function clone(policyDetailsConfig: UIPolicyConfig): UIPolicyConfig {
4545
}
4646

4747
/**
48-
* Returns cloned `configuration` with `value` set by the `keyPath`.
48+
* Returns value from `configuration`
4949
*/
50+
export const getIn = (a: UIPolicyConfig) => <Key extends keyof UIPolicyConfig>(key: Key) => <
51+
subKey extends keyof UIPolicyConfig[Key]
52+
>(
53+
subKey: subKey
54+
) => <LeafKey extends keyof UIPolicyConfig[Key][subKey]>(
55+
leafKey: LeafKey
56+
): UIPolicyConfig[Key][subKey][LeafKey] => {
57+
return a[key][subKey][leafKey];
58+
};
5059

51-
export function setIn<
52-
K1 extends keyof UIPolicyConfig,
53-
K2 extends keyof UIPolicyConfig[K1],
54-
K3 extends keyof UIPolicyConfig[K1][K2]
55-
>(configuration: UIPolicyConfig, keyPath: [K1, K2, K3], value: boolean | string): UIPolicyConfig;
56-
export function setIn<K1 extends keyof UIPolicyConfig, K2 extends keyof UIPolicyConfig[K1]>(
57-
configuration: UIPolicyConfig,
58-
keyPath: [K1, K2],
59-
value: UIPolicyConfig[K1][K2]
60-
): UIPolicyConfig;
61-
export function setIn<K1 extends keyof UIPolicyConfig>(
62-
configuration: UIPolicyConfig,
63-
keyPath: [K1],
64-
value: UIPolicyConfig[K1]
65-
): UIPolicyConfig;
66-
export function setIn(
67-
configuration: UIPolicyConfig,
68-
keyPath: string[],
69-
value: boolean | string
70-
): UIPolicyConfig {
71-
const payload = clone(configuration);
72-
let current: any = payload;
73-
while (keyPath.length > 1) {
74-
current = current[keyPath.shift()!];
75-
}
76-
current[keyPath[0]] = value;
77-
return payload;
78-
}
60+
/**
61+
* Returns cloned `configuration` with `value` set by the `keyPath`.
62+
*/
63+
export const setIn = (a: UIPolicyConfig) => <Key extends keyof UIPolicyConfig>(key: Key) => <
64+
subKey extends keyof UIPolicyConfig[Key]
65+
>(
66+
subKey: subKey
67+
) => <LeafKey extends keyof UIPolicyConfig[Key][subKey]>(leafKey: LeafKey) => <
68+
V extends UIPolicyConfig[Key][subKey][LeafKey]
69+
>(
70+
v: V
71+
): UIPolicyConfig => {
72+
const c = clone(a);
73+
c[key][subKey][leafKey] = v;
74+
return c;
75+
};

x-pack/plugins/endpoint/public/applications/endpoint/types.ts

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,21 @@ export interface PolicyDetailsState {
118118
* Endpoint Policy configuration
119119
*/
120120
export interface PolicyConfig {
121-
windows: {
122-
events: {
123-
process: boolean;
124-
network: boolean;
125-
};
126-
/** malware mode can be off, detect, prevent or prevent and notify user */
127-
malware: MalwareFields;
121+
windows: UIPolicyConfig['windows'] & {
128122
logging: {
129123
stdout: string;
130124
file: string;
131125
};
132126
advanced: PolicyConfigAdvancedOptions;
133127
};
134-
mac: {
135-
events: {
136-
file: boolean;
137-
process: boolean;
138-
network: boolean;
139-
};
140-
malware: MalwareFields;
128+
mac: UIPolicyConfig['mac'] & {
141129
logging: {
142130
stdout: string;
143131
file: string;
144132
};
145133
advanced: PolicyConfigAdvancedOptions;
146134
};
147-
linux: {
148-
events: {
149-
file: boolean;
150-
process: boolean;
151-
network: boolean;
152-
};
135+
linux: UIPolicyConfig['linux'] & {
153136
logging: {
154137
stdout: string;
155138
file: string;
@@ -172,29 +155,39 @@ interface PolicyConfigAdvancedOptions {
172155
};
173156
}
174157

175-
/**
176-
* Windows-specific policy configuration that is supported via the UI
177-
*/
178-
type WindowsPolicyConfig = Pick<PolicyConfig['windows'], 'events' | 'malware'>;
179-
180-
/**
181-
* Mac-specific policy configuration that is supported via the UI
182-
*/
183-
type MacPolicyConfig = Pick<PolicyConfig['mac'], 'malware' | 'events'>;
184-
185-
/**
186-
* Linux-specific policy configuration that is supported via the UI
187-
*/
188-
type LinuxPolicyConfig = Pick<PolicyConfig['linux'], 'events'>;
189-
190158
/**
191159
* The set of Policy configuration settings that are show/edited via the UI
192160
*/
193-
export interface UIPolicyConfig {
194-
windows: WindowsPolicyConfig;
195-
mac: MacPolicyConfig;
196-
linux: LinuxPolicyConfig;
197-
}
161+
/* eslint-disable @typescript-eslint/consistent-type-definitions */
162+
export type UIPolicyConfig = {
163+
windows: {
164+
events: {
165+
process: boolean;
166+
network: boolean;
167+
};
168+
/** malware mode can be off, detect, prevent or prevent and notify user */
169+
malware: MalwareFields;
170+
};
171+
mac: {
172+
events: {
173+
file: boolean;
174+
process: boolean;
175+
network: boolean;
176+
};
177+
malware: MalwareFields;
178+
};
179+
180+
/**
181+
* Linux-specific policy configuration that is supported via the UI
182+
*/
183+
linux: {
184+
events: {
185+
file: boolean;
186+
process: boolean;
187+
network: boolean;
188+
};
189+
};
190+
};
198191

199192
/** OS used in Policy */
200193
export enum OS {

x-pack/plugins/endpoint/public/applications/endpoint/view/policy/policy_forms/eventing/checkbox.tsx

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,34 @@ import React, { useCallback, useMemo } from 'react';
88
import { EuiCheckbox } from '@elastic/eui';
99
import { useDispatch } from 'react-redux';
1010
import { htmlIdGenerator } from '@elastic/eui';
11-
import { setIn } from '../../../../models/policy_details_config';
1211
import { usePolicyDetailsSelector } from '../../policy_hooks';
1312
import { policyConfig } from '../../../../store/policy_details/selectors';
1413
import { PolicyDetailsAction } from '../../../../store/policy_details';
1514
import { UIPolicyConfig } from '../../../../types';
1615

17-
export const EventingCheckbox = React.memo(function<
18-
T extends keyof UIPolicyConfig & string,
19-
TT extends keyof UIPolicyConfig[T] & string,
20-
TTT extends keyof UIPolicyConfig[T][TT] & string
21-
>({
16+
export const EventingCheckbox = React.memo(function({
2217
name,
23-
os,
24-
protectionEvent,
25-
protectionField,
18+
setter,
19+
getter,
2620
}: {
2721
name: string;
28-
os: T;
29-
protectionEvent: TT;
30-
protectionField: TTT;
22+
setter: (config: UIPolicyConfig, checked: boolean) => UIPolicyConfig;
23+
getter: (config: UIPolicyConfig) => boolean;
3124
}) {
3225
const policyDetailsConfig = usePolicyDetailsSelector(policyConfig);
33-
const selected = policyDetailsConfig[os][protectionEvent][protectionField];
26+
const selected = getter(policyDetailsConfig);
3427
const dispatch = useDispatch<(action: PolicyDetailsAction) => void>();
3528

3629
const handleCheckboxChange = useCallback(
3730
(event: React.ChangeEvent<HTMLInputElement>) => {
3831
if (policyDetailsConfig) {
39-
const payload = setIn(
40-
policyDetailsConfig,
41-
[os, protectionEvent, protectionField],
42-
event.target.checked
43-
);
44-
4532
dispatch({
4633
type: 'userChangedPolicyConfig',
47-
payload: { policyConfig: payload },
34+
payload: { policyConfig: setter(policyDetailsConfig, event.target.checked) },
4835
});
4936
}
5037
},
51-
[dispatch, os, policyDetailsConfig, protectionEvent, protectionField]
38+
[dispatch, policyDetailsConfig, setter]
5239
);
5340

5441
return (

x-pack/plugins/endpoint/public/applications/endpoint/view/policy/policy_forms/eventing/mac.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import React, { useMemo } from 'react';
88
import { i18n } from '@kbn/i18n';
99
import { FormattedMessage } from '@kbn/i18n/react';
10-
import { htmlIdGenerator } from '@elastic/eui';
1110
import { EuiTitle, EuiText, EuiSpacer } from '@elastic/eui';
1211
import { EventingCheckbox } from './checkbox';
1312
import { OS, UIPolicyConfig } from '../../../../types';
1413
import { usePolicyDetailsSelector } from '../../policy_hooks';
1514
import { selectedMacEventing, totalMacEventing } from '../../../../store/policy_details/selectors';
1615
import { ConfigForm } from '../config_form';
16+
import { getIn, setIn } from '../../../../models/policy_details_config';
1717

1818
export const MacEventing = React.memo(() => {
1919
const selected = usePolicyDetailsSelector(selectedMacEventing);
@@ -22,7 +22,6 @@ export const MacEventing = React.memo(() => {
2222
const checkboxes: Array<{
2323
name: string;
2424
os: 'mac';
25-
protectionEvent: keyof UIPolicyConfig['mac'];
2625
protectionField: keyof UIPolicyConfig['mac']['events'];
2726
}> = useMemo(
2827
() => [
@@ -31,23 +30,20 @@ export const MacEventing = React.memo(() => {
3130
defaultMessage: 'File',
3231
}),
3332
os: OS.mac,
34-
protectionEvent: 'events',
3533
protectionField: 'file',
3634
},
3735
{
3836
name: i18n.translate('xpack.endpoint.policyDetailsConfig.mac.events.process', {
3937
defaultMessage: 'Process',
4038
}),
4139
os: OS.mac,
42-
protectionEvent: 'events',
4340
protectionField: 'process',
4441
},
4542
{
4643
name: i18n.translate('xpack.endpoint.policyDetailsConfig.mac.events.network', {
4744
defaultMessage: 'Network',
4845
}),
4946
os: OS.mac,
50-
protectionEvent: 'events',
5147
protectionField: 'network',
5248
},
5349
],
@@ -69,12 +65,12 @@ export const MacEventing = React.memo(() => {
6965
{checkboxes.map((item, index) => {
7066
return (
7167
<EventingCheckbox
72-
id={useMemo(() => htmlIdGenerator()(), [])}
7368
name={item.name}
7469
key={index}
75-
os={item.os}
76-
protectionEvent={item.protectionEvent}
77-
protectionField={item.protectionField}
70+
setter={(config, checked) =>
71+
setIn(config)(item.os)('events')(item.protectionField)(checked)
72+
}
73+
getter={config => getIn(config)(item.os)('events')(item.protectionField)}
7874
/>
7975
);
8076
})}

x-pack/plugins/endpoint/public/applications/endpoint/view/policy/policy_forms/eventing/windows.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
totalWindowsEventing,
1717
} from '../../../../store/policy_details/selectors';
1818
import { ConfigForm } from '../config_form';
19+
import { setIn, getIn } from '../../../../models/policy_details_config';
1920

2021
export const WindowsEventing = React.memo(() => {
2122
const selected = usePolicyDetailsSelector(selectedWindowsEventing);
@@ -24,7 +25,6 @@ export const WindowsEventing = React.memo(() => {
2425
const checkboxes: Array<{
2526
name: string;
2627
os: 'windows';
27-
protectionEvent: keyof UIPolicyConfig['windows'];
2828
protectionField: keyof UIPolicyConfig['windows']['events'];
2929
}> = useMemo(
3030
() => [
@@ -33,15 +33,13 @@ export const WindowsEventing = React.memo(() => {
3333
defaultMessage: 'Process',
3434
}),
3535
os: OS.windows,
36-
protectionEvent: 'events',
3736
protectionField: 'process',
3837
},
3938
{
4039
name: i18n.translate('xpack.endpoint.policyDetailsConfig.windows.events.network', {
4140
defaultMessage: 'Network',
4241
}),
4342
os: OS.windows,
44-
protectionEvent: 'events',
4543
protectionField: 'network',
4644
},
4745
],
@@ -65,9 +63,10 @@ export const WindowsEventing = React.memo(() => {
6563
<EventingCheckbox
6664
name={item.name}
6765
key={index}
68-
os={item.os}
69-
protectionEvent={item.protectionEvent}
70-
protectionField={item.protectionField}
66+
setter={(config, checked) =>
67+
setIn(config)(item.os)('events')(item.protectionField)(checked)
68+
}
69+
getter={config => getIn(config)(item.os)('events')(item.protectionField)}
7170
/>
7271
);
7372
})}
@@ -99,7 +98,7 @@ export const WindowsEventing = React.memo(() => {
9998
[]
10099
)}
101100
id="windowsEventingForm"
102-
rightCorner={collectionsEnabled()}
101+
rightCorner={collectionsEnabled}
103102
children={renderCheckboxes}
104103
/>
105104
);

0 commit comments

Comments
 (0)