ciyidogan commited on
Commit
8f934bf
·
verified ·
1 Parent(s): 4112c2d

Update flare-ui/src/app/components/environment/environment.component.ts

Browse files
flare-ui/src/app/components/environment/environment.component.ts CHANGED
@@ -1,210 +1,269 @@
1
- import { Component, inject, OnInit } from '@angular/core';
2
- import { CommonModule } from '@angular/common';
3
- import { FormsModule } from '@angular/forms';
4
- import { ApiService, Environment } from '../../services/api.service';
5
-
6
- @Component({
7
- selector: 'app-environment',
8
- standalone: true,
9
- imports: [CommonModule, FormsModule],
10
- template: `
11
- <div class="environment-container">
12
- <h2>Environment Configuration</h2>
13
-
14
- <div class="card">
15
- <div class="card-body">
16
- <form (ngSubmit)="save()" #envForm="ngForm">
17
- <div class="form-group">
18
- <label for="workMode">Work Mode *</label>
19
- <select
20
- id="workMode"
21
- name="workMode"
22
- [(ngModel)]="environment.work_mode"
23
- (change)="onWorkModeChange()"
24
- required
25
- [disabled]="loading"
26
- >
27
- <option value="hfcloud">HF Cloud</option>
28
- <option value="cloud">Cloud</option>
29
- <option value="on-premise">On-Premise</option>
30
- </select>
31
- </div>
32
-
33
- <div class="form-group">
34
- <label for="cloudToken">Cloud Token</label>
35
- <input
36
- type="password"
37
- id="cloudToken"
38
- name="cloudToken"
39
- [(ngModel)]="environment.cloud_token"
40
- [disabled]="loading || environment.work_mode === 'on-premise'"
41
- placeholder="Enter cloud token"
42
- >
43
- <small class="text-muted">Required for HF Cloud and Cloud modes</small>
44
- </div>
45
-
46
- <div class="form-group">
47
- <label for="sparkEndpoint">Spark Endpoint *</label>
48
- <div class="input-with-button">
49
- <input
50
- type="url"
51
- id="sparkEndpoint"
52
- name="sparkEndpoint"
53
- [(ngModel)]="environment.spark_endpoint"
54
- required
55
- [disabled]="loading"
56
- placeholder="https://spark-service.example.com"
57
- >
58
- <button
59
- type="button"
60
- class="btn btn-secondary"
61
- (click)="testConnection()"
62
- [disabled]="loading || !environment.spark_endpoint"
63
- >
64
- Test Connection
65
- </button>
66
- </div>
67
- </div>
68
-
69
- @if (message) {
70
- <div class="alert" [class.alert-success]="!isError" [class.alert-danger]="isError">
71
- {{ message }}
72
- </div>
73
- }
74
-
75
- <div class="form-actions">
76
- <button
77
- type="submit"
78
- class="btn btn-primary"
79
- [disabled]="loading || !envForm.valid"
80
- >
81
- @if (saving) {
82
- <span class="spinner"></span> Saving...
83
- } @else {
84
- Save
85
- }
86
- </button>
87
- <button
88
- type="button"
89
- class="btn btn-secondary"
90
- (click)="reloadFromSpark()"
91
- [disabled]="loading"
92
- >
93
- Reload from Spark
94
- </button>
95
- </div>
96
- </form>
97
- </div>
98
- </div>
99
- </div>
100
- `,
101
- styles: [`
102
- .environment-container {
103
- h2 {
104
- margin-bottom: 1.5rem;
105
- }
106
- }
107
-
108
- .input-with-button {
109
- display: flex;
110
- gap: 0.5rem;
111
-
112
- input {
113
- flex: 1;
114
- }
115
- }
116
-
117
- .form-actions {
118
- display: flex;
119
- gap: 0.5rem;
120
- margin-top: 1.5rem;
121
- }
122
-
123
- .text-muted {
124
- color: #6c757d;
125
- font-size: 0.875rem;
126
- }
127
- `]
128
- })
129
- export class EnvironmentComponent implements OnInit {
130
- private apiService = inject(ApiService);
131
-
132
- environment: Environment = {
133
- work_mode: 'hfcloud',
134
- cloud_token: '',
135
- spark_endpoint: ''
136
- };
137
-
138
- loading = true;
139
- saving = false;
140
- message = '';
141
- isError = false;
142
-
143
- ngOnInit() {
144
- this.loadEnvironment();
145
- }
146
-
147
- loadEnvironment() {
148
- this.loading = true;
149
- this.apiService.getEnvironment().subscribe({
150
- next: (env) => {
151
- this.environment = env;
152
- this.loading = false;
153
- },
154
- error: (err) => {
155
- this.showMessage('Failed to load environment configuration', true);
156
- this.loading = false;
157
- }
158
- });
159
- }
160
-
161
- onWorkModeChange() {
162
- if (this.environment.work_mode === 'on-premise') {
163
- this.environment.cloud_token = '';
164
- }
165
- }
166
-
167
- save() {
168
- this.saving = true;
169
- this.message = '';
170
-
171
- this.apiService.updateEnvironment(this.environment).subscribe({
172
- next: () => {
173
- this.showMessage('Environment configuration saved successfully', false);
174
- this.saving = false;
175
- },
176
- error: (err) => {
177
- this.showMessage(err.error?.detail || 'Failed to save configuration', true);
178
- this.saving = false;
179
- }
180
- });
181
- }
182
-
183
- testConnection() {
184
- // TODO: Implement connection test
185
- this.showMessage('Testing connection to Spark endpoint...', false);
186
- setTimeout(() => {
187
- this.showMessage('Connection successful!', false);
188
- }, 1000);
189
- }
190
-
191
- reloadFromSpark() {
192
- // TODO: Implement reload from Spark
193
- this.showMessage('Reloading configuration from Spark...', false);
194
- setTimeout(() => {
195
- this.loadEnvironment();
196
- this.showMessage('Configuration reloaded', false);
197
- }, 1000);
198
- }
199
-
200
- private showMessage(message: string, isError: boolean) {
201
- this.message = message;
202
- this.isError = isError;
203
-
204
- if (!isError) {
205
- setTimeout(() => {
206
- this.message = '';
207
- }, 5000);
208
- }
209
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  }
 
1
+ import { Component, inject, OnInit } from '@angular/core';
2
+ import { CommonModule } from '@angular/common';
3
+ import { FormsModule } from '@angular/forms';
4
+ import { MatCardModule } from '@angular/material/card';
5
+ import { MatFormFieldModule } from '@angular/material/form-field';
6
+ import { MatInputModule } from '@angular/material/input';
7
+ import { MatSelectModule } from '@angular/material/select';
8
+ import { MatButtonModule } from '@angular/material/button';
9
+ import { MatIconModule } from '@angular/material/icon';
10
+ import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
11
+ import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
12
+ import { ApiService, Environment } from '../../services/api.service';
13
+
14
+ @Component({
15
+ selector: 'app-environment',
16
+ standalone: true,
17
+ imports: [
18
+ CommonModule,
19
+ FormsModule,
20
+ MatCardModule,
21
+ MatFormFieldModule,
22
+ MatInputModule,
23
+ MatSelectModule,
24
+ MatButtonModule,
25
+ MatIconModule,
26
+ MatProgressSpinnerModule,
27
+ MatSnackBarModule
28
+ ],
29
+ template: `
30
+ <div class="environment-container">
31
+ <mat-card>
32
+ <mat-card-header>
33
+ <mat-card-title>
34
+ <mat-icon>settings</mat-icon>
35
+ Environment Configuration
36
+ </mat-card-title>
37
+ </mat-card-header>
38
+
39
+ <mat-card-content>
40
+ <form (ngSubmit)="save()" #envForm="ngForm">
41
+ <mat-form-field appearance="outline" class="full-width">
42
+ <mat-label>Work Mode</mat-label>
43
+ <mat-select
44
+ name="workMode"
45
+ [(ngModel)]="environment.work_mode"
46
+ (selectionChange)="onWorkModeChange()"
47
+ required
48
+ [disabled]="loading"
49
+ >
50
+ <mat-option value="hfcloud">HF Cloud</mat-option>
51
+ <mat-option value="cloud">Cloud</mat-option>
52
+ <mat-option value="on-premise">On-Premise</mat-option>
53
+ </mat-select>
54
+ <mat-icon matPrefix>cloud</mat-icon>
55
+ </mat-form-field>
56
+
57
+ <mat-form-field appearance="outline" class="full-width">
58
+ <mat-label>Cloud Token</mat-label>
59
+ <input
60
+ matInput
61
+ type="password"
62
+ name="cloudToken"
63
+ [(ngModel)]="environment.cloud_token"
64
+ [disabled]="loading || environment.work_mode === 'on-premise'"
65
+ placeholder="Enter cloud token"
66
+ >
67
+ <mat-icon matPrefix>vpn_key</mat-icon>
68
+ <mat-hint>Required for HF Cloud and Cloud modes</mat-hint>
69
+ </mat-form-field>
70
+
71
+ <mat-form-field appearance="outline" class="full-width">
72
+ <mat-label>Spark Endpoint</mat-label>
73
+ <input
74
+ matInput
75
+ type="url"
76
+ name="sparkEndpoint"
77
+ [(ngModel)]="environment.spark_endpoint"
78
+ required
79
+ [disabled]="loading"
80
+ placeholder="https://spark-service.example.com"
81
+ >
82
+ <mat-icon matPrefix>link</mat-icon>
83
+ <button
84
+ mat-icon-button
85
+ matSuffix
86
+ type="button"
87
+ (click)="testConnection()"
88
+ [disabled]="loading || !environment.spark_endpoint"
89
+ matTooltip="Test Connection"
90
+ >
91
+ <mat-icon>wifi_tethering</mat-icon>
92
+ </button>
93
+ </mat-form-field>
94
+
95
+ <div class="form-actions">
96
+ <button
97
+ mat-raised-button
98
+ color="primary"
99
+ type="submit"
100
+ [disabled]="loading || !envForm.valid || saving"
101
+ >
102
+ @if (saving) {
103
+ <mat-spinner diameter="20"></mat-spinner>
104
+ Saving...
105
+ } @else {
106
+ <mat-icon>save</mat-icon>
107
+ Save
108
+ }
109
+ </button>
110
+
111
+ <button
112
+ mat-raised-button
113
+ type="button"
114
+ (click)="reloadFromSpark()"
115
+ [disabled]="loading"
116
+ >
117
+ <mat-icon>refresh</mat-icon>
118
+ Reload from Spark
119
+ </button>
120
+ </div>
121
+ </form>
122
+ </mat-card-content>
123
+ </mat-card>
124
+ </div>
125
+ `,
126
+ styles: [`
127
+ .environment-container {
128
+ max-width: 800px;
129
+ margin: 0 auto;
130
+ }
131
+
132
+ mat-card-header {
133
+ margin-bottom: 24px;
134
+
135
+ mat-card-title {
136
+ display: flex;
137
+ align-items: center;
138
+ gap: 8px;
139
+ font-size: 24px;
140
+
141
+ mat-icon {
142
+ font-size: 28px;
143
+ width: 28px;
144
+ height: 28px;
145
+ }
146
+ }
147
+ }
148
+
149
+ .full-width {
150
+ width: 100%;
151
+ margin-bottom: 20px;
152
+ }
153
+
154
+ .form-actions {
155
+ display: flex;
156
+ gap: 16px;
157
+ margin-top: 24px;
158
+ padding-top: 24px;
159
+ border-top: 1px solid #e0e0e0;
160
+
161
+ button {
162
+ display: flex;
163
+ align-items: center;
164
+ gap: 8px;
165
+
166
+ mat-spinner {
167
+ margin-right: 8px;
168
+ }
169
+ }
170
+ }
171
+
172
+ ::ng-deep {
173
+ .mat-mdc-form-field-icon-prefix {
174
+ padding-right: 12px;
175
+ }
176
+
177
+ .mat-mdc-progress-spinner {
178
+ --mdc-circular-progress-active-indicator-color: white;
179
+ }
180
+ }
181
+ `]
182
+ })
183
+ export class EnvironmentComponent implements OnInit {
184
+ private apiService = inject(ApiService);
185
+ private snackBar = inject(MatSnackBar);
186
+
187
+ environment: Environment = {
188
+ work_mode: 'hfcloud',
189
+ cloud_token: '',
190
+ spark_endpoint: ''
191
+ };
192
+
193
+ loading = true;
194
+ saving = false;
195
+
196
+ ngOnInit() {
197
+ this.loadEnvironment();
198
+ }
199
+
200
+ loadEnvironment() {
201
+ this.loading = true;
202
+ this.apiService.getEnvironment().subscribe({
203
+ next: (env) => {
204
+ this.environment = env;
205
+ this.loading = false;
206
+ },
207
+ error: (err) => {
208
+ this.snackBar.open('Failed to load environment configuration', 'Close', {
209
+ duration: 5000,
210
+ panelClass: 'error-snackbar'
211
+ });
212
+ this.loading = false;
213
+ }
214
+ });
215
+ }
216
+
217
+ onWorkModeChange() {
218
+ if (this.environment.work_mode === 'on-premise') {
219
+ this.environment.cloud_token = '';
220
+ }
221
+ }
222
+
223
+ save() {
224
+ this.saving = true;
225
+
226
+ this.apiService.updateEnvironment(this.environment).subscribe({
227
+ next: () => {
228
+ this.snackBar.open('Environment configuration saved successfully', 'Close', {
229
+ duration: 3000
230
+ });
231
+ this.saving = false;
232
+ },
233
+ error: (err) => {
234
+ this.snackBar.open(
235
+ err.error?.detail || 'Failed to save configuration',
236
+ 'Close',
237
+ { duration: 5000, panelClass: 'error-snackbar' }
238
+ );
239
+ this.saving = false;
240
+ }
241
+ });
242
+ }
243
+
244
+ testConnection() {
245
+ this.snackBar.open('Testing connection to Spark endpoint...', undefined, {
246
+ duration: 2000
247
+ });
248
+
249
+ // TODO: Implement actual connection test
250
+ setTimeout(() => {
251
+ this.snackBar.open('Connection successful!', 'Close', {
252
+ duration: 3000
253
+ });
254
+ }, 2000);
255
+ }
256
+
257
+ reloadFromSpark() {
258
+ this.snackBar.open('Reloading configuration from Spark...', undefined, {
259
+ duration: 2000
260
+ });
261
+
262
+ setTimeout(() => {
263
+ this.loadEnvironment();
264
+ this.snackBar.open('Configuration reloaded', 'Close', {
265
+ duration: 3000
266
+ });
267
+ }, 1000);
268
+ }
269
  }