summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/components/confirmation-modal/confirmation-modal.component.ts
blob: fe56249816a01fb794725badaf8612f026fb3876 (plain)
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
import { Component, OnDestroy, OnInit, TemplateRef } from '@angular/core';
import { FormGroup } from '@angular/forms';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'cd-confirmation-modal',
  templateUrl: './confirmation-modal.component.html',
  styleUrls: ['./confirmation-modal.component.scss']
})
export class ConfirmationModalComponent implements OnInit, OnDestroy {
  // Needed
  buttonText: string;
  titleText: string;
  onSubmit: Function;

  // One of them is needed
  bodyTpl?: TemplateRef<any>;
  description?: TemplateRef<any>;

  // Optional
  warning = false;
  bodyData?: object;
  onCancel?: Function;
  bodyContext?: object;
  showSubmit = true;

  // Component only
  boundCancel = this.cancel.bind(this);
  confirmationForm: FormGroup;
  private canceled = false;

  constructor(public activeModal: NgbActiveModal) {
    this.confirmationForm = new FormGroup({});
  }

  ngOnInit() {
    this.bodyContext = this.bodyContext || {};
    this.bodyContext['$implicit'] = this.bodyData;
    if (!this.onSubmit) {
      throw new Error('No submit action defined');
    } else if (!this.buttonText) {
      throw new Error('No action name defined');
    } else if (!this.titleText) {
      throw new Error('No title defined');
    } else if (!this.bodyTpl && !this.description) {
      throw new Error('No description defined');
    }
  }

  ngOnDestroy() {
    if (this.onCancel && this.canceled) {
      this.onCancel();
    }
  }

  cancel() {
    this.canceled = true;
    this.activeModal.close();
  }

  stopLoadingSpinner() {
    this.confirmationForm.setErrors({ cdSubmitButton: true });
  }
}