Skip to content

Commit

Permalink
Code review Change the order of the sections #622
Browse files Browse the repository at this point in the history
Signed-off-by: Jovan Cvetkovic <[email protected]>
  • Loading branch information
jovancvetkovic3006 committed Jun 19, 2023
1 parent dac6581 commit 8c19090
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
22 changes: 17 additions & 5 deletions public/pages/Rules/components/RuleEditor/DetectionVisualEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ export class DetectionVisualEditor extends React.Component<
DetectionVisualEditorProps,
DetectionVisualEditorState
> {
/**
* Text area editor row height
* @private
*/
private textareaRowHeight = 25;

/**
* Text area editor empty space to occupy before filling in the editor
* @private
*/
private textareaEmptySpace = 40;

constructor(props: DetectionVisualEditorProps) {
super(props);
this.state = {
Expand All @@ -136,7 +148,7 @@ export class DetectionVisualEditor extends React.Component<

if (this.props.isInvalid != prevProps.isInvalid) {
this.validateCondition(this.state.detectionObj.condition);
this.validateDatum(this.state.detectionObj.selections);
this.validateData(this.state.detectionObj.selections);
}
}

Expand Down Expand Up @@ -208,7 +220,7 @@ export class DetectionVisualEditor extends React.Component<
return dump(compiledDetection);
};

private validateDatum = (selections: Selection[]) => {
private validateData = (selections: Selection[]) => {
const { errors } = this.state;
selections.map((selection, selIdx) => {
const fieldNames = new Set<string>();
Expand Down Expand Up @@ -276,7 +288,7 @@ export class DetectionVisualEditor extends React.Component<
},
},
() => {
this.validateDatum(newSelections);
this.validateData(newSelections);
}
);
};
Expand Down Expand Up @@ -361,7 +373,7 @@ export class DetectionVisualEditor extends React.Component<
private updateCondition = (value: string) => {
value = value.trim();

const detectionObj = { ...this.state.detectionObj, condition: value } as DetectionObject;
const detectionObj: DetectionObject = { ...this.state.detectionObj, condition: value };
this.setState(
{
detectionObj,
Expand Down Expand Up @@ -438,7 +450,7 @@ export class DetectionVisualEditor extends React.Component<
};

private getTextareaHeight = (rowNo: number = 0) => {
return `${rowNo * 25 + 40}px`;
return `${rowNo * this.textareaRowHeight + this.textareaEmptySpace}px`;
};

render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import {
EuiPopoverTitle,
EuiFlexItem,
Expand Down Expand Up @@ -149,6 +149,34 @@ export const SelectionExpField: React.FC<SelectionExpFieldProps> = ({
</div>
);

const onSelectionPopup = (e: Event, idx: number) => {
e.preventDefault();
openPopover(idx);
};

const onRemoveSelection = (idx: number) => {
const usedExp = _.cloneDeep(usedExpressions);
usedExp.splice(idx, 1);
usedExp.length && (usedExp[0].description = '');
setUsedExpressions([...usedExp]);
onChange(getValue(usedExp));
};

const onAddSelection = useCallback(() => {
const usedExp = _.cloneDeep(usedExpressions);
const differences = _.differenceBy(selections, usedExp, 'name');
const exp = [
...usedExp,
{
description: usedExpressions.length ? 'AND' : '',
isOpen: false,
name: differences[0]?.name,
},
];
setUsedExpressions(exp);
onChange(getValue(exp));
}, [usedExpressions, selections]);

return (
<EuiFlexGroup gutterSize="s" data-test-subj={dataTestSubj}>
{!usedExpressions.length && (
Expand Down Expand Up @@ -184,10 +212,7 @@ export const SelectionExpField: React.FC<SelectionExpFieldProps> = ({
description={exp.description}
value={exp.name}
isActive={exp.isOpen}
onClick={(e: any) => {
e.preventDefault();
openPopover(idx);
}}
onClick={(e) => onSelectionPopup(e, idx)}
/>
}
isOpen={exp.isOpen}
Expand All @@ -200,36 +225,18 @@ export const SelectionExpField: React.FC<SelectionExpFieldProps> = ({
<EuiButtonIcon
data-test-subj={`selection-exp-field-item-remove-${idx}`}
className={'selection-exp-field-item-remove'}
onClick={() => {
const usedExp = _.cloneDeep(usedExpressions);
usedExp.splice(idx, 1);
usedExp.length && (usedExp[0].description = '');
setUsedExpressions([...usedExp]);
onChange(getValue(usedExp));
}}
onClick={() => onRemoveSelection(idx)}
color={'danger'}
iconType="cross"
aria-label={'Remove condition'}
key={idx}
/>
</EuiFlexItem>
))}
{selections.length > usedExpressions.length && (
<EuiFlexItem grow={false} key={`selections_add`}>
<EuiButtonIcon
onClick={() => {
const usedExp = _.cloneDeep(usedExpressions);
const differences = _.differenceBy(selections, usedExp, 'name');
const exp = [
...usedExp,
{
description: usedExpressions.length ? 'AND' : '',
isOpen: false,
name: differences[0]?.name,
},
];
setUsedExpressions(exp);
onChange(getValue(exp));
}}
onClick={onAddSelection}
color={'primary'}
iconType="plusInCircleFilled"
aria-label={'Add one more condition'}
Expand Down
7 changes: 5 additions & 2 deletions public/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const NAME_REGEX = new RegExp(/^[a-zA-Z0-9 _-]{5,50}$/);
// numbers 0-9, hyphens, dot, and underscores.
export const DETECTION_NAME_REGEX = new RegExp(/^[a-zA-Z0-9_.-]{5,50}$/);

export const CONDITION_REGEX = new RegExp(
export const DETECTION_CONDITION_REGEX = new RegExp(
/^((not )?[a-zA-Z0-9_]+)?( (and|or|not) ?([a-zA-Z0-9_]+))*(?<!and|or|not)$/
);

Expand All @@ -39,7 +39,10 @@ export function validateDetectionFieldName(
return name.trim().match(regex) !== null;
}

export function validateCondition(name: string, regex: RegExp = CONDITION_REGEX): boolean {
export function validateCondition(
name: string,
regex: RegExp = DETECTION_CONDITION_REGEX
): boolean {
return name.trim().match(regex) !== null;
}

Expand Down

0 comments on commit 8c19090

Please sign in to comment.