member_webapp/@overflow/noauth-probe/component/noauth-probe-list.component.ts

148 lines
4.4 KiB
TypeScript
Raw Normal View History

2018-05-31 07:38:44 +00:00
import { Component, Input, Output, EventEmitter, AfterContentInit, OnInit, OnDestroy } from '@angular/core';
2018-05-31 06:48:49 +00:00
import { Store, select } from '@ngrx/store';
2018-05-31 07:38:44 +00:00
import { Observable, of, Subscription } from 'rxjs';
2018-05-31 06:48:49 +00:00
import { catchError, exhaustMap, map, tap } from 'rxjs/operators';
2018-04-11 08:41:38 +00:00
import { ConfirmationService, Message } from 'primeng/primeng';
2018-04-24 05:31:20 +00:00
2018-05-31 06:48:49 +00:00
import { NoAuthProbe } from '@overflow/commons-typescript/model/noauth';
2018-06-01 09:45:28 +00:00
import { AuthSelector } from '@overflow/shared/auth/store';
2018-05-31 06:48:49 +00:00
import { DomainMember } from '@overflow/commons-typescript/model/domain';
import { NoAuthProbeService } from '../service/noauth-probe.service';
2018-05-31 13:11:23 +00:00
import { NoAuthProbeSubscriber, NoAuthProbeNotify } from '../subscriber/noauth-probe.subscriber';
2018-05-31 06:48:49 +00:00
2018-04-11 06:20:23 +00:00
@Component({
2018-05-28 05:41:56 +00:00
selector: 'of-noauth-probe-list',
templateUrl: './noauth-probe-list.component.html',
2018-05-28 13:40:44 +00:00
providers: [ConfirmationService]
2018-04-11 06:20:23 +00:00
})
2018-05-31 07:38:44 +00:00
export class NoAuthProbeListComponent implements OnInit, OnDestroy {
2018-05-31 06:48:49 +00:00
pending$: Observable<boolean>;
error$: Observable<any>;
2018-04-06 11:02:18 +00:00
2018-05-31 07:38:44 +00:00
noauthProbes: NoAuthProbe[];
2018-06-01 09:56:00 +00:00
noauthProbeSubscription: Subscription;
2018-05-31 07:38:44 +00:00
2018-04-11 06:20:23 +00:00
constructor(
2018-04-11 08:41:38 +00:00
private confirmationService: ConfirmationService,
2018-05-31 06:48:49 +00:00
private store: Store<any>,
private noAuthProbeService: NoAuthProbeService,
2018-05-31 13:11:23 +00:00
private noAuthProbeSubscriber: NoAuthProbeSubscriber,
2018-04-11 06:20:23 +00:00
) {
2018-05-31 08:27:00 +00:00
this.noauthProbes = [];
2018-04-11 06:20:23 +00:00
}
2018-04-06 11:02:18 +00:00
2018-05-31 06:48:49 +00:00
ngOnInit() {
this.store.pipe(
tap(() => {
this.pending$ = of(true);
}),
2018-06-01 09:45:28 +00:00
select(AuthSelector.selectDomainMember),
2018-05-31 06:48:49 +00:00
exhaustMap((domainMember: DomainMember) =>
this.noAuthProbeService.readAllByDomainID(domainMember.domain.id)
.pipe(
map((noauthProbes: NoAuthProbe[]) => {
2018-05-31 08:37:38 +00:00
this.setNoauthProbes(noauthProbes);
2018-05-31 06:48:49 +00:00
}),
catchError(error => {
this.setError$(error);
return of();
})
)
),
tap(() => {
this.pending$ = of(false);
}),
).take(1).subscribe();
2018-06-01 09:56:00 +00:00
this.noauthProbeSubscription = this.noAuthProbeSubscriber.observable().pipe(
2018-05-31 13:11:23 +00:00
tap((noAuthProbeNotify: NoAuthProbeNotify) => {
2018-05-31 07:38:44 +00:00
const noauthProbes = [];
this.noauthProbes.forEach(noauthProbe => {
2018-05-31 13:11:23 +00:00
const n = noAuthProbeNotify.params;
if (noauthProbe.id === n.id) {
noauthProbes.push(n);
2018-05-31 07:38:44 +00:00
} else {
noauthProbes.push(noauthProbe);
}
});
2018-05-31 08:37:38 +00:00
this.setNoauthProbes(noauthProbes);
2018-05-31 07:38:44 +00:00
}
),
).subscribe();
2018-05-31 06:48:49 +00:00
}
2018-05-31 07:38:44 +00:00
ngOnDestroy(): void {
2018-06-01 09:56:00 +00:00
this.noauthProbeSubscription.unsubscribe();
2018-05-31 07:38:44 +00:00
}
2018-04-24 05:47:03 +00:00
onAcceptOrDeny(isAccept: boolean, selected: NoAuthProbe) {
2018-04-11 08:41:38 +00:00
const title = isAccept ?
'Are you sure to accept this Probe?' : 'Are you sure to deny this Probe';
const message = isAccept ?
'Start collecting data as a Probe.' : 'It will be permanently deleted.';
this.confirmationService.confirm({
header: title,
message: message,
icon: isAccept ? 'fa-check' : 'fa fa-trash',
accept: () => {
2018-05-31 06:48:49 +00:00
if (isAccept) {
this.noAuthProbeService.acceptNoAuthProbe(selected.id)
.pipe(
tap(() => {
this.pending$ = of(true);
}),
map((noauthProbes: NoAuthProbe[]) => {
2018-05-31 08:37:38 +00:00
this.setNoauthProbes(noauthProbes);
2018-05-31 06:48:49 +00:00
}),
catchError(error => {
this.setError$(error);
return of();
}),
tap(() => {
this.pending$ = of(false);
}),
).take(1).subscribe();
} else {
this.noAuthProbeService.denyNoauthProbe(selected.id)
.pipe(
tap(() => {
this.pending$ = of(true);
}),
map((noauthProbes: NoAuthProbe[]) => {
2018-05-31 08:37:38 +00:00
this.setNoauthProbes(noauthProbes);
2018-05-31 06:48:49 +00:00
}),
catchError(error => {
this.setError$(error);
return of();
}),
tap(() => {
this.pending$ = of(false);
}),
).take(1).subscribe();
}
2018-04-11 08:41:38 +00:00
},
reject: () => {
}
});
}
2018-05-31 06:48:49 +00:00
2018-05-31 08:37:38 +00:00
private setNoauthProbes(noauthProbes: NoAuthProbe[]): void {
2018-05-31 06:48:49 +00:00
if (null === noauthProbes) {
return;
}
noauthProbes.forEach(noauthProbe => {
noauthProbe.descriptions = JSON.parse(noauthProbe.description);
});
2018-05-31 07:38:44 +00:00
this.noauthProbes = noauthProbes;
2018-05-31 06:48:49 +00:00
}
private setError$(error: any): void {
this.error$ = of(error);
}
2018-04-11 06:20:23 +00:00
}