Spaces:
Building
Building
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
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
15 |
+
imports: [
|
16 |
+
CommonModule,
|
17 |
+
ReactiveFormsModule,
|
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() {
|
132 |
+
this.form = this.fb.group({
|
133 |
+
name: [
|
134 |
+
{ value: '', disabled: this.data.mode === 'edit' },
|
135 |
+
[Validators.required, Validators.pattern(/^[a-zA-Z0-9_]+$/)]
|
136 |
+
],
|
137 |
+
caption: [''],
|
138 |
+
last_update_date: ['']
|
139 |
+
});
|
140 |
+
|
141 |
+
if (this.data.mode === 'edit' && this.data.project) {
|
142 |
+
this.form.patchValue(this.data.project);
|
143 |
+
}
|
144 |
+
}
|
145 |
+
|
146 |
+
formatDate(dateString: string | undefined): string {
|
147 |
+
if (!dateString) return 'Unknown';
|
148 |
+
return new Date(dateString).toLocaleDateString();
|
149 |
+
}
|
150 |
+
|
151 |
+
async save() {
|
152 |
+
if (this.form.invalid) return;
|
153 |
+
|
154 |
+
this.saving = true;
|
155 |
+
try {
|
156 |
+
const formData = this.form.getRawValue();
|
157 |
+
|
158 |
+
if (this.data.mode === 'create') {
|
159 |
+
await this.apiService.createProject(formData).toPromise();
|
160 |
+
this.snackBar.open('Project created successfully', 'Close', { duration: 3000 });
|
161 |
+
} else {
|
162 |
+
await this.apiService.updateProject(this.data.project.id, formData).toPromise();
|
163 |
+
this.snackBar.open('Project updated successfully', 'Close', { duration: 3000 });
|
164 |
+
}
|
165 |
+
|
166 |
+
this.dialogRef.close(true);
|
167 |
+
} catch (error: any) {
|
168 |
+
const message = error.error?.detail ||
|
169 |
+
(this.data.mode === 'create' ? 'Failed to create project' : 'Failed to update project');
|
170 |
+
this.snackBar.open(message, 'Close', {
|
171 |
+
duration: 5000,
|
172 |
+
panelClass: 'error-snackbar'
|
173 |
+
});
|
174 |
+
} finally {
|
175 |
+
this.saving = false;
|
176 |
+
}
|
177 |
+
}
|
178 |
+
|
179 |
+
cancel() {
|
180 |
+
this.dialogRef.close(false);
|
181 |
+
}
|
182 |
+
}
|