hermes-web-angular/src/app/policies/policy/policy.component.ts

63 lines
1.9 KiB
TypeScript

import { Component, inject, OnDestroy } from '@angular/core';
import { PolicyTableComponent } from "../policy-table/policy-table.component";
import { Policy } from '../../shared/models/policy';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { PolicyAddButtonComponent } from "../policy-add-button/policy-add-button.component";
import { Group } from '../../shared/models/group';
import PolicyService from '../../shared/services/policy.service';
import GroupService from '../../shared/services/group.service';
@Component({
selector: 'policy',
imports: [
MatButtonModule,
MatIconModule,
PolicyTableComponent,
RouterModule,
PolicyAddButtonComponent,
],
templateUrl: './policy.component.html',
styleUrl: './policy.component.scss'
})
export class PolicyComponent implements OnDestroy {
private readonly route = inject(ActivatedRoute);
private readonly policyService = inject(PolicyService);
private readonly groupService = inject(GroupService);
private readonly _subscriptions: any[] = [];
private _policies: Policy[] = [];
private _groups: Group[] = [];
constructor() {
this.route.data.subscribe((data) => {
this._policies = data['policies'];
this._groups = data['groups'];
});
this._subscriptions.push(this.policyService.delete$?.subscribe(_ =>
this.policyService.fetch().subscribe(p => this._policies = p)));
this._subscriptions.push(this.groupService.deleteGroup$?.subscribe(_ =>
this.groupService.fetch().subscribe(g => this._groups = g.groups)));
}
ngOnDestroy(): void {
this._subscriptions.filter(s => !!s).forEach(s => s.unsubscribe());
}
get policies() {
return this._policies;
}
get groups() {
return this._groups;
}
compare(a: Policy, b: Policy) {
return a.path.localeCompare(b.path);
}
}