]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, OnDestroy, OnInit } from '@angular/core'; |
2 | import { AbstractControl, FormControl, Validators } from '@angular/forms'; | |
3 | ||
f67539c2 | 4 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
11fdf7f2 TL |
5 | import { Subscription } from 'rxjs'; |
6 | ||
f67539c2 TL |
7 | import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service'; |
8 | import { ActionLabelsI18n } from '~/app/shared/constants/app.constants'; | |
9 | import { CdFormGroup } from '~/app/shared/forms/cd-form-group'; | |
10 | import { FinishedTask } from '~/app/shared/models/finished-task'; | |
11 | import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service'; | |
11fdf7f2 TL |
12 | import { PoolEditModeResponseModel } from './pool-edit-mode-response.model'; |
13 | ||
14 | @Component({ | |
15 | selector: 'cd-pool-edit-mode-modal', | |
16 | templateUrl: './pool-edit-mode-modal.component.html', | |
17 | styleUrls: ['./pool-edit-mode-modal.component.scss'] | |
18 | }) | |
19 | export class PoolEditModeModalComponent implements OnInit, OnDestroy { | |
20 | poolName: string; | |
21 | ||
22 | subs: Subscription; | |
23 | ||
24 | editModeForm: CdFormGroup; | |
25 | bsConfig = { | |
26 | containerClass: 'theme-default' | |
27 | }; | |
28 | pattern: string; | |
29 | ||
30 | response: PoolEditModeResponseModel; | |
31 | peerExists = false; | |
32 | ||
33 | mirrorModes: Array<{ id: string; name: string }> = [ | |
f67539c2 TL |
34 | { id: 'disabled', name: $localize`Disabled` }, |
35 | { id: 'pool', name: $localize`Pool` }, | |
36 | { id: 'image', name: $localize`Image` } | |
11fdf7f2 TL |
37 | ]; |
38 | ||
39 | constructor( | |
f67539c2 TL |
40 | public activeModal: NgbActiveModal, |
41 | public actionLabels: ActionLabelsI18n, | |
11fdf7f2 TL |
42 | private rbdMirroringService: RbdMirroringService, |
43 | private taskWrapper: TaskWrapperService | |
44 | ) { | |
45 | this.createForm(); | |
46 | } | |
47 | ||
48 | createForm() { | |
49 | this.editModeForm = new CdFormGroup({ | |
50 | mirrorMode: new FormControl('', { | |
51 | validators: [Validators.required, this.validateMode.bind(this)] | |
52 | }) | |
53 | }); | |
54 | } | |
55 | ||
56 | ngOnInit() { | |
57 | this.pattern = `${this.poolName}`; | |
58 | this.rbdMirroringService.getPool(this.poolName).subscribe((resp: PoolEditModeResponseModel) => { | |
59 | this.setResponse(resp); | |
60 | }); | |
61 | ||
f6b5b4d7 | 62 | this.subs = this.rbdMirroringService.subscribeSummary((data) => { |
11fdf7f2 | 63 | this.peerExists = false; |
11fdf7f2 | 64 | const poolData = data.content_data.pools; |
9f95a23c | 65 | const pool = poolData.find((o: any) => this.poolName === o['name']); |
11fdf7f2 TL |
66 | this.peerExists = pool && pool['peer_uuids'].length; |
67 | }); | |
68 | } | |
69 | ||
70 | ngOnDestroy(): void { | |
71 | this.subs.unsubscribe(); | |
72 | } | |
73 | ||
74 | validateMode(control: AbstractControl) { | |
75 | if (control.value === 'disabled' && this.peerExists) { | |
76 | return { cannotDisable: { value: control.value } }; | |
77 | } | |
78 | return null; | |
79 | } | |
80 | ||
81 | setResponse(response: PoolEditModeResponseModel) { | |
82 | this.editModeForm.get('mirrorMode').setValue(response.mirror_mode); | |
83 | } | |
84 | ||
85 | update() { | |
86 | const request = new PoolEditModeResponseModel(); | |
87 | request.mirror_mode = this.editModeForm.getValue('mirrorMode'); | |
88 | ||
89 | const action = this.taskWrapper.wrapTaskAroundCall({ | |
90 | task: new FinishedTask('rbd/mirroring/pool/edit', { | |
91 | pool_name: this.poolName | |
92 | }), | |
93 | call: this.rbdMirroringService.updatePool(this.poolName, request) | |
94 | }); | |
95 | ||
f67539c2 TL |
96 | action.subscribe({ |
97 | error: () => this.editModeForm.setErrors({ cdSubmitButton: true }), | |
98 | complete: () => { | |
11fdf7f2 | 99 | this.rbdMirroringService.refresh(); |
f67539c2 | 100 | this.activeModal.close(); |
11fdf7f2 | 101 | } |
f67539c2 | 102 | }); |
11fdf7f2 TL |
103 | } |
104 | } |