forked from opensearch-project/security-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] More validations on YAML rule editor (opensearch-project#279)
* yaml editor as custom form component Signed-off-by: Aleksandar Djindjic <[email protected]> * refactor file structure for rule editor Signed-off-by: Aleksandar Djindjic <[email protected]> * rename RuleEditorFormState to RuleEditorFormModel Signed-off-by: Aleksandar Djindjic <[email protected]> * refactor yaml rule editor custom form component Signed-off-by: Aleksandar Djindjic <[email protected]> * pr comments fixes and snapshot tests Signed-off-by: Aleksandar Djindjic <[email protected]> * more snapshot tests Signed-off-by: Aleksandar Djindjic <[email protected]> * align fields labels styling with create detector Signed-off-by: Aleksandar Djindjic <[email protected]> * aligne formik version with alerting plugin Signed-off-by: Aleksandar Djindjic <[email protected]> * addresing validation UX issues Signed-off-by: Aleksandar Djindjic <[email protected]> * validate tag format Signed-off-by: Aleksandar Djindjic <[email protected]> Signed-off-by: Aleksandar Djindjic <[email protected]>
- Loading branch information
Showing
24 changed files
with
878 additions
and
995 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,6 @@ | |
"yarn": "^1.21.1" | ||
}, | ||
"dependencies": { | ||
"formik": "^2.2.9" | ||
"formik": "^2.2.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 0 additions & 140 deletions
140
public/pages/Rules/components/RuleEditor/RuleEditor.tsx
This file was deleted.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
public/pages/Rules/components/RuleEditor/RuleEditorContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useCallback } from 'react'; | ||
import { RouteComponentProps } from 'react-router-dom'; | ||
import { NotificationsStart } from 'opensearch-dashboards/public'; | ||
import { RuleService } from '../../../../services'; | ||
import { ROUTES } from '../../../../utils/constants'; | ||
import { EuiSpacer } from '@elastic/eui'; | ||
import { Rule } from '../../../../../models/interfaces'; | ||
import { RuleEditorFormModel, ruleEditorStateDefaultValue } from './RuleEditorFormModel'; | ||
import { mapFormToRule, mapRuleToForm } from './mappers'; | ||
import { RuleEditorForm } from './RuleEditorForm'; | ||
import { validateRule } from '../../utils/helpers'; | ||
import { errorNotificationToast } from '../../../../utils/helpers'; | ||
|
||
export interface RuleEditorProps { | ||
title: string; | ||
rule?: Rule; | ||
history: RouteComponentProps['history']; | ||
notifications?: NotificationsStart; | ||
ruleService: RuleService; | ||
mode: 'create' | 'edit'; | ||
} | ||
|
||
export interface VisualEditorFormErrorsState { | ||
nameError: string | null; | ||
descriptionError: string | null; | ||
authorError: string | null; | ||
} | ||
|
||
export const RuleEditorContainer: React.FC<RuleEditorProps> = ({ | ||
history, | ||
notifications, | ||
title, | ||
rule, | ||
ruleService, | ||
mode, | ||
}) => { | ||
const initialRuleValue = rule | ||
? { ...mapRuleToForm(rule), id: ruleEditorStateDefaultValue.id } | ||
: ruleEditorStateDefaultValue; | ||
|
||
const onSubmit = async (values: RuleEditorFormModel) => { | ||
const submitingRule = mapFormToRule(values); | ||
if (!validateRule(submitingRule, notifications!, 'create')) { | ||
return; | ||
} | ||
|
||
let result; | ||
if (mode === 'edit') { | ||
if (!rule) { | ||
console.error('No rule id found'); | ||
return; | ||
} | ||
result = await ruleService.updateRule(rule?.id, submitingRule.category, submitingRule); | ||
} else { | ||
result = await ruleService.createRule(submitingRule); | ||
} | ||
|
||
if (!result.ok) { | ||
errorNotificationToast( | ||
notifications!, | ||
mode === 'create' ? 'create' : 'save', | ||
'rule', | ||
result.error | ||
); | ||
} else { | ||
history.replace(ROUTES.RULES); | ||
} | ||
}; | ||
|
||
const goToRulesList = useCallback(() => { | ||
history.replace(ROUTES.RULES); | ||
}, [history]); | ||
|
||
return ( | ||
<> | ||
<RuleEditorForm | ||
title={title} | ||
mode={mode} | ||
notifications={notifications} | ||
initialValue={initialRuleValue} | ||
cancel={goToRulesList} | ||
submit={onSubmit} | ||
/> | ||
<EuiSpacer size="xl" /> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.