]>
Commit | Line | Data |
---|---|---|
aee94f69 TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormControl, Validators } from '@angular/forms'; | |
3 | import { Observable } from 'rxjs'; | |
4 | ||
5 | import { Icons } from '~/app/shared/enum/icons.enum'; | |
6 | import { Permission } from '~/app/shared/models/permissions'; | |
7 | import { ActionLabelsI18n } from '~/app/shared/constants/app.constants'; | |
8 | import { AuthStorageService } from '~/app/shared/services/auth-storage.service'; | |
9 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; | |
10 | import { UpgradeService } from '~/app/shared/api/upgrade.service'; | |
11 | import { UpgradeInfoInterface } from '~/app/shared/models/upgrade.interface'; | |
12 | import { NotificationType } from '~/app/shared/enum/notification-type.enum'; | |
13 | import { CdFormGroup } from '~/app/shared/forms/cd-form-group'; | |
14 | import { NotificationService } from '~/app/shared/services/notification.service'; | |
15 | ||
16 | @Component({ | |
17 | selector: 'cd-upgrade-start-modal.component', | |
18 | templateUrl: './upgrade-start-modal.component.html', | |
19 | styleUrls: ['./upgrade-start-modal.component.scss'] | |
20 | }) | |
21 | export class UpgradeStartModalComponent implements OnInit { | |
22 | permission: Permission; | |
23 | upgradeInfoError$: Observable<any>; | |
24 | upgradeInfo$: Observable<UpgradeInfoInterface>; | |
25 | upgradeForm: CdFormGroup; | |
26 | icons = Icons; | |
27 | versions: string[]; | |
28 | ||
29 | showImageField = false; | |
30 | ||
31 | constructor( | |
32 | public actionLabels: ActionLabelsI18n, | |
33 | private authStorageService: AuthStorageService, | |
34 | public activeModal: NgbActiveModal, | |
35 | private upgradeService: UpgradeService, | |
36 | private notificationService: NotificationService | |
37 | ) { | |
38 | this.permission = this.authStorageService.getPermissions().configOpt; | |
39 | } | |
40 | ||
41 | ngOnInit() { | |
42 | this.upgradeForm = new CdFormGroup({ | |
43 | availableVersions: new FormControl(null, [Validators.required]), | |
44 | useImage: new FormControl(false), | |
45 | customImageName: new FormControl(null) | |
46 | }); | |
47 | if (this.versions === undefined) { | |
48 | const availableVersionsControl = this.upgradeForm.get('availableVersions'); | |
49 | availableVersionsControl.clearValidators(); | |
50 | const customImageNameControl = this.upgradeForm.get('customImageName'); | |
51 | customImageNameControl.setValidators(Validators.required); | |
52 | customImageNameControl.updateValueAndValidity(); | |
53 | } | |
54 | } | |
55 | ||
56 | startUpgrade() { | |
57 | const version = this.upgradeForm.getValue('availableVersions'); | |
58 | const image = this.upgradeForm.getValue('customImageName'); | |
59 | this.upgradeService.start(version, image).subscribe({ | |
60 | next: () => { | |
61 | this.notificationService.show( | |
62 | NotificationType.success, | |
63 | $localize`Started upgrading the cluster` | |
64 | ); | |
65 | }, | |
66 | error: (error) => { | |
67 | this.upgradeForm.setErrors({ cdSubmitButton: true }); | |
68 | this.notificationService.show( | |
69 | NotificationType.error, | |
70 | $localize`Failed to start the upgrade`, | |
71 | error | |
72 | ); | |
73 | }, | |
74 | complete: () => { | |
75 | this.activeModal.close(); | |
76 | } | |
77 | }); | |
78 | } | |
79 | ||
80 | useImage() { | |
81 | this.showImageField = !this.showImageField; | |
82 | const availableVersionsControl = this.upgradeForm.get('availableVersions'); | |
83 | const customImageNameControl = this.upgradeForm.get('customImageName'); | |
84 | ||
85 | if (this.showImageField) { | |
86 | availableVersionsControl.disable(); | |
87 | availableVersionsControl.clearValidators(); | |
88 | ||
89 | customImageNameControl.setValidators(Validators.required); | |
90 | customImageNameControl.updateValueAndValidity(); | |
91 | } else { | |
92 | availableVersionsControl.enable(); | |
93 | availableVersionsControl.setValidators(Validators.required); | |
94 | availableVersionsControl.updateValueAndValidity(); | |
95 | ||
96 | customImageNameControl.clearValidators(); | |
97 | } | |
98 | } | |
99 | } |