2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 import { MatSort } from '@angular/material';
22 import { Component, OnInit, ViewChild, Input, AfterViewInit } from '@angular/core';
23 import { MatDialog } from '@angular/material/dialog';
24 import { PolicyType } from '../interfaces/policy.types';
25 import { PolicyInstanceDataSource } from './policy-instance.datasource';
26 import { ErrorDialogService } from '../services/ui/error-dialog.service';
27 import { NotificationService } from '../services/ui/notification.service';
28 import { PolicyService } from '../services/policy/policy.service';
29 import { ConfirmDialogService } from './../services/ui/confirm-dialog.service';
30 import { PolicyInstance } from '../interfaces/policy.types';
31 import { PolicyInstanceDialogComponent } from './policy-instance-dialog.component';
32 import { getPolicyDialogProperties } from './policy-instance-dialog.component';
33 import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
34 import { Observable } from 'rxjs';
35 import { UiService } from '../services/ui/ui.service';
38 selector: 'rd-policy-instance',
39 templateUrl: './policy-instance.component.html',
40 styleUrls: ['./policy-instance.component.scss']
44 export class PolicyInstanceComponent implements OnInit, AfterViewInit {
45 instanceDataSource: PolicyInstanceDataSource;
46 @Input() policyType: PolicyType;
47 @Input() expanded: Observable<boolean>;
48 @ViewChild(MatSort, { static: true }) sort: MatSort;
52 private policySvc: PolicyService,
53 private dialog: MatDialog,
54 private errorDialogService: ErrorDialogService,
55 private notificationService: NotificationService,
56 private confirmDialogService: ConfirmDialogService,
57 private ui: UiService) {
61 this.instanceDataSource = new PolicyInstanceDataSource(this.policySvc, this.sort, this.notificationService, this.policyType);
62 this.expanded.subscribe((isExpanded: boolean) => this.onExpand(isExpanded));
63 this.ui.darkModeState.subscribe((isDark) => {
64 this.darkMode = isDark;
69 this.instanceDataSource.sort = this.sort;
72 private onExpand(isExpanded: boolean) {
74 this.instanceDataSource.loadTable();
78 modifyInstance(instance: PolicyInstance): void {
79 this.policySvc.getPolicy(this.policyType.name, instance.id).subscribe(
80 (refreshedJson: any) => {
81 instance.json = JSON.stringify(refreshedJson);
82 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode));
84 (httpError: HttpErrorResponse) => {
85 this.notificationService.error('Could not refresh instance ' + httpError.message);
86 this.dialog.open(PolicyInstanceDialogComponent, getPolicyDialogProperties(this.policyType, instance, this.darkMode));
91 hasInstances(): boolean {
92 return this.instanceDataSource.rowCount > 0;
95 toLocalTime(utcTime: string): string {
96 const date = new Date(utcTime);
97 const toutc = date.toUTCString();
98 return new Date(toutc + " UTC").toLocaleString();
102 deleteInstance(instance: PolicyInstance): void {
103 this.confirmDialogService
104 .openConfirmDialog('Are you sure you want to delete this policy instance?')
105 .afterClosed().subscribe(
108 this.policySvc.deletePolicy(this.policyType.name, instance.id)
110 (response: HttpResponse<Object>) => {
111 switch (response.status) {
113 this.notificationService.success('Delete succeeded!');
114 this.instanceDataSource.loadTable();
117 this.notificationService.warn('Delete failed.');
120 (error: HttpErrorResponse) => {
121 this.errorDialogService.displayError(error.message);