ciyidogan commited on
Commit
5e97b8d
·
verified ·
1 Parent(s): 558eb0e

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

Browse files
flare-ui/src/app/dialogs/project-edit-dialog/project-edit-dialog.component.ts CHANGED
@@ -1,14 +1,23 @@
1
  import { Component, Inject, OnInit } from '@angular/core';
2
  import { CommonModule } from '@angular/common';
3
- import { FormBuilder, FormGroup, 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 { MatButtonModule } from '@angular/material/button';
8
  import { MatIconModule } from '@angular/material/icon';
 
 
9
  import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
10
  import { ApiService } from '../../services/api.service';
11
 
 
 
 
 
 
12
  @Component({
13
  selector: 'app-project-edit-dialog',
14
  standalone: true,
@@ -18,114 +27,43 @@ import { ApiService } from '../../services/api.service';
18
  MatDialogModule,
19
  MatFormFieldModule,
20
  MatInputModule,
 
 
21
  MatButtonModule,
22
  MatIconModule,
 
 
23
  MatSnackBarModule
24
  ],
25
- template: `
26
- <h2 mat-dialog-title>{{ data.mode === 'create' ? 'Create New Project' : 'Edit Project' }}</h2>
27
-
28
- <mat-dialog-content>
29
- <form [formGroup]="form">
30
- <mat-form-field appearance="outline" class="full-width">
31
- <mat-label>Name*</mat-label>
32
- <input matInput formControlName="name"
33
- [readonly]="data.mode === 'edit'"
34
- placeholder="e.g., airline_agent">
35
- <mat-hint>Use only letters, numbers, and underscores</mat-hint>
36
- <mat-error *ngIf="form.get('name')?.hasError('required')">Name is required</mat-error>
37
- <mat-error *ngIf="form.get('name')?.hasError('pattern')">Invalid characters in name</mat-error>
38
- </mat-form-field>
39
-
40
- <mat-form-field appearance="outline" class="full-width">
41
- <mat-label>Caption</mat-label>
42
- <input matInput formControlName="caption"
43
- placeholder="e.g., Airline Customer Service Agent">
44
- </mat-form-field>
45
-
46
- <div class="metadata" *ngIf="data.mode === 'edit' && data.project">
47
- <div class="metadata-item">
48
- <mat-icon>calendar_today</mat-icon>
49
- <span>Created: {{ formatDate(data.project.created_date) }} by <strong>{{ data.project.created_by || 'unknown' }}</strong></span>
50
- </div>
51
- <div class="metadata-item">
52
- <mat-icon>update</mat-icon>
53
- <span>Modified: {{ formatDate(data.project.last_update_date) }} by <strong>{{ data.project.last_update_user || 'unknown' }}</strong></span>
54
- </div>
55
- </div>
56
- </form>
57
- </mat-dialog-content>
58
-
59
- <mat-dialog-actions align="end">
60
- <button mat-button (click)="cancel()">Cancel</button>
61
- <button mat-raised-button color="primary"
62
- (click)="save()"
63
- [disabled]="form.invalid || saving">
64
- {{ saving ? 'Saving...' : 'Save' }}
65
- </button>
66
- </mat-dialog-actions>
67
- `,
68
- styles: [`
69
- mat-dialog-content {
70
- padding: 20px 24px;
71
- min-width: 400px;
72
- }
73
-
74
- .full-width {
75
- width: 100%;
76
- margin-bottom: 16px;
77
- }
78
-
79
- .metadata {
80
- margin-top: 24px;
81
- padding: 16px;
82
- background-color: #f5f5f5;
83
- border-radius: 4px;
84
-
85
- .metadata-item {
86
- display: flex;
87
- align-items: center;
88
- gap: 8px;
89
- margin-bottom: 8px;
90
-
91
- &:last-child {
92
- margin-bottom: 0;
93
- }
94
-
95
- mat-icon {
96
- color: #666;
97
- font-size: 20px;
98
- width: 20px;
99
- height: 20px;
100
- }
101
-
102
- span {
103
- color: #666;
104
- font-size: 14px;
105
-
106
- strong {
107
- color: #333;
108
- }
109
- }
110
- }
111
- }
112
-
113
- mat-dialog-actions {
114
- padding: 16px 24px;
115
- margin: 0;
116
- }
117
- `]
118
  })
119
  export default class ProjectEditDialogComponent implements OnInit {
120
  form!: FormGroup;
121
  saving = false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  constructor(
124
  private fb: FormBuilder,
125
  private apiService: ApiService,
126
  private snackBar: MatSnackBar,
127
  public dialogRef: MatDialogRef<ProjectEditDialogComponent>,
128
- @Inject(MAT_DIALOG_DATA) public data: any
129
  ) {}
130
 
131
  ngOnInit() {
@@ -147,18 +85,36 @@ export default class ProjectEditDialogComponent implements OnInit {
147
  // Rebuild test users form array
148
  const testUsersArray = this.form.get('testUsers') as FormArray;
149
  testUsersArray.clear();
150
- (this.data.project.test_users || []).forEach(phoneNumber => {
151
  testUsersArray.push(this.fb.control(phoneNumber, Validators.required));
152
  });
153
- } else if (this.data.mode === 'create') {
154
- // Yeni proje için boş bir versiyon otomatik ekle
155
- this.data.autoCreateVersion = true;
156
  }
157
  }
158
 
159
- formatDate(dateString: string | undefined): string {
160
- if (!dateString) return 'Unknown';
161
- return new Date(dateString).toLocaleDateString();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  }
163
 
164
  async save() {
@@ -166,7 +122,7 @@ export default class ProjectEditDialogComponent implements OnInit {
166
  this.form.markAllAsTouched();
167
  return;
168
  }
169
-
170
  this.saving = true;
171
  try {
172
  const formValue = this.form.value;
@@ -176,14 +132,15 @@ export default class ProjectEditDialogComponent implements OnInit {
176
  default_language: formValue.defaultLanguage,
177
  supported_languages: formValue.supportedLanguages
178
  };
179
-
180
  let result;
181
  if (this.data.mode === 'create') {
182
  result = await this.apiService.createProject(projectData).toPromise();
183
 
184
  // Otomatik boş versiyon oluştur
185
- if (this.data.autoCreateVersion && result) {
186
  const defaultVersion = {
 
187
  description: 'Initial version',
188
  default_api: '',
189
  published: false,
@@ -203,17 +160,21 @@ export default class ProjectEditDialogComponent implements OnInit {
203
  parameters: []
204
  };
205
 
206
- await this.apiService.createVersion(result.id, defaultVersion).toPromise();
 
 
 
 
207
  }
208
 
209
  this.showMessage('Project created successfully!', false);
210
  } else {
211
  projectData.id = this.data.project.id;
212
  projectData.last_update_date = this.data.project.last_update_date;
213
- result = await this.apiService.updateProject(projectData).toPromise();
214
  this.showMessage('Project updated successfully!', false);
215
  }
216
-
217
  this.dialogRef.close(result);
218
  } catch (error: any) {
219
  if (error.status === 409) {
@@ -226,7 +187,14 @@ export default class ProjectEditDialogComponent implements OnInit {
226
  }
227
  }
228
 
229
- cancel() {
230
- this.dialogRef.close(false);
 
 
 
 
 
 
 
231
  }
232
  }
 
1
  import { Component, Inject, OnInit } from '@angular/core';
2
  import { CommonModule } from '@angular/common';
3
+ import { FormBuilder, FormGroup, Validators, ReactiveFormsModule, FormArray } 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 { MatDividerModule } from '@angular/material/divider';
13
  import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
14
  import { ApiService } from '../../services/api.service';
15
 
16
+ export interface ProjectDialogData {
17
+ mode: 'create' | 'edit';
18
+ project?: any;
19
+ }
20
+
21
  @Component({
22
  selector: 'app-project-edit-dialog',
23
  standalone: true,
 
27
  MatDialogModule,
28
  MatFormFieldModule,
29
  MatInputModule,
30
+ MatSelectModule,
31
+ MatCheckboxModule,
32
  MatButtonModule,
33
  MatIconModule,
34
+ MatChipsModule,
35
+ MatDividerModule,
36
  MatSnackBarModule
37
  ],
38
+ templateUrl: './project-edit-dialog.component.html',
39
+ styleUrls: ['./project-edit-dialog.component.scss']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  })
41
  export default class ProjectEditDialogComponent implements OnInit {
42
  form!: FormGroup;
43
  saving = false;
44
+ projectIcons = ['folder', 'work', 'shopping_cart', 'school', 'local_hospital', 'restaurant', 'home', 'business'];
45
+ languages = [
46
+ { code: 'tr', name: 'Turkish' },
47
+ { code: 'en', name: 'English' },
48
+ { code: 'de', name: 'German' },
49
+ { code: 'fr', name: 'French' },
50
+ { code: 'es', name: 'Spanish' }
51
+ ];
52
+ timezones = [
53
+ 'Europe/Istanbul',
54
+ 'Europe/London',
55
+ 'Europe/Berlin',
56
+ 'America/New_York',
57
+ 'America/Los_Angeles',
58
+ 'Asia/Tokyo'
59
+ ];
60
 
61
  constructor(
62
  private fb: FormBuilder,
63
  private apiService: ApiService,
64
  private snackBar: MatSnackBar,
65
  public dialogRef: MatDialogRef<ProjectEditDialogComponent>,
66
+ @Inject(MAT_DIALOG_DATA) public data: ProjectDialogData
67
  ) {}
68
 
69
  ngOnInit() {
 
85
  // Rebuild test users form array
86
  const testUsersArray = this.form.get('testUsers') as FormArray;
87
  testUsersArray.clear();
88
+ (this.data.project.test_users || []).forEach((phoneNumber: string) => {
89
  testUsersArray.push(this.fb.control(phoneNumber, Validators.required));
90
  });
 
 
 
91
  }
92
  }
93
 
94
+ initializeForm() {
95
+ this.form = this.fb.group({
96
+ name: ['', [Validators.required, Validators.pattern(/^[a-z0-9_]+$/)]],
97
+ caption: ['', Validators.required],
98
+ icon: ['folder'],
99
+ description: [''],
100
+ defaultLanguage: ['tr'],
101
+ supportedLanguages: [['tr']],
102
+ timezone: ['Europe/Istanbul'],
103
+ region: ['tr-TR'],
104
+ testUsers: this.fb.array([])
105
+ });
106
+ }
107
+
108
+ get testUsers() {
109
+ return this.form.get('testUsers') as FormArray;
110
+ }
111
+
112
+ addTestUser() {
113
+ this.testUsers.push(this.fb.control('', Validators.required));
114
+ }
115
+
116
+ removeTestUser(index: number) {
117
+ this.testUsers.removeAt(index);
118
  }
119
 
120
  async save() {
 
122
  this.form.markAllAsTouched();
123
  return;
124
  }
125
+
126
  this.saving = true;
127
  try {
128
  const formValue = this.form.value;
 
132
  default_language: formValue.defaultLanguage,
133
  supported_languages: formValue.supportedLanguages
134
  };
135
+
136
  let result;
137
  if (this.data.mode === 'create') {
138
  result = await this.apiService.createProject(projectData).toPromise();
139
 
140
  // Otomatik boş versiyon oluştur
141
+ if (result) {
142
  const defaultVersion = {
143
+ caption: 'Version 1',
144
  description: 'Initial version',
145
  default_api: '',
146
  published: false,
 
160
  parameters: []
161
  };
162
 
163
+ try {
164
+ await this.apiService.createVersion(result.id, defaultVersion).toPromise();
165
+ } catch (versionError) {
166
+ console.error('Failed to create initial version:', versionError);
167
+ }
168
  }
169
 
170
  this.showMessage('Project created successfully!', false);
171
  } else {
172
  projectData.id = this.data.project.id;
173
  projectData.last_update_date = this.data.project.last_update_date;
174
+ result = await this.apiService.updateProject(this.data.project.id, projectData).toPromise();
175
  this.showMessage('Project updated successfully!', false);
176
  }
177
+
178
  this.dialogRef.close(result);
179
  } catch (error: any) {
180
  if (error.status === 409) {
 
187
  }
188
  }
189
 
190
+ private showMessage(message: string, isError: boolean) {
191
+ this.snackBar.open(message, 'Close', {
192
+ duration: isError ? 5000 : 3000,
193
+ panelClass: isError ? 'error-snackbar' : 'success-snackbar'
194
+ });
195
+ }
196
+
197
+ close() {
198
+ this.dialogRef.close();
199
  }
200
  }