3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- import React , { useState } from 'react' ;
6
+ import React , { useState , useCallback } from 'react' ;
7
+ import { RouteComponentProps } from 'react-router-dom' ;
8
+ import { NotificationsStart } from 'opensearch-dashboards/public' ;
9
+ import { RuleService } from '../../../../services' ;
10
+ import { ROUTES } from '../../../../utils/constants' ;
7
11
import { ContentPanel } from '../../../../components/ContentPanel' ;
8
12
import { EuiSpacer , EuiButtonGroup } from '@elastic/eui' ;
9
13
import { Rule } from '../../../../../models/interfaces' ;
10
14
import { RuleEditorFormState , ruleEditorStateDefaultValue } from './RuleEditorFormState' ;
11
15
import { mapFormToRule , mapRuleToForm } from './mappers' ;
12
16
import { VisualRuleEditor } from './VisualRuleEditor' ;
13
17
import { YamlRuleEditor } from './YamlRuleEditor' ;
18
+ import { validateRule } from '../../utils/helpers' ;
19
+ import { errorNotificationToast } from '../../../../utils/helpers' ;
14
20
15
21
export interface RuleEditorProps {
16
22
title : string ;
17
- FooterActions : React . FC < { rule : Rule } > ;
18
23
rule ?: Rule ;
24
+ history : RouteComponentProps [ 'history' ] ;
25
+ notifications ?: NotificationsStart ;
26
+ ruleService : RuleService ;
27
+ mode : 'create' | 'edit' ;
19
28
}
20
29
21
30
export interface VisualEditorFormErrorsState {
@@ -35,7 +44,14 @@ const editorTypes = [
35
44
} ,
36
45
] ;
37
46
38
- export const RuleEditor : React . FC < RuleEditorProps > = ( { title, rule, FooterActions } ) => {
47
+ export const RuleEditor : React . FC < RuleEditorProps > = ( {
48
+ history,
49
+ notifications,
50
+ title,
51
+ rule,
52
+ ruleService,
53
+ mode,
54
+ } ) => {
39
55
const [ ruleEditorFormState , setRuleEditorFormState ] = useState < RuleEditorFormState > (
40
56
rule
41
57
? { ...mapRuleToForm ( rule ) , id : ruleEditorStateDefaultValue . id }
@@ -48,15 +64,44 @@ export const RuleEditor: React.FC<RuleEditorProps> = ({ title, rule, FooterActio
48
64
setSelectedEditorType ( optionId ) ;
49
65
} ;
50
66
51
- const getRule = ( ) : Rule => {
52
- return mapFormToRule ( ruleEditorFormState ) ;
53
- } ;
54
-
55
67
const onYamlRuleEditorChange = ( value : Rule ) => {
56
68
const formState = mapRuleToForm ( value ) ;
57
69
setRuleEditorFormState ( formState ) ;
58
70
} ;
59
71
72
+ const onSubmit = async ( ) => {
73
+ const submitingRule = mapFormToRule ( ruleEditorFormState ) ;
74
+ if ( ! validateRule ( submitingRule , notifications ! , 'create' ) ) {
75
+ return ;
76
+ }
77
+
78
+ let result ;
79
+ if ( mode === 'edit' ) {
80
+ if ( ! rule ) {
81
+ console . error ( 'No rule id found' ) ;
82
+ return ;
83
+ }
84
+ result = await ruleService . updateRule ( rule ?. id , submitingRule . category , submitingRule ) ;
85
+ } else {
86
+ result = await ruleService . createRule ( submitingRule ) ;
87
+ }
88
+
89
+ if ( ! result . ok ) {
90
+ errorNotificationToast (
91
+ notifications ! ,
92
+ mode === 'create' ? 'create' : 'save' ,
93
+ 'rule' ,
94
+ result . error
95
+ ) ;
96
+ } else {
97
+ history . replace ( ROUTES . RULES ) ;
98
+ }
99
+ } ;
100
+
101
+ const goToRulesList = useCallback ( ( ) => {
102
+ history . replace ( ROUTES . RULES ) ;
103
+ } , [ history ] ) ;
104
+
60
105
return (
61
106
< >
62
107
< ContentPanel title = { title } >
@@ -70,20 +115,26 @@ export const RuleEditor: React.FC<RuleEditorProps> = ({ title, rule, FooterActio
70
115
< EuiSpacer size = "xl" />
71
116
{ selectedEditorType === 'visual' && (
72
117
< VisualRuleEditor
118
+ mode = { mode }
119
+ notifications = { notifications }
73
120
ruleEditorFormState = { ruleEditorFormState }
74
121
setRuleEditorFormState = { setRuleEditorFormState }
122
+ cancel = { goToRulesList }
123
+ submit = { onSubmit }
75
124
/>
76
125
) }
77
126
{ selectedEditorType === 'yaml' && (
78
127
< YamlRuleEditor
128
+ mode = { mode }
79
129
rule = { mapFormToRule ( ruleEditorFormState ) }
80
130
change = { onYamlRuleEditorChange }
131
+ cancel = { goToRulesList }
132
+ submit = { onSubmit }
81
133
/>
82
134
) }
83
135
< EuiSpacer />
84
136
</ ContentPanel >
85
137
< EuiSpacer size = "xl" />
86
- < FooterActions rule = { getRule ( ) } />
87
138
</ >
88
139
) ;
89
140
} ;
0 commit comments