ciyidogan commited on
Commit
64fb1e3
·
verified ·
1 Parent(s): 23e2852

Update flare-ui/src/app/dialogs/intent-edit-dialog/intent-edit-dialog.component.ts

Browse files
flare-ui/src/app/dialogs/intent-edit-dialog/intent-edit-dialog.component.ts CHANGED
@@ -1,198 +1,199 @@
1
- import { Component, Inject, OnInit } from '@angular/core';
2
- import { CommonModule } from '@angular/common';
3
- import { FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule } from '@angular/forms';
4
- import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
5
- import { MatFormFieldModule } from '@angular/material/form-field';
6
- import { MatInputModule } from '@angular/material/input';
7
- import { MatSelectModule } from '@angular/material/select';
8
- import { MatCheckboxModule } from '@angular/material/checkbox';
9
- import { MatButtonModule } from '@angular/material/button';
10
- import { MatIconModule } from '@angular/material/icon';
11
- import { MatChipsModule } from '@angular/material/chips';
12
- import { MatTableModule } from '@angular/material/table';
13
- import { MatTabsModule } from '@angular/material/tabs';
14
- import { MatExpansionModule } from '@angular/material/expansion';
15
- import { MatListModule } from '@angular/material/list';
16
- import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
17
-
18
- @Component({
19
- selector: 'app-intent-edit-dialog',
20
- standalone: true,
21
- imports: [
22
- CommonModule,
23
- ReactiveFormsModule,
24
- MatDialogModule,
25
- MatFormFieldModule,
26
- MatInputModule,
27
- MatSelectModule,
28
- MatCheckboxModule,
29
- MatButtonModule,
30
- MatIconModule,
31
- MatChipsModule,
32
- MatTableModule,
33
- MatTabsModule,
34
- MatExpansionModule,
35
- MatListModule,
36
- MatSnackBarModule
37
- ],
38
- templateUrl: './intent-edit-dialog.component.html',
39
- styleUrls: ['./intent-edit-dialog.component.scss']
40
- })
41
- export default class IntentEditDialogComponent implements OnInit {
42
- form!: FormGroup;
43
- availableAPIs: any[] = [];
44
- parameterTypes = ['str', 'int', 'float', 'bool', 'date'];
45
-
46
- newExample = '';
47
-
48
- constructor(
49
- private fb: FormBuilder,
50
- private snackBar: MatSnackBar,
51
- public dialogRef: MatDialogRef<IntentEditDialogComponent>,
52
- @Inject(MAT_DIALOG_DATA) public data: any
53
- ) {
54
- this.availableAPIs = data.apis || [];
55
- }
56
-
57
- ngOnInit() {
58
- this.initializeForm();
59
- if (this.data.intent) {
60
- this.populateForm(this.data.intent);
61
- }
62
- }
63
-
64
- initializeForm() {
65
- this.form = this.fb.group({
66
- name: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9-]+$/)]],
67
- caption: [''],
68
- locale: ['tr-TR'],
69
- detection_prompt: ['', Validators.required],
70
- examples: this.fb.array([]),
71
- parameters: this.fb.array([]),
72
- action: ['', Validators.required],
73
- fallback_timeout_prompt: [''],
74
- fallback_error_prompt: ['']
75
- });
76
- }
77
-
78
- populateForm(intent: any) {
79
- this.form.patchValue({
80
- name: intent.name,
81
- caption: intent.caption,
82
- locale: intent.locale,
83
- detection_prompt: intent.detection_prompt,
84
- action: intent.action,
85
- fallback_timeout_prompt: intent.fallback_timeout_prompt,
86
- fallback_error_prompt: intent.fallback_error_prompt
87
- });
88
-
89
- // Populate examples
90
- if (intent.examples) {
91
- const examplesArray = this.form.get('examples') as FormArray;
92
- intent.examples.forEach((example: string) => {
93
- examplesArray.push(this.fb.control(example));
94
- });
95
- }
96
-
97
- // Populate parameters
98
- if (intent.parameters) {
99
- const parametersArray = this.form.get('parameters') as FormArray;
100
- intent.parameters.forEach((param: any) => {
101
- parametersArray.push(this.createParameterFormGroup(param));
102
- });
103
- }
104
- }
105
-
106
- createParameterFormGroup(param: any = {}): FormGroup {
107
- return this.fb.group({
108
- name: [param.name || '', [Validators.required, Validators.pattern(/^[a-zA-Z0-9_]+$/)]],
109
- caption: [param.caption || ''],
110
- type: [param.type || 'str', Validators.required],
111
- required: [param.required !== false],
112
- variable_name: [param.variable_name || '', Validators.required],
113
- extraction_prompt: [param.extraction_prompt || ''],
114
- validation_regex: [param.validation_regex || ''],
115
- invalid_prompt: [param.invalid_prompt || ''],
116
- type_error_prompt: [param.type_error_prompt || '']
117
- });
118
- }
119
-
120
- get examples() {
121
- return this.form.get('examples') as FormArray;
122
- }
123
-
124
- get parameters() {
125
- return this.form.get('parameters') as FormArray;
126
- }
127
-
128
- addExample() {
129
- if (this.newExample.trim()) {
130
- this.examples.push(this.fb.control(this.newExample.trim()));
131
- this.newExample = '';
132
- }
133
- }
134
-
135
- removeExample(index: number) {
136
- this.examples.removeAt(index);
137
- }
138
-
139
- addParameter() {
140
- this.parameters.push(this.createParameterFormGroup());
141
- }
142
-
143
- removeParameter(index: number) {
144
- this.parameters.removeAt(index);
145
- }
146
-
147
- moveParameter(index: number, direction: 'up' | 'down') {
148
- const newIndex = direction === 'up' ? index - 1 : index + 1;
149
- if (newIndex >= 0 && newIndex < this.parameters.length) {
150
- const param = this.parameters.at(index);
151
- this.parameters.removeAt(index);
152
- this.parameters.insert(newIndex, param);
153
- }
154
- }
155
-
156
- async testRegex(index: number) {
157
- const param = this.parameters.at(index);
158
- const regex = param.get('validation_regex')?.value;
159
-
160
- if (!regex) {
161
- this.snackBar.open('No regex pattern to test', 'Close', { duration: 2000 });
162
- return;
163
- }
164
-
165
- const testValue = prompt('Enter a test value:');
166
- if (testValue !== null) {
167
- try {
168
- const pattern = new RegExp(regex);
169
- const matches = pattern.test(testValue);
170
-
171
- this.snackBar.open(
172
- matches ? '✓ Pattern matches!' : '✗ Pattern does not match',
173
- 'Close',
174
- { duration: 3000 }
175
- );
176
- } catch (error) {
177
- this.snackBar.open('Invalid regex pattern', 'Close', {
178
- duration: 3000,
179
- panelClass: 'error-snackbar'
180
- });
181
- }
182
- }
183
- }
184
-
185
- save() {
186
- if (this.form.invalid) {
187
- this.snackBar.open('Please fix all validation errors', 'Close', { duration: 3000 });
188
- return;
189
- }
190
-
191
- const formValue = this.form.value;
192
- this.dialogRef.close(formValue);
193
- }
194
-
195
- cancel() {
196
- this.dialogRef.close(null);
197
- }
 
198
  }
 
1
+ import { Component, Inject, OnInit } from '@angular/core';
2
+ import { CommonModule } from '@angular/common';
3
+ import { FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule, FormsModule } from '@angular/forms'; // FormsModule EKLE
4
+ import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
5
+ import { MatFormFieldModule } from '@angular/material/form-field';
6
+ import { MatInputModule } from '@angular/material/input';
7
+ import { MatSelectModule } from '@angular/material/select';
8
+ import { MatCheckboxModule } from '@angular/material/checkbox';
9
+ import { MatButtonModule } from '@angular/material/button';
10
+ import { MatIconModule } from '@angular/material/icon';
11
+ import { MatChipsModule } from '@angular/material/chips';
12
+ import { MatTableModule } from '@angular/material/table';
13
+ import { MatTabsModule } from '@angular/material/tabs';
14
+ import { MatExpansionModule } from '@angular/material/expansion';
15
+ import { MatListModule } from '@angular/material/list';
16
+ import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
17
+
18
+ @Component({
19
+ selector: 'app-intent-edit-dialog',
20
+ standalone: true,
21
+ imports: [
22
+ CommonModule,
23
+ ReactiveFormsModule,
24
+ FormsModule, // BU SATIRI EKLE
25
+ MatDialogModule,
26
+ MatFormFieldModule,
27
+ MatInputModule,
28
+ MatSelectModule,
29
+ MatCheckboxModule,
30
+ MatButtonModule,
31
+ MatIconModule,
32
+ MatChipsModule,
33
+ MatTableModule,
34
+ MatTabsModule,
35
+ MatExpansionModule,
36
+ MatListModule,
37
+ MatSnackBarModule
38
+ ],
39
+ templateUrl: './intent-edit-dialog.component.html',
40
+ styleUrls: ['./intent-edit-dialog.component.scss']
41
+ })
42
+ export default class IntentEditDialogComponent implements OnInit {
43
+ form!: FormGroup;
44
+ availableAPIs: any[] = [];
45
+ parameterTypes = ['str', 'int', 'float', 'bool', 'date'];
46
+
47
+ newExample = '';
48
+
49
+ constructor(
50
+ private fb: FormBuilder,
51
+ private snackBar: MatSnackBar,
52
+ public dialogRef: MatDialogRef<IntentEditDialogComponent>,
53
+ @Inject(MAT_DIALOG_DATA) public data: any
54
+ ) {
55
+ this.availableAPIs = data.apis || [];
56
+ }
57
+
58
+ ngOnInit() {
59
+ this.initializeForm();
60
+ if (this.data.intent) {
61
+ this.populateForm(this.data.intent);
62
+ }
63
+ }
64
+
65
+ initializeForm() {
66
+ this.form = this.fb.group({
67
+ name: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9-]+$/)]],
68
+ caption: [''],
69
+ locale: ['tr-TR'],
70
+ detection_prompt: ['', Validators.required],
71
+ examples: this.fb.array([]),
72
+ parameters: this.fb.array([]),
73
+ action: ['', Validators.required],
74
+ fallback_timeout_prompt: [''],
75
+ fallback_error_prompt: ['']
76
+ });
77
+ }
78
+
79
+ populateForm(intent: any) {
80
+ this.form.patchValue({
81
+ name: intent.name,
82
+ caption: intent.caption,
83
+ locale: intent.locale,
84
+ detection_prompt: intent.detection_prompt,
85
+ action: intent.action,
86
+ fallback_timeout_prompt: intent.fallback_timeout_prompt,
87
+ fallback_error_prompt: intent.fallback_error_prompt
88
+ });
89
+
90
+ // Populate examples
91
+ if (intent.examples) {
92
+ const examplesArray = this.form.get('examples') as FormArray;
93
+ intent.examples.forEach((example: string) => {
94
+ examplesArray.push(this.fb.control(example));
95
+ });
96
+ }
97
+
98
+ // Populate parameters
99
+ if (intent.parameters) {
100
+ const parametersArray = this.form.get('parameters') as FormArray;
101
+ intent.parameters.forEach((param: any) => {
102
+ parametersArray.push(this.createParameterFormGroup(param));
103
+ });
104
+ }
105
+ }
106
+
107
+ createParameterFormGroup(param: any = {}): FormGroup {
108
+ return this.fb.group({
109
+ name: [param.name || '', [Validators.required, Validators.pattern(/^[a-zA-Z0-9_]+$/)]],
110
+ caption: [param.caption || ''],
111
+ type: [param.type || 'str', Validators.required],
112
+ required: [param.required !== false],
113
+ variable_name: [param.variable_name || '', Validators.required],
114
+ extraction_prompt: [param.extraction_prompt || ''],
115
+ validation_regex: [param.validation_regex || ''],
116
+ invalid_prompt: [param.invalid_prompt || ''],
117
+ type_error_prompt: [param.type_error_prompt || '']
118
+ });
119
+ }
120
+
121
+ get examples() {
122
+ return this.form.get('examples') as FormArray;
123
+ }
124
+
125
+ get parameters() {
126
+ return this.form.get('parameters') as FormArray;
127
+ }
128
+
129
+ addExample() {
130
+ if (this.newExample.trim()) {
131
+ this.examples.push(this.fb.control(this.newExample.trim()));
132
+ this.newExample = '';
133
+ }
134
+ }
135
+
136
+ removeExample(index: number) {
137
+ this.examples.removeAt(index);
138
+ }
139
+
140
+ addParameter() {
141
+ this.parameters.push(this.createParameterFormGroup());
142
+ }
143
+
144
+ removeParameter(index: number) {
145
+ this.parameters.removeAt(index);
146
+ }
147
+
148
+ moveParameter(index: number, direction: 'up' | 'down') {
149
+ const newIndex = direction === 'up' ? index - 1 : index + 1;
150
+ if (newIndex >= 0 && newIndex < this.parameters.length) {
151
+ const param = this.parameters.at(index);
152
+ this.parameters.removeAt(index);
153
+ this.parameters.insert(newIndex, param);
154
+ }
155
+ }
156
+
157
+ async testRegex(index: number) {
158
+ const param = this.parameters.at(index);
159
+ const regex = param.get('validation_regex')?.value;
160
+
161
+ if (!regex) {
162
+ this.snackBar.open('No regex pattern to test', 'Close', { duration: 2000 });
163
+ return;
164
+ }
165
+
166
+ const testValue = prompt('Enter a test value:');
167
+ if (testValue !== null) {
168
+ try {
169
+ const pattern = new RegExp(regex);
170
+ const matches = pattern.test(testValue);
171
+
172
+ this.snackBar.open(
173
+ matches ? '✓ Pattern matches!' : '✗ Pattern does not match',
174
+ 'Close',
175
+ { duration: 3000 }
176
+ );
177
+ } catch (error) {
178
+ this.snackBar.open('Invalid regex pattern', 'Close', {
179
+ duration: 3000,
180
+ panelClass: 'error-snackbar'
181
+ });
182
+ }
183
+ }
184
+ }
185
+
186
+ save() {
187
+ if (this.form.invalid) {
188
+ this.snackBar.open('Please fix all validation errors', 'Close', { duration: 3000 });
189
+ return;
190
+ }
191
+
192
+ const formValue = this.form.value;
193
+ this.dialogRef.close(formValue);
194
+ }
195
+
196
+ cancel() {
197
+ this.dialogRef.close(null);
198
+ }
199
  }