1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import * as _ from 'lodash';
6 import { BsModalRef } from 'ngx-bootstrap/modal';
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
10 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
11 import { CdValidators } from '../../../shared/forms/cd-validators';
12 import { RgwUserS3Key } from '../models/rgw-user-s3-key';
15 selector: 'cd-rgw-user-s3-key-modal',
16 templateUrl: './rgw-user-s3-key-modal.component.html',
17 styleUrls: ['./rgw-user-s3-key-modal.component.scss']
19 export class RgwUserS3KeyModalComponent {
21 * The event that is triggered when the 'Add' button as been pressed.
24 submitAction = new EventEmitter();
26 formGroup: CdFormGroup;
28 userCandidates: string[] = [];
33 private formBuilder: CdFormBuilder,
34 public bsModalRef: BsModalRef,
36 public actionLabels: ActionLabelsI18n
38 this.resource = this.i18n('S3 Key');
43 this.formGroup = this.formBuilder.group({
44 user: [null, [Validators.required]],
46 access_key: [null, [CdValidators.requiredIf({ generate_key: false })]],
47 secret_key: [null, [CdValidators.requiredIf({ generate_key: false })]]
52 * Set the 'viewing' flag. If set to TRUE, the modal dialog is in 'View' mode,
53 * otherwise in 'Add' mode. According to the mode the dialog and its controls
55 * @param {boolean} viewing
57 setViewing(viewing: boolean = true) {
58 this.viewing = viewing;
59 this.action = this.viewing ? this.actionLabels.SHOW : this.actionLabels.CREATE;
63 * Set the values displayed in the dialog.
65 setValues(user: string, access_key: string, secret_key: string) {
66 this.formGroup.setValue({
68 generate_key: _.isEmpty(access_key),
69 access_key: access_key,
70 secret_key: secret_key
75 * Set the user candidates displayed in the 'Username' dropdown box.
77 setUserCandidates(candidates: string[]) {
78 this.userCandidates = candidates;
82 const key: RgwUserS3Key = this.formGroup.value;
83 this.submitAction.emit(key);
84 this.bsModalRef.hide();