Fixed error when re-selecting policies tab.

This commit is contained in:
Tom 2025-01-13 19:08:43 +00:00
parent 2692294b4b
commit 07db9000d8

View File

@ -17,6 +17,7 @@ export class PolicyTableComponent implements OnInit, OnDestroy {
@Input() policies: Policy[] = []
displayedColumns = ['path', 'group', 'usage', 'span', 'actions']
groups: { [id: string]: { id: string, name: string, priority: number } }
private readonly _subscriptions: Subscription[] = [];
@ViewChild(MatTable) table: MatTable<Policy>;
private subscription: Subscription | undefined;
@ -27,7 +28,7 @@ export class PolicyTableComponent implements OnInit, OnDestroy {
}
ngOnInit(): void {
this.subscription = this.events.listen('addPolicy', (payload) => {
this._subscriptions.push(this.events.listen('addPolicy', (payload) => {
if (!payload)
return;
if (this.policies.map(p => p.path).includes(payload)) {
@ -36,12 +37,14 @@ export class PolicyTableComponent implements OnInit, OnDestroy {
this.policies.push(new Policy("", "", payload, 1, 5000, "", true, true));
this.table.renderRows();
});
this.hermes.subscribe(4, (response: any) => {
}));
const subscription = this.hermes.subscribe(4, (response: any) => {
if (response.request.type == "get_policies") {
for (let policy of response.data) {
this.policies.push(new Policy(policy.id, policy.group_id, policy.path, policy.usage, policy.span, "", false, false));
}
console.log('getting policies', response.data);
this.table.renderRows();
} else if (response.request.type == "create_policy") {
const policy = this.policies.find(p => this.groups[response.data.group_id].name == p.temp_group_name && p.path == response.data.path);
@ -79,13 +82,18 @@ export class PolicyTableComponent implements OnInit, OnDestroy {
this.groups = Object.assign({}, ...response.data.groups.map((g: any) => ({ [g.id]: g })));
}
});
if (subscription) {
this._subscriptions.push(subscription);
}
this.hermes.fetchPolicies();
this.hermes.fetchPermissionsAndGroups();
}
ngOnDestroy(): void {
if (this.subscription)
this.subscription.unsubscribe();
if (this._subscriptions.length > 0)
this._subscriptions.forEach(s => s.unsubscribe());
}
cancel(policy: Policy) {