]>
Commit | Line | Data |
---|---|---|
aee94f69 TL |
1 | import { Component, OnInit } from '@angular/core'; |
2 | import { UntypedFormControl, Validators } from '@angular/forms'; | |
3 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; | |
4 | import { RgwRealmService } from '~/app/shared/api/rgw-realm.service'; | |
5 | import { ActionLabelsI18n } from '~/app/shared/constants/app.constants'; | |
6 | import { NotificationType } from '~/app/shared/enum/notification-type.enum'; | |
7 | import { CdFormGroup } from '~/app/shared/forms/cd-form-group'; | |
8 | import { CdValidators } from '~/app/shared/forms/cd-validators'; | |
9 | import { NotificationService } from '~/app/shared/services/notification.service'; | |
10 | import { RgwRealm } from '../models/rgw-multisite'; | |
11 | import { DocService } from '~/app/shared/services/doc.service'; | |
12 | ||
13 | @Component({ | |
14 | selector: 'cd-rgw-multisite-realm-form', | |
15 | templateUrl: './rgw-multisite-realm-form.component.html', | |
16 | styleUrls: ['./rgw-multisite-realm-form.component.scss'] | |
17 | }) | |
18 | export class RgwMultisiteRealmFormComponent implements OnInit { | |
19 | action: string; | |
20 | multisiteRealmForm: CdFormGroup; | |
21 | info: any; | |
22 | editing = false; | |
23 | resource: string; | |
24 | multisiteInfo: object[] = []; | |
25 | realm: RgwRealm; | |
26 | realmList: RgwRealm[] = []; | |
27 | zonegroupList: RgwRealm[] = []; | |
28 | realmNames: string[]; | |
29 | newRealmName: string; | |
30 | isMaster: boolean; | |
31 | defaultsInfo: string[]; | |
32 | defaultRealmDisabled = false; | |
33 | docUrl: string; | |
34 | ||
35 | constructor( | |
36 | public activeModal: NgbActiveModal, | |
37 | public actionLabels: ActionLabelsI18n, | |
38 | public rgwRealmService: RgwRealmService, | |
39 | public notificationService: NotificationService, | |
40 | public docService: DocService | |
41 | ) { | |
42 | this.action = this.editing | |
43 | ? this.actionLabels.EDIT + this.resource | |
44 | : this.actionLabels.CREATE + this.resource; | |
45 | this.createForm(); | |
46 | } | |
47 | ||
48 | createForm() { | |
49 | this.multisiteRealmForm = new CdFormGroup({ | |
50 | realmName: new UntypedFormControl(null, { | |
51 | validators: [ | |
52 | Validators.required, | |
53 | CdValidators.custom('uniqueName', (realmName: string) => { | |
54 | return ( | |
55 | this.action === 'create' && | |
56 | this.realmNames && | |
57 | this.realmNames.indexOf(realmName) !== -1 | |
58 | ); | |
59 | }) | |
60 | ] | |
61 | }), | |
62 | default_realm: new UntypedFormControl(false) | |
63 | }); | |
64 | } | |
65 | ||
66 | ngOnInit(): void { | |
67 | this.realmList = | |
68 | this.multisiteInfo[0] !== undefined && this.multisiteInfo[0].hasOwnProperty('realms') | |
69 | ? this.multisiteInfo[0]['realms'] | |
70 | : []; | |
71 | this.realmNames = this.realmList.map((realm) => { | |
72 | return realm['name']; | |
73 | }); | |
74 | if (this.action === 'edit') { | |
75 | this.zonegroupList = | |
76 | this.multisiteInfo[1] !== undefined && this.multisiteInfo[1].hasOwnProperty('zonegroups') | |
77 | ? this.multisiteInfo[1]['zonegroups'] | |
78 | : []; | |
79 | this.multisiteRealmForm.get('realmName').setValue(this.info.data.name); | |
80 | this.multisiteRealmForm.get('default_realm').setValue(this.info.data.is_default); | |
81 | if (this.info.data.is_default) { | |
82 | this.multisiteRealmForm.get('default_realm').disable(); | |
83 | } | |
84 | } | |
85 | this.zonegroupList.forEach((zgp: any) => { | |
86 | if (zgp.is_master === true && zgp.realm_id === this.info.data.id) { | |
87 | this.isMaster = true; | |
88 | } | |
89 | }); | |
90 | if (this.defaultsInfo && this.defaultsInfo['defaultRealmName'] !== null) { | |
91 | this.multisiteRealmForm.get('default_realm').disable(); | |
92 | this.defaultRealmDisabled = true; | |
93 | } | |
94 | this.docUrl = this.docService.urlGenerator('rgw-multisite'); | |
95 | } | |
96 | ||
97 | submit() { | |
98 | const values = this.multisiteRealmForm.getRawValue(); | |
99 | this.realm = new RgwRealm(); | |
100 | if (this.action === 'create') { | |
101 | this.realm.name = values['realmName']; | |
102 | this.rgwRealmService.create(this.realm, values['default_realm']).subscribe( | |
103 | () => { | |
104 | this.notificationService.show( | |
105 | NotificationType.success, | |
106 | $localize`Realm: '${values['realmName']}' created successfully` | |
107 | ); | |
108 | this.activeModal.close(); | |
109 | }, | |
110 | () => { | |
111 | this.multisiteRealmForm.setErrors({ cdSubmitButton: true }); | |
112 | } | |
113 | ); | |
114 | } else if (this.action === 'edit') { | |
115 | this.realm.name = this.info.data.name; | |
116 | this.newRealmName = values['realmName']; | |
117 | this.rgwRealmService.update(this.realm, values['default_realm'], this.newRealmName).subscribe( | |
118 | () => { | |
119 | this.notificationService.show( | |
120 | NotificationType.success, | |
121 | $localize`Realm: '${values['realmName']}' updated successfully` | |
122 | ); | |
123 | this.activeModal.close(); | |
124 | }, | |
125 | () => { | |
126 | this.multisiteRealmForm.setErrors({ cdSubmitButton: true }); | |
127 | } | |
128 | ); | |
129 | } | |
130 | } | |
131 | } |