This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
/
edit-application.component.ts
137 lines (119 loc) · 3.92 KB
/
edit-application.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { of as observableOf, Observable, Subscription } from 'rxjs';
import { map, filter, take } from 'rxjs/operators';
import { Component, OnDestroy, OnInit, Inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Http } from '@angular/http';
import { ErrorStateMatcher, ShowOnDirtyErrorStateMatcher } from '@angular/material';
import { Store } from '@ngrx/store';
import { StepOnNextFunction } from '../../../shared/components/stepper/step/step.component';
import { AppMetadataTypes } from '../../../store/actions/app-metadata.actions';
import { SetCFDetails, SetNewAppName } from '../../../store/actions/create-applications-page.actions';
import { AppState } from '../../../store/app-state';
import { AppNameUniqueChecking, AppNameUniqueDirective } from '../app-name-unique.directive/app-name-unique.directive';
import { ApplicationService } from '../application.service';
@Component({
selector: 'app-edit-application',
templateUrl: './edit-application.component.html',
styleUrls: ['./edit-application.component.scss'],
providers: [
{ provide: ErrorStateMatcher, useClass: ShowOnDirtyErrorStateMatcher }
]
})
export class EditApplicationComponent implements OnInit, OnDestroy {
editAppForm: FormGroup;
uniqueNameValidator: AppNameUniqueDirective;
appNameChecking: AppNameUniqueChecking = new AppNameUniqueChecking();
constructor(
public applicationService: ApplicationService,
private store: Store<AppState>,
private fb: FormBuilder,
private http: Http,
) {
this.uniqueNameValidator = new AppNameUniqueDirective(this.store, this.http);
this.editAppForm = this.fb.group({
name: ['',
[Validators.required],
[this.uniqueNameValidator],
],
instances: [0, [
Validators.required,
Validators.min(0)
]],
disk_quota: [0, [
Validators.required,
Validators.min(0)
]],
memory: [0, [
Validators.required,
Validators.min(0)
]],
enable_ssh: false,
production: false
});
}
private app: any = {
entity: {}
};
private sub: Subscription;
private error = false;
ngOnInit() {
this.sub = this.applicationService.application$.pipe(
filter(app => app.app.entity),
take(1),
map(app => app.app.entity)
).subscribe(app => {
this.app = app;
this.store.dispatch(new SetCFDetails({
cloudFoundry: this.applicationService.cfGuid,
org: '',
space: this.app.space_guid,
}));
this.store.dispatch(new SetNewAppName(this.app.name));
this.editAppForm.setValue({
name: this.app.name,
instances: this.app.instances,
memory: this.app.memory,
disk_quota: this.app.disk_quota,
production: this.app.production,
enable_ssh: this.app.enable_ssh,
});
// Don't want the values to change while the user is editing
this.clearSub();
});
}
updateApp: StepOnNextFunction = () => {
const updates = {};
// We will only send the values that were actually edited
for (const key of Object.keys(this.editAppForm.value)) {
if (!this.editAppForm.controls[key].pristine) {
updates[key] = this.editAppForm.value[key];
}
}
let obs$: Observable<any>;
if (Object.keys(updates).length) {
// We had at least one value to change - send update action
obs$ = this.applicationService.updateApplication(updates, [AppMetadataTypes.SUMMARY]).pipe(map(v => (
{
success: !v.error,
message: `Could not update application: ${v.message}`
})));
} else {
obs$ = observableOf({ success: true });
}
return obs$.pipe(take(1), map(res => {
return {
...res,
redirect: res.success
};
}));
}
clearSub() {
if (this.sub) {
this.sub.unsubscribe();
this.sub = undefined;
}
}
ngOnDestroy() {
this.clearSub();
}
}